diff --git a/vitrage/synchronizer/plugins/__init__.py b/vitrage/synchronizer/plugins/__init__.py index 505fcc37d..3a22886c1 100644 --- a/vitrage/synchronizer/plugins/__init__.py +++ b/vitrage/synchronizer/plugins/__init__.py @@ -31,4 +31,8 @@ OPTS = [ help='Nagios url for querying the data. Example: ' ' http:///monitoring/nagios/cgi-bin/status.cgi' ), + cfg.StrOpt('nagios_config_file', + default='/etc/vitrage/nagios_conf.yaml', + help='Nagios configuration file' + ), ] diff --git a/vitrage/synchronizer/plugins/nagios/config.py b/vitrage/synchronizer/plugins/nagios/config.py new file mode 100644 index 000000000..c203f9a01 --- /dev/null +++ b/vitrage/synchronizer/plugins/nagios/config.py @@ -0,0 +1,51 @@ +# Copyright 2016 - Nokia +# +# 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 vitrage.common import file_utils + + +class NagiosConfig(object): + + NAGIOS = 'nagios' + NAGIOS_HOST = 'nagios_host' + HOST = 'host' + TYPE = 'type' + NAME = 'name' + + def __init__(self, conf): + self.rules = [] + + nagios_config = file_utils.load_yaml_file( + conf.synchronizer_plugins.nagios_config_file) + + for config in nagios_config[self.NAGIOS]: + self.rules.append(NagiosHostMapping(config[self.NAGIOS_HOST], + config[self.TYPE], + config[self.NAME])) + + +class NagiosHostMapping(object): + def __init__(self, nagios_host, type, name): + self.nagios_host = nagios_host + self.type = type + self.name = name + + def applies(self, service): + """Check if the rule applies to this service + + :param service: + :return: true/false + """ + + # TODO(iafek) implement it + pass diff --git a/vitrage/tests/resources/nagios/nagios_conf.yaml b/vitrage/tests/resources/nagios/nagios_conf.yaml new file mode 100644 index 000000000..90510dd3a --- /dev/null +++ b/vitrage/tests/resources/nagios/nagios_conf.yaml @@ -0,0 +1,16 @@ +nagios: + - nagios_host: compute-1 + type: nova.host + name: compute-1 + + - nagios_host: compute-2 + type: nova.host + name: host2 + + - nagios_host: compute-(.*) + type: nova.host + name: ${nagios_host} + + - nagios_host: instance-(.*) + type: nova.instance + name: ${nagios_host} diff --git a/vitrage/tests/unit/synchronizer/nagios/test_nagios_config.py b/vitrage/tests/unit/synchronizer/nagios/test_nagios_config.py new file mode 100644 index 000000000..4d6c6e4f1 --- /dev/null +++ b/vitrage/tests/unit/synchronizer/nagios/test_nagios_config.py @@ -0,0 +1,83 @@ +# Copyright 2016 - Nokia +# +# 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 +from oslo_log import log as logging + +from vitrage.synchronizer.plugins.nagios.config import NagiosConfig +from vitrage.synchronizer.plugins.nagios.config import NagiosHostMapping +from vitrage.tests import base +from vitrage.tests.mocks import utils + +LOG = logging.getLogger(__name__) + + +class TestNagiosConfig(base.BaseTest): + + OPTS = [ + cfg.StrOpt('nagios_config_file', + default=utils.get_resources_dir() + + '/nagios/nagios_conf.yaml', + help='Nagios configuation file' + ), + ] + + # the rules match the ones in nagios_conf.yaml + RULE_1 = NagiosHostMapping('compute-1', 'nova.host', 'compute-1') + RULE_2 = NagiosHostMapping('compute-2', 'nova.host', 'host2') + RULE_3 = NagiosHostMapping('compute-(.*)', 'nova.host', '${nagios_host}') + RULE_4 = NagiosHostMapping('instance-(.*)', + 'nova.instance', + '${nagios_host}') + RULES = [RULE_1, RULE_2, RULE_3, RULE_4] + + NON_EXISTING_RULE_1 = NagiosHostMapping('X', 'nova.host', 'compute-1') + NON_EXISTING_RULE_2 = NagiosHostMapping('compute-1', 'X', 'compute-1') + NON_EXISTING_RULE_3 = NagiosHostMapping('compute-1', 'nova.host', 'X') + NON_EXISTING_RULES = [NON_EXISTING_RULE_1, + NON_EXISTING_RULE_2, + NON_EXISTING_RULE_3] + + @classmethod + def setUpClass(cls): + cls.conf = cfg.ConfigOpts() + cls.conf.register_opts(cls.OPTS, group='synchronizer_plugins') + + def test_nagios_configuration_loading(self): + # Action + nagios_conf = NagiosConfig(self.conf) + + # Test assertions + rules = nagios_conf.rules + self.assertIsNotNone(nagios_conf, "no nagios configuration loaded") + self.assertEqual(len(self.RULES), len(rules)) + + for expected_rule in self.RULES: + self.assertTrue(TestNagiosConfig._check_contains(expected_rule, + rules)) + for expected_rule in self.NON_EXISTING_RULES: + self.assertFalse(TestNagiosConfig._check_contains(expected_rule, + rules)) + + @staticmethod + def _check_contains(expected_rule, rules): + for rule in rules: + if TestNagiosConfig._assert_equals(expected_rule, rule): + return True + return False + + @staticmethod + def _assert_equals(rule1, rule2): + return rule1.nagios_host == rule2.nagios_host and \ + rule1.type == rule2.type and \ + rule1.name == rule2.name