From 191724d60da6266a9ab8eb492e990d1b46fc3f9b Mon Sep 17 00:00:00 2001 From: Kengo Takahara Date: Mon, 26 Dec 2016 19:07:59 +0900 Subject: [PATCH] Add implement of loading hostmonitor driver This pathc adds implementation of loading hostmonitor driver using stevedore. Change-Id: I29666484b35cb5f295b0ab183e9bb3cbbcc28d21 Implements: bp pythonize-host-and-process-monitor --- masakarimonitors/conf/__init__.py | 2 + masakarimonitors/conf/host.py | 30 +++++++++++++++ masakarimonitors/hostmonitor/host.py | 38 ++++++++++++++++++- .../hostmonitor/host_handler/__init__.py | 0 .../hostmonitor/host_handler/driver.py | 32 ++++++++++++++++ .../hostmonitor/host_handler/handle_host.py | 28 ++++++++++++++ setup.cfg | 4 ++ 7 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 masakarimonitors/conf/host.py create mode 100644 masakarimonitors/hostmonitor/host_handler/__init__.py create mode 100644 masakarimonitors/hostmonitor/host_handler/driver.py create mode 100644 masakarimonitors/hostmonitor/host_handler/handle_host.py diff --git a/masakarimonitors/conf/__init__.py b/masakarimonitors/conf/__init__.py index 745ec70..f72ba9f 100644 --- a/masakarimonitors/conf/__init__.py +++ b/masakarimonitors/conf/__init__.py @@ -15,6 +15,7 @@ from oslo_config import cfg from masakarimonitors.conf import api from masakarimonitors.conf import base +from masakarimonitors.conf import host from masakarimonitors.conf import instance from masakarimonitors.conf import process from masakarimonitors.conf import service @@ -23,6 +24,7 @@ CONF = cfg.CONF api.register_opts(CONF) base.register_opts(CONF) +host.register_opts(CONF) instance.register_opts(CONF) process.register_opts(CONF) service.register_opts(CONF) diff --git a/masakarimonitors/conf/host.py b/masakarimonitors/conf/host.py new file mode 100644 index 0000000..e55f2f0 --- /dev/null +++ b/masakarimonitors/conf/host.py @@ -0,0 +1,30 @@ +# 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. +from oslo_config import cfg + +monitor_host_opts = [ + cfg.StrOpt('monitoring_driver', + default='default', + help='Driver that hostmonitor uses for monitoring hosts.'), +] + + +def register_opts(conf): + conf.register_opts(monitor_host_opts, group='host') + + +def list_opts(): + return { + 'host': monitor_host_opts + } diff --git a/masakarimonitors/hostmonitor/host.py b/masakarimonitors/hostmonitor/host.py index 3dd9474..f78ac67 100644 --- a/masakarimonitors/hostmonitor/host.py +++ b/masakarimonitors/hostmonitor/host.py @@ -12,11 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os +from stevedore import driver + from oslo_log import log as oslo_logging +import masakarimonitors.conf +from masakarimonitors.i18n import _LE from masakarimonitors import manager LOG = oslo_logging.getLogger(__name__) +CONF = masakarimonitors.conf.CONF class HostmonitorManager(manager.Manager): @@ -25,8 +31,38 @@ class HostmonitorManager(manager.Manager): def __init__(self, *args, **kwargs): super(HostmonitorManager, self).__init__( service_name="hostmonitor", *args, **kwargs) + self.driver = None + + def init_host(self): + """Initialization for hostmonitor.""" + try: + # Determine dynamic load driver from configuration. + driver_name = CONF.host.monitoring_driver + + # Load the driver to global. + self.driver = driver.DriverManager( + namespace='hostmonitor.driver', + name=driver_name, + invoke_on_load=True, + invoke_args=(), + ) + except Exception as e: + LOG.exception( + _LE("Exception caught during initializing hostmonitor: %s"), + e) + os._exit(1) + + def stop(self): + self.driver.driver.stop() def main(self): """Main method.""" - pass + try: + # Call the host monitoring driver. + self.driver.driver.monitor_hosts() + + except Exception as e: + LOG.exception(_LE("Exception caught: %s"), e) + + return diff --git a/masakarimonitors/hostmonitor/host_handler/__init__.py b/masakarimonitors/hostmonitor/host_handler/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/masakarimonitors/hostmonitor/host_handler/driver.py b/masakarimonitors/hostmonitor/host_handler/driver.py new file mode 100644 index 0000000..008be32 --- /dev/null +++ b/masakarimonitors/hostmonitor/host_handler/driver.py @@ -0,0 +1,32 @@ +# 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 abc +import six + + +@six.add_metaclass(abc.ABCMeta) +class DriverBase(object): + """Driver Base class. + + This class is base of monitoring hosts. + """ + + def __init__(self): + pass + + @abc.abstractmethod + def monitor_hosts(self): + """Must override monitor_hosts method.""" + pass diff --git a/masakarimonitors/hostmonitor/host_handler/handle_host.py b/masakarimonitors/hostmonitor/host_handler/handle_host.py new file mode 100644 index 0000000..12b685a --- /dev/null +++ b/masakarimonitors/hostmonitor/host_handler/handle_host.py @@ -0,0 +1,28 @@ +# 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 masakarimonitors.hostmonitor.host_handler.driver as driver + + +class HandleHost(driver.DriverBase): + """Handle hosts. + + This class handles the host status. + """ + + def __init__(self): + super(HandleHost, self).__init__() + + def monitor_hosts(self): + pass diff --git a/setup.cfg b/setup.cfg index d14536b..6a75873 100644 --- a/setup.cfg +++ b/setup.cfg @@ -46,6 +46,10 @@ console_scripts = masakari-processmonitor = masakarimonitors.cmd.processmonitor:main masakari-hostmonitor = masakarimonitors.cmd.hostmonitor:main +hostmonitor.driver = + simple = masakarimonitors.hostmonitor.host_handler.handle_host:HandleHost + default = masakarimonitors.hostmonitor.host_handler.handle_host:HandleHost + [build_sphinx] source-dir = doc/source build-dir = doc/build