Add implement of comparing host status

This patch adds implementation of comparing if host status
changed.

Change-Id: Ibd009a27765acd4673ff75ffb097088c091f101b
Implements: bp pythonize-host-and-process-monitor
This commit is contained in:
Kengo Takahara
2016-12-28 13:25:56 +09:00
committed by takahara.kengo
parent 29d82c3ce2
commit fc8a8568dd
3 changed files with 132 additions and 0 deletions

View File

@@ -19,8 +19,10 @@ from oslo_log import log as oslo_logging
import masakarimonitors.conf
import masakarimonitors.hostmonitor.host_handler.driver as driver
from masakarimonitors.hostmonitor.host_handler import hold_host_status
from masakarimonitors.hostmonitor.host_handler import parse_cib_xml
from masakarimonitors.i18n import _LE
from masakarimonitors.i18n import _LI
from masakarimonitors.i18n import _LW
from masakarimonitors import utils
@@ -38,6 +40,7 @@ class HandleHost(driver.DriverBase):
super(HandleHost, self).__init__()
self.my_hostname = socket.gethostname()
self.xml_parser = parse_cib_xml.ParseCibXml()
self.status_holder = hold_host_status.HostHoldStatus()
def _check_host_status_by_crmadmin(self):
try:
@@ -78,6 +81,43 @@ class HandleHost(driver.DriverBase):
return out
def _check_if_status_changed(self, node_state_tag_list):
# Check if host status changed.
for node_state_tag in node_state_tag_list:
# hostmonitor doesn't monitor itself.
if node_state_tag.get('uname') == self.my_hostname:
continue
# Get current status and old status.
current_status = node_state_tag.get('crmd')
old_status = self.status_holder.get_host_status(
node_state_tag.get('uname'))
# If old_status is None, This is first get of host status.
if old_status is None:
msg = ("Recognized '%s' as a new member of cluster."
" Host status is '%s'.") \
% (node_state_tag.get('uname'), current_status)
LOG.info(_LI("%s"), msg)
self.status_holder.set_host_status(node_state_tag)
continue
# Output host status.
msg = ("'%s' is '%s'.") % (node_state_tag.get('uname'),
current_status)
LOG.info(_LI("%s"), msg)
# If host status changed, send a notification.
if current_status != old_status:
# TODO(takahara.kengo)
# Implement the notification processing.
pass
# Update host status.
self.status_holder.set_host_status(node_state_tag)
def _check_host_status_by_cibadmin(self):
# Get xml of cib info.
cib_xml = self._get_cib_xml()
@@ -101,6 +141,9 @@ class HandleHost(driver.DriverBase):
raise Exception(
"Failed to get node_state tag from cib xml.")
# Check if status changed.
self._check_if_status_changed(node_state_tag_list)
return 0
def stop(self):

View File

@@ -0,0 +1,46 @@
# Copyright(c) 2016 Nippon Telegraph and Telephone Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
class HostHoldStatus(object):
"""Hold host status.
This class holds the host status.
"""
def __init__(self):
# host_status is dictionary like {'hostname': 'online'}.
self.host_status = {}
def set_host_status(self, node_state_tag):
"""Setter method.
This method set host status by node_state tag of cib xml.
:params node_state_tag: node_state tag of cib xml.
"""
self.host_status[node_state_tag.get('uname')] = \
node_state_tag.get('crmd')
def get_host_status(self, hostname):
"""Getter method.
This method returns the requested host status.
host status is 'online' or 'offline'.
:params hostname: Hostname to get status.
:returns: Host status.
"""
return self.host_status.get(hostname)

View File

@@ -0,0 +1,43 @@
# Copyright(c) 2016 Nippon Telegraph and Telephone Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import testtools
from xml.etree import ElementTree
import eventlet
from masakarimonitors.hostmonitor.host_handler import hold_host_status
eventlet.monkey_patch(os=False)
NODE_STATE_XML = '<node_state uname="masakari-node" crmd="online">' \
' <test hoge="hoge"/>' \
'</node_state>'
NODE_STATE_TAG = ElementTree.fromstring(NODE_STATE_XML)
class TestHostHoldStatus(testtools.TestCase):
def setUp(self):
super(TestHostHoldStatus, self).setUp()
def test_set_host_status(self):
obj = hold_host_status.HostHoldStatus()
obj.set_host_status(NODE_STATE_TAG)
self.assertEqual('online', obj.get_host_status('masakari-node'))
def test_get_host_status(self):
obj = hold_host_status.HostHoldStatus()
obj.set_host_status(NODE_STATE_TAG)
self.assertEqual('online', obj.get_host_status('masakari-node'))