From 3ae0be599b4433919e3db6526a26070fea536db5 Mon Sep 17 00:00:00 2001 From: Xav Paice Date: Fri, 2 Oct 2020 13:13:22 +1300 Subject: [PATCH] Add NRPE checks for services Adds the nrpe-external-master layer and checks for services managed by this charm. func-test-pr: https://github.com/openstack-charmers/zaza-openstack-tests/pull/436 Change-Id: Ice5eada838626059f8cb2c427f7b9b0884317660 Closes-Bug: #1897809 Related-Bug: #1933643 Co-authored-by: Aurelien Lourot --- src/config.yaml | 15 +++++++++++++++ src/layer.yaml | 2 +- src/lib/charm/openstack/designate.py | 10 ++++++++++ src/metadata.yaml | 3 +++ src/reactive/designate_handlers.py | 12 ++++++++++++ src/tests/bundles/bionic-queens.yaml | 6 +++++- src/tests/bundles/bionic-rocky.yaml | 6 +++++- src/tests/bundles/bionic-stein.yaml | 6 +++++- src/tests/bundles/bionic-train.yaml | 6 +++++- src/tests/bundles/bionic-ussuri.yaml | 4 ++++ src/tests/bundles/focal-ussuri.yaml | 8 +++++++- src/tests/bundles/focal-victoria.yaml | 8 +++++++- src/tests/bundles/focal-wallaby.yaml | 8 +++++++- src/tests/bundles/groovy-victoria.yaml | 8 +++++++- src/tests/bundles/hirsute-wallaby.yaml | 8 +++++++- src/tests/bundles/xenial-mitaka.yaml | 6 +++++- src/tests/bundles/xenial-ocata.yaml | 6 +++++- src/tests/bundles/xenial-pike.yaml | 6 +++++- src/tests/bundles/xenial-queens.yaml | 6 +++++- src/tests/tests.yaml | 8 ++++++++ unit_tests/__init__.py | 1 + unit_tests/test_designate_handlers.py | 8 ++++++++ unit_tests/test_lib_charm_openstack_designate.py | 12 ++++++++++++ 23 files changed, 149 insertions(+), 14 deletions(-) diff --git a/src/config.yaml b/src/config.yaml index bab878b..40450d5 100644 --- a/src/config.yaml +++ b/src/config.yaml @@ -128,3 +128,18 @@ options: description: | Default for how old deleted zones should be (deleted_at) to be purged, in seconds. + nagios_context: + default: "juju" + type: string + description: | + A string that will be prepended to instance name to set the host name + in nagios. So for instance the hostname would be something like: + juju-myservice-0 + If you're running multiple environments with the same services in them + this allows you to differentiate between them. + nagios_servicegroups: + default: "" + type: string + description: | + A comma-separated list of nagios servicegroups. If left empty, the + nagios_context will be used as the servicegroup. diff --git a/src/layer.yaml b/src/layer.yaml index 92d2677..8cff467 100644 --- a/src/layer.yaml +++ b/src/layer.yaml @@ -1,4 +1,4 @@ -includes: ['layer:openstack-api', 'layer:leadership', 'interface:bind-rndc', 'interface:hacluster', 'interface:openstack-ha', 'interface:memcache', 'interface:designate'] +includes: ['layer:openstack-api', 'layer:leadership', 'interface:bind-rndc', 'interface:hacluster', 'interface:openstack-ha', 'interface:memcache', 'interface:designate', 'interface:nrpe-external-master'] options: basic: use_venv: True diff --git a/src/lib/charm/openstack/designate.py b/src/lib/charm/openstack/designate.py index fb2ea6e..f2306e8 100644 --- a/src/lib/charm/openstack/designate.py +++ b/src/lib/charm/openstack/designate.py @@ -19,6 +19,7 @@ import subprocess import uuid import charmhelpers.contrib.openstack.utils as ch_utils +import charmhelpers.contrib.charmsupport.nrpe as nrpe import charms_openstack.adapters as openstack_adapters import charms_openstack.charm as openstack_charm import charms_openstack.ip as os_ip @@ -613,6 +614,15 @@ class DesignateCharm(ch_plugins.PolicydOverridePlugin, hookenv.leader_set({'pool-manager-cache-sync-done': True}) self.restart_all() + def render_nrpe(self): + """Configure Nagios NRPE checks.""" + hostname = nrpe.get_nagios_hostname() + current_unit = nrpe.get_nagios_unit_name() + charm_nrpe = nrpe.NRPE(hostname=hostname) + nrpe.add_init_service_checks( + charm_nrpe, self.services, current_unit) + charm_nrpe.write() + class DesignateCharmQueens(DesignateCharm): diff --git a/src/metadata.yaml b/src/metadata.yaml index c05f082..56e4a71 100644 --- a/src/metadata.yaml +++ b/src/metadata.yaml @@ -24,6 +24,9 @@ provides: interface: designate nova-designate: interface: nova-designate + nrpe-external-master: + interface: nrpe-external-master + scope: container requires: dns-backend: interface: bind-rndc diff --git a/src/reactive/designate_handlers.py b/src/reactive/designate_handlers.py index 0247544..141c101 100644 --- a/src/reactive/designate_handlers.py +++ b/src/reactive/designate_handlers.py @@ -254,3 +254,15 @@ def reset_shared_db(): """ reactive.remove_state('shared-db.setup') reactive.remove_state('db.synched') + + +@reactive.when_not('is-update-status-hook') +@reactive.when('base-config.rendered') +@reactive.when_any('config.changed.nagios_context', + 'config.changed.nagios_servicegroups', + 'endpoint.nrpe-external-master.changed', + 'nrpe-external-master.available') +def configure_nrpe(): + """Handle config-changed for NRPE options.""" + with charm.provide_charm_instance() as charm_instance: + charm_instance.render_nrpe() diff --git a/src/tests/bundles/bionic-queens.yaml b/src/tests/bundles/bionic-queens.yaml index 516adc1..7c1ef35 100644 --- a/src/tests/bundles/bionic-queens.yaml +++ b/src/tests/bundles/bionic-queens.yaml @@ -48,6 +48,8 @@ applications: nameservers: 'ns1.amuletexample.com. ns2.amuletexample.com.' to: - '5' + nrpe: + charm: cs:nrpe relations: - - 'designate:shared-db' @@ -69,4 +71,6 @@ relations: - - 'neutron-api:shared-db' - 'percona-cluster:shared-db' - - 'neutron-api:amqp' - - 'rabbitmq-server:amqp' \ No newline at end of file + - 'rabbitmq-server:amqp' +- - 'designate:nrpe-external-master' + - 'nrpe:nrpe-external-master' diff --git a/src/tests/bundles/bionic-rocky.yaml b/src/tests/bundles/bionic-rocky.yaml index c84d6c6..4dc7a81 100644 --- a/src/tests/bundles/bionic-rocky.yaml +++ b/src/tests/bundles/bionic-rocky.yaml @@ -53,6 +53,8 @@ applications: openstack-origin: cloud:bionic-rocky to: - '5' + nrpe: + charm: cs:nrpe relations: - - 'designate:shared-db' @@ -74,4 +76,6 @@ relations: - - 'neutron-api:shared-db' - 'percona-cluster:shared-db' - - 'neutron-api:amqp' - - 'rabbitmq-server:amqp' \ No newline at end of file + - 'rabbitmq-server:amqp' +- - 'designate:nrpe-external-master' + - 'nrpe:nrpe-external-master' diff --git a/src/tests/bundles/bionic-stein.yaml b/src/tests/bundles/bionic-stein.yaml index 794d8a2..aa47c96 100644 --- a/src/tests/bundles/bionic-stein.yaml +++ b/src/tests/bundles/bionic-stein.yaml @@ -53,6 +53,8 @@ applications: openstack-origin: cloud:bionic-stein to: - '5' + nrpe: + charm: cs:nrpe relations: - - 'designate:shared-db' @@ -74,4 +76,6 @@ relations: - - 'neutron-api:shared-db' - 'percona-cluster:shared-db' - - 'neutron-api:amqp' - - 'rabbitmq-server:amqp' \ No newline at end of file + - 'rabbitmq-server:amqp' +- - 'designate:nrpe-external-master' + - 'nrpe:nrpe-external-master' diff --git a/src/tests/bundles/bionic-train.yaml b/src/tests/bundles/bionic-train.yaml index c101ebf..454d82d 100644 --- a/src/tests/bundles/bionic-train.yaml +++ b/src/tests/bundles/bionic-train.yaml @@ -53,6 +53,8 @@ applications: openstack-origin: cloud:bionic-train to: - '5' + nrpe: + charm: cs:nrpe relations: - - 'designate:shared-db' @@ -74,4 +76,6 @@ relations: - - 'neutron-api:shared-db' - 'percona-cluster:shared-db' - - 'neutron-api:amqp' - - 'rabbitmq-server:amqp' \ No newline at end of file + - 'rabbitmq-server:amqp' +- - 'designate:nrpe-external-master' + - 'nrpe:nrpe-external-master' diff --git a/src/tests/bundles/bionic-ussuri.yaml b/src/tests/bundles/bionic-ussuri.yaml index 6c542ab..4bf08e7 100644 --- a/src/tests/bundles/bionic-ussuri.yaml +++ b/src/tests/bundles/bionic-ussuri.yaml @@ -54,6 +54,8 @@ applications: openstack-origin: cloud:bionic-ussuri to: - '5' + nrpe: + charm: cs:nrpe relations: - - 'designate:shared-db' @@ -76,3 +78,5 @@ relations: - 'percona-cluster:shared-db' - - 'neutron-api:amqp' - 'rabbitmq-server:amqp' +- - 'designate:nrpe-external-master' + - 'nrpe:nrpe-external-master' diff --git a/src/tests/bundles/focal-ussuri.yaml b/src/tests/bundles/focal-ussuri.yaml index dfd26a8..af64a0b 100644 --- a/src/tests/bundles/focal-ussuri.yaml +++ b/src/tests/bundles/focal-ussuri.yaml @@ -74,7 +74,7 @@ applications: memcached: charm: cs:~memcached-team/memcached num_units: 1 - # holding charm at bionic as it isn't available for focal yet + # holding charm at bionic as it isn't available for focal+ yet series: bionic to: - '7' @@ -88,6 +88,9 @@ applications: to: - '8' + nrpe: + charm: cs:nrpe + relations: - - 'keystone:shared-db' - 'keystone-mysql-router:shared-db' @@ -124,3 +127,6 @@ relations: - - 'neutron-api:amqp' - 'rabbitmq-server:amqp' + + - - 'designate:nrpe-external-master' + - 'nrpe:nrpe-external-master' diff --git a/src/tests/bundles/focal-victoria.yaml b/src/tests/bundles/focal-victoria.yaml index a7694b1..ad2eac2 100644 --- a/src/tests/bundles/focal-victoria.yaml +++ b/src/tests/bundles/focal-victoria.yaml @@ -74,7 +74,7 @@ applications: memcached: charm: cs:~memcached-team/memcached num_units: 1 - # holding charm at bionic as it isn't available for focal yet + # holding charm at bionic as it isn't available for focal+ yet series: bionic to: - '7' @@ -88,6 +88,9 @@ applications: to: - '8' + nrpe: + charm: cs:nrpe + relations: - - 'keystone:shared-db' - 'keystone-mysql-router:shared-db' @@ -124,3 +127,6 @@ relations: - - 'neutron-api:amqp' - 'rabbitmq-server:amqp' + + - - 'designate:nrpe-external-master' + - 'nrpe:nrpe-external-master' diff --git a/src/tests/bundles/focal-wallaby.yaml b/src/tests/bundles/focal-wallaby.yaml index 4ab3939..4a3d95a 100644 --- a/src/tests/bundles/focal-wallaby.yaml +++ b/src/tests/bundles/focal-wallaby.yaml @@ -74,7 +74,7 @@ applications: memcached: charm: cs:~memcached-team/memcached num_units: 1 - # holding charm at bionic as it isn't available for focal yet + # holding charm at bionic as it isn't available for focal+ yet series: bionic to: - '7' @@ -88,6 +88,9 @@ applications: to: - '8' + nrpe: + charm: cs:nrpe + relations: - - 'keystone:shared-db' - 'keystone-mysql-router:shared-db' @@ -124,3 +127,6 @@ relations: - - 'neutron-api:amqp' - 'rabbitmq-server:amqp' + + - - 'designate:nrpe-external-master' + - 'nrpe:nrpe-external-master' diff --git a/src/tests/bundles/groovy-victoria.yaml b/src/tests/bundles/groovy-victoria.yaml index 3ce2e07..15dc28c 100644 --- a/src/tests/bundles/groovy-victoria.yaml +++ b/src/tests/bundles/groovy-victoria.yaml @@ -74,7 +74,7 @@ applications: memcached: charm: cs:~memcached-team/memcached num_units: 1 - # holding charm at bionic as it isn't available for focal/groovy yet + # holding charm at bionic as it isn't available for focal+ yet series: bionic to: - '7' @@ -88,6 +88,9 @@ applications: to: - '8' + nrpe: + charm: cs:nrpe + relations: - - 'keystone:shared-db' - 'keystone-mysql-router:shared-db' @@ -124,3 +127,6 @@ relations: - - 'neutron-api:amqp' - 'rabbitmq-server:amqp' + + - - 'designate:nrpe-external-master' + - 'nrpe:nrpe-external-master' diff --git a/src/tests/bundles/hirsute-wallaby.yaml b/src/tests/bundles/hirsute-wallaby.yaml index e90546b..c51ccb1 100644 --- a/src/tests/bundles/hirsute-wallaby.yaml +++ b/src/tests/bundles/hirsute-wallaby.yaml @@ -74,7 +74,7 @@ applications: memcached: charm: cs:~memcached-team/memcached num_units: 1 - # holding charm at bionic as it isn't available for focal/groovy yet + # holding charm at bionic as it isn't available for focal+ yet series: bionic to: - '7' @@ -88,6 +88,9 @@ applications: to: - '8' + nrpe: + charm: cs:nrpe + relations: - - 'keystone:shared-db' - 'keystone-mysql-router:shared-db' @@ -124,3 +127,6 @@ relations: - - 'neutron-api:amqp' - 'rabbitmq-server:amqp' + + - - 'designate:nrpe-external-master' + - 'nrpe:nrpe-external-master' diff --git a/src/tests/bundles/xenial-mitaka.yaml b/src/tests/bundles/xenial-mitaka.yaml index 506941f..60439a8 100644 --- a/src/tests/bundles/xenial-mitaka.yaml +++ b/src/tests/bundles/xenial-mitaka.yaml @@ -48,6 +48,8 @@ applications: nameservers: 'ns1.amuletexample.com. ns2.amuletexample.com.' to: - '5' + nrpe: + charm: cs:nrpe relations: - - 'designate:shared-db' @@ -69,4 +71,6 @@ relations: - - 'neutron-api:shared-db' - 'percona-cluster:shared-db' - - 'neutron-api:amqp' - - 'rabbitmq-server:amqp' \ No newline at end of file + - 'rabbitmq-server:amqp' +- - 'designate:nrpe-external-master' + - 'nrpe:nrpe-external-master' diff --git a/src/tests/bundles/xenial-ocata.yaml b/src/tests/bundles/xenial-ocata.yaml index 83c0b37..d9469e2 100644 --- a/src/tests/bundles/xenial-ocata.yaml +++ b/src/tests/bundles/xenial-ocata.yaml @@ -53,6 +53,8 @@ applications: openstack-origin: cloud:xenial-ocata to: - '5' + nrpe: + charm: cs:nrpe relations: - - 'designate:shared-db' @@ -74,4 +76,6 @@ relations: - - 'neutron-api:shared-db' - 'percona-cluster:shared-db' - - 'neutron-api:amqp' - - 'rabbitmq-server:amqp' \ No newline at end of file + - 'rabbitmq-server:amqp' +- - 'designate:nrpe-external-master' + - 'nrpe:nrpe-external-master' diff --git a/src/tests/bundles/xenial-pike.yaml b/src/tests/bundles/xenial-pike.yaml index 96650ca..8480a89 100644 --- a/src/tests/bundles/xenial-pike.yaml +++ b/src/tests/bundles/xenial-pike.yaml @@ -53,6 +53,8 @@ applications: openstack-origin: cloud:xenial-pike to: - '5' + nrpe: + charm: cs:nrpe relations: - - 'designate:shared-db' @@ -74,4 +76,6 @@ relations: - - 'neutron-api:shared-db' - 'percona-cluster:shared-db' - - 'neutron-api:amqp' - - 'rabbitmq-server:amqp' \ No newline at end of file + - 'rabbitmq-server:amqp' +- - 'designate:nrpe-external-master' + - 'nrpe:nrpe-external-master' diff --git a/src/tests/bundles/xenial-queens.yaml b/src/tests/bundles/xenial-queens.yaml index f080e22..6136327 100644 --- a/src/tests/bundles/xenial-queens.yaml +++ b/src/tests/bundles/xenial-queens.yaml @@ -53,6 +53,8 @@ applications: openstack-origin: cloud:xenial-queens to: - '5' + nrpe: + charm: cs:nrpe relations: - - 'designate:shared-db' @@ -74,4 +76,6 @@ relations: - - 'neutron-api:shared-db' - 'percona-cluster:shared-db' - - 'neutron-api:amqp' - - 'rabbitmq-server:amqp' \ No newline at end of file + - 'rabbitmq-server:amqp' +- - 'designate:nrpe-external-master' + - 'nrpe:nrpe-external-master' diff --git a/src/tests/tests.yaml b/src/tests/tests.yaml index 924ec5a..8311d4a 100644 --- a/src/tests/tests.yaml +++ b/src/tests/tests.yaml @@ -25,6 +25,14 @@ dev_bundles: tests: - zaza.openstack.charm_tests.designate.tests.DesignateTests +target_deploy_status: + nrpe: + workload-status: blocked + workload-status-message: "Nagios server not configured or related" + tests_options: force_deploy: + # NOTE(lourot): this is needed because the NRPE charm isn't available on + # non-LTS Ubuntu series. See lp:1933643 + - groovy-victoria - hirsute-wallaby diff --git a/unit_tests/__init__.py b/unit_tests/__init__.py index ea4c403..4417b53 100644 --- a/unit_tests/__init__.py +++ b/unit_tests/__init__.py @@ -23,6 +23,7 @@ import charms_openstack.test_mocks # noqa charms_openstack.test_mocks.mock_charmhelpers() sys.modules['charmhelpers.core.decorators'] = ( charms_openstack.test_mocks.charmhelpers.core.decorators) +sys.modules['charmhelpers.contrib.charmsupport.nrpe'] = mock.MagicMock() def _fake_retry(num_retries, base_delay=0, exc_type=Exception): diff --git a/unit_tests/test_designate_handlers.py b/unit_tests/test_designate_handlers.py index 4fd4299..27535be 100644 --- a/unit_tests/test_designate_handlers.py +++ b/unit_tests/test_designate_handlers.py @@ -40,6 +40,7 @@ class TestRegisteredHooks(test_utils.TestRegisteredHooks): 'remote_pools_updated': ( 'leadership.changed.pool-yaml-hash', ), 'reset_shared_db': ('shared-db.setup', ), + 'configure_nrpe': ('base-config.rendered', ), }, 'when_not': { 'setup_amqp_req': ('amqp.requested-access', ), @@ -52,10 +53,17 @@ class TestRegisteredHooks(test_utils.TestRegisteredHooks): 'run_assess_status_on_every_hook': ( 'dont-set-assess-status', ), 'reset_shared_db': ('shared-db.connected', ), + 'configure_nrpe': ('is-update-status-hook', ), }, 'when_any': { 'set_dns_config_available': ( 'dns-slaves-config-valid', 'dns-backend.available', ), + 'configure_nrpe': ( + 'config.changed.nagios_context', + 'config.changed.nagios_servicegroups', + 'endpoint.nrpe-external-master.changed', + 'nrpe-external-master.available', + ), }, 'when_none': { 'clear_dns_config_available': ( diff --git a/unit_tests/test_lib_charm_openstack_designate.py b/unit_tests/test_lib_charm_openstack_designate.py index 7181ebe..e202e07 100644 --- a/unit_tests/test_lib_charm_openstack_designate.py +++ b/unit_tests/test_lib_charm_openstack_designate.py @@ -363,6 +363,18 @@ class TestDesignateCharm(Helper): pass self.assertFalse(self.leader_set.called) + def test_render_nrpe(self): + self.patch_object(designate.nrpe, 'add_init_service_checks') + charm_instance = designate.DesignateCharm(release='queens') + charm_instance.render_nrpe() + self.add_init_service_checks.assert_has_calls([ + mock.call().add_init_service_checks( + mock.ANY, + charm_instance.services, + mock.ANY + ), + ]) + class TestDesignateQueensCharm(Helper):