diff --git a/etc/masakarimonitors/process_list.yaml.sample b/etc/masakarimonitors/process_list.yaml.sample new file mode 100644 index 0000000..0ca6be9 --- /dev/null +++ b/etc/masakarimonitors/process_list.yaml.sample @@ -0,0 +1,62 @@ +# Define the monitoring processes as follows: +# process_name: [Name of the process as it in 'ps -ef'.] +# start_command: [Start command of the process.] +# pre_start_command: [Command which is executed before start_command.] +# post_start_command: [Command which is executed after start_command.] +# restart_command: [Restart command of the process.] +# pre_restart_command: [Command which is executed before restart_command.] +# post_restart_command: [Command which is executed after restart_command.] +# run_as_root: [Bool value whether to execute commands as root authority.] +# +# These definitions need to be set according to the environment to use. +# Sample of definitions is shown below. +- + # libvirt-bin + process_name: /usr/sbin/libvirtd + start_command: systemctl start libvirt-bin + pre_start_command: + post_start_command: + restart_command: systemctl restart libvirt-bin + pre_restart_command: + post_restart_command: + run_as_root: True +- + # nova-compute + process_name: /usr/local/bin/nova-compute + start_command: systemctl start nova-compute + pre_start_command: + post_start_command: + restart_command: systemctl restart service nova-compute + pre_restart_command: + post_restart_command: + run_as_root: True +- + # instancemonitor + process_name: /usr/bin/python /usr/local/bin/masakari-instancemonitor + start_command: systemctl start masakari-instancemonitor + pre_start_command: + post_start_command: + restart_command: systemctl restart masakari-instancemonitor + pre_restart_command: + post_restart_command: + run_as_root: True +- + # hostmonitor + process_name: /usr/bin/python /usr/local/bin/masakari-hostmonitor + start_command: systemctl start masakari-hostmonitor + pre_start_command: + post_start_command: + restart_command: systemctl restart masakari-hostmonitor + pre_restart_command: + post_restart_command: + run_as_root: True +- + # sshd + process_name: /usr/sbin/sshd + start_command: systemctl start ssh + pre_start_command: + post_start_command: + restart_command: systemctl restart ssh + pre_restart_command: + post_restart_command: + run_as_root: True diff --git a/masakarimonitors/conf/__init__.py b/masakarimonitors/conf/__init__.py index 406cc35..745ec70 100644 --- a/masakarimonitors/conf/__init__.py +++ b/masakarimonitors/conf/__init__.py @@ -16,6 +16,7 @@ from oslo_config import cfg from masakarimonitors.conf import api from masakarimonitors.conf import base from masakarimonitors.conf import instance +from masakarimonitors.conf import process from masakarimonitors.conf import service CONF = cfg.CONF @@ -23,4 +24,5 @@ CONF = cfg.CONF api.register_opts(CONF) base.register_opts(CONF) instance.register_opts(CONF) +process.register_opts(CONF) service.register_opts(CONF) diff --git a/masakarimonitors/conf/process.py b/masakarimonitors/conf/process.py new file mode 100644 index 0000000..b5de217 --- /dev/null +++ b/masakarimonitors/conf/process.py @@ -0,0 +1,31 @@ +# 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_process_opts = [ + cfg.StrOpt('process_list_path', + default='/etc/masakarimonitors/process_list.yaml', + help='The file path of process list.'), +] + + +def register_opts(conf): + conf.register_opts(monitor_process_opts, group='process') + + +def list_opts(): + return { + 'process': monitor_process_opts + } diff --git a/masakarimonitors/processmonitor/process.py b/masakarimonitors/processmonitor/process.py index 076352d..bb12630 100644 --- a/masakarimonitors/processmonitor/process.py +++ b/masakarimonitors/processmonitor/process.py @@ -12,11 +12,16 @@ # See the License for the specific language governing permissions and # limitations under the License. +import yaml + 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 ProcessmonitorManager(manager.Manager): @@ -26,7 +31,31 @@ class ProcessmonitorManager(manager.Manager): super(ProcessmonitorManager, self).__init__( service_name="processmonitor", *args, **kwargs) + def _load_process_list(self): + try: + process_list = yaml.load(file(CONF.process.process_list_path)) + LOG.debug("Loaded process list. %s" % process_list) + + return process_list + except yaml.YAMLError as e: + LOG.exception(_LE("YAMLError caught: %s"), e) + return + except Exception as e: + LOG.exception(_LE("Exception caught: %s"), e) + return + def main(self): """Main method.""" - pass + try: + # Load process list. + process_list = self._load_process_list() + if process_list is None: + LOG.error(_LE("Failed to load process list file.")) + return + + except Exception as e: + LOG.exception(_LE("Exception caught: %s"), e) + return + + return diff --git a/masakarimonitors/tests/unit/__init__.py b/masakarimonitors/tests/unit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/masakarimonitors/tests/unit/processmonitor/__init__.py b/masakarimonitors/tests/unit/processmonitor/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/masakarimonitors/tests/unit/processmonitor/test_process.py b/masakarimonitors/tests/unit/processmonitor/test_process.py new file mode 100644 index 0000000..5b47be1 --- /dev/null +++ b/masakarimonitors/tests/unit/processmonitor/test_process.py @@ -0,0 +1,50 @@ +# 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 eventlet +eventlet.monkey_patch(os=False) + +import mock +import yaml + +import testtools + +from masakarimonitors.processmonitor import process as processmonitor_manager + +MOCK_PROCESS_LIST = [ + { + 'process_name': 'mock_process_name_A', + 'start_command': 'mock_start_command', + 'pre_start_command': 'mock_pre_start_command', + 'post_start_command': 'mock_post_start_command', + 'restart_command': 'mock_restart_command', + 'pre_restart_command': 'mock_pre_restart_command', + 'post_restart_command': 'mock_post_restart_command', + 'run_as_root': True + }, +] + + +class TestProcessmonitorManager(testtools.TestCase): + + def setUp(self): + super(TestProcessmonitorManager, self).setUp() + + @mock.patch.object(yaml, 'load') + def test_main(self, mock_load): + + mock_load.return_value = MOCK_PROCESS_LIST + + obj = processmonitor_manager.ProcessmonitorManager() + obj.main()