From 88def9dcea92b8f722ca18f9d92b1b17f4ef26f2 Mon Sep 17 00:00:00 2001 From: Gustavo Sanchez Date: Tue, 17 Aug 2021 20:13:05 -0400 Subject: [PATCH] Adds 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/624 Closes-Bug: #1925977 Change-Id: I2536e0b95d9b7568e8c20b6f6eedc2c92eca0603 --- src/config.yaml | 15 +++++++++++++++ src/layer.yaml | 1 + src/lib/charm/openstack/manila.py | 10 ++++++++++ src/metadata.yaml | 4 ++++ src/reactive/manila_handlers.py | 12 ++++++++++++ src/tests/bundles/bionic-queens.yaml | 6 +++++- src/tests/bundles/bionic-rocky.yaml | 7 ++++++- src/tests/bundles/bionic-stein.yaml | 4 ++++ src/tests/bundles/bionic-train.yaml | 7 ++++++- src/tests/bundles/bionic-ussuri.yaml | 5 +++++ src/tests/bundles/focal-ussuri.yaml | 6 ++++++ src/tests/bundles/focal-victoria.yaml | 6 ++++++ src/tests/bundles/groovy-victoria.yaml | 6 ++++++ src/tests/bundles/xenial-mitaka.yaml | 5 +++++ src/tests/bundles/xenial-ocata.yaml | 6 +++++- src/tests/bundles/xenial-pike.yaml | 7 ++++++- src/tests/bundles/xenial-queens.yaml | 6 +++++- src/tests/tests.yaml | 11 ++++++++++- unit_tests/__init__.py | 2 ++ unit_tests/test_lib_charm_openstack_manila.py | 19 +++++++++++++++++++ unit_tests/test_manila_handlers.py | 13 +++++++++++-- 21 files changed, 149 insertions(+), 9 deletions(-) diff --git a/src/config.yaml b/src/config.yaml index 492170b..ec00061 100644 --- a/src/config.yaml +++ b/src/config.yaml @@ -61,3 +61,18 @@ options: description: | The 'default_share_type' must match the configured default_share_type set up in manila using 'manila create-type'. + nagios_context: + type: string + default: "juju" + description: | + Used by the nrpe-external-master subordinate charm. A string that will + be prepended to instance names in order to set the hostname in nagios. + With the default value for example the resulting hostname will look + like 'juju-myservice-0'. If you are running multiple environments with + the same services this allows you to differentiate between them. + nagios_servicegroups: + type: string + default: "" + 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 0602a4f..e420222 100644 --- a/src/layer.yaml +++ b/src/layer.yaml @@ -5,6 +5,7 @@ includes: - interface:keystone - interface:neutron-plugin - interface:manila-plugin + - interface:nrpe-external-master repo: https://github.com/openstack/charm-manila options: basic: diff --git a/src/lib/charm/openstack/manila.py b/src/lib/charm/openstack/manila.py index 00edcc7..9da6035 100644 --- a/src/lib/charm/openstack/manila.py +++ b/src/lib/charm/openstack/manila.py @@ -22,6 +22,7 @@ import re import subprocess import charmhelpers.core.host as host +import charmhelpers.contrib.charmsupport.nrpe as nrpe import charms_openstack.charm import charms_openstack.adapters import charms_openstack.ip as os_ip @@ -468,6 +469,15 @@ class ManilaCharm(charms_openstack.charm.HAOpenStackCharm): host.service_reload('apache2', restart_on_failure=True) + def render_nrpe_checks(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 ManilaCharmRocky(ManilaCharm): diff --git a/src/metadata.yaml b/src/metadata.yaml index ae7ecfc..afb0127 100644 --- a/src/metadata.yaml +++ b/src/metadata.yaml @@ -33,3 +33,7 @@ requires: scope: container remote-manila-plugin: interface: manila-plugin +provides: + nrpe-external-master: + interface: nrpe-external-master + scope: container \ No newline at end of file diff --git a/src/reactive/manila_handlers.py b/src/reactive/manila_handlers.py index f94cf1f..668ff49 100644 --- a/src/reactive/manila_handlers.py +++ b/src/reactive/manila_handlers.py @@ -197,3 +197,15 @@ def cluster_connected(hacluster): with charms_openstack.charm.provide_charm_instance() as manila_charm: manila_charm.configure_ha_resources(hacluster) manila_charm.assess_status() + + +@charms.reactive.when_none('charm.paused', 'is-update-status-hook') +@charms.reactive.when('config.rendered') +@charms.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 charms_openstack.charm.provide_charm_instance() as manila_charm: + manila_charm.render_nrpe_checks() diff --git a/src/tests/bundles/bionic-queens.yaml b/src/tests/bundles/bionic-queens.yaml index 54c6567..61783f7 100644 --- a/src/tests/bundles/bionic-queens.yaml +++ b/src/tests/bundles/bionic-queens.yaml @@ -19,6 +19,8 @@ services: keystone: charm: cs:~openstack-charmers-next/keystone num_units: 1 + nrpe: + charm: cs:nrpe relations: - - manila:manila-plugin @@ -30,4 +32,6 @@ relations: - - keystone - percona-cluster - - manila - - percona-cluster \ No newline at end of file + - percona-cluster + - - nrpe:nrpe-external-master + - manila:nrpe-external-master diff --git a/src/tests/bundles/bionic-rocky.yaml b/src/tests/bundles/bionic-rocky.yaml index 21ea8b8..f1e038d 100644 --- a/src/tests/bundles/bionic-rocky.yaml +++ b/src/tests/bundles/bionic-rocky.yaml @@ -83,6 +83,9 @@ services: num_units: 1 options: openstack-origin: *source + nrpe: + charm: cs:nrpe + relations: - - ceph-mon - ceph-osd @@ -143,4 +146,6 @@ relations: - - 'nova-cloud-controller:image-service' - 'glance:image-service' - - 'nova-cloud-controller:quantum-network-service' - - 'neutron-gateway:quantum-network-service' \ No newline at end of file + - 'neutron-gateway:quantum-network-service' + - - 'nrpe:nrpe-external-master' + - 'manila:nrpe-external-master' diff --git a/src/tests/bundles/bionic-stein.yaml b/src/tests/bundles/bionic-stein.yaml index 73dca61..465f29d 100644 --- a/src/tests/bundles/bionic-stein.yaml +++ b/src/tests/bundles/bionic-stein.yaml @@ -83,6 +83,8 @@ services: num_units: 1 options: openstack-origin: *source + nrpe: + charm: cs:nrpe relations: - - ceph-mon @@ -145,3 +147,5 @@ relations: - 'glance:image-service' - - 'nova-cloud-controller:quantum-network-service' - 'neutron-gateway:quantum-network-service' + - - 'nrpe:nrpe-external-master' + - 'manila:nrpe-external-master' diff --git a/src/tests/bundles/bionic-train.yaml b/src/tests/bundles/bionic-train.yaml index cf57a26..32e001f 100644 --- a/src/tests/bundles/bionic-train.yaml +++ b/src/tests/bundles/bionic-train.yaml @@ -88,6 +88,9 @@ services: num_units: 3 options: openstack-origin: *source + nrpe: + charm: cs:nrpe + relations: - - ceph-mon - ceph-osd @@ -156,4 +159,6 @@ relations: - - 'placement:shared-db' - 'percona-cluster:shared-db' - - 'placement:identity-service' - - 'keystone:identity-service' \ No newline at end of file + - 'keystone:identity-service' + - - 'nrpe:nrpe-external-master' + - 'manila:nrpe-external-master' diff --git a/src/tests/bundles/bionic-ussuri.yaml b/src/tests/bundles/bionic-ussuri.yaml index e15dd3e..7af898c 100644 --- a/src/tests/bundles/bionic-ussuri.yaml +++ b/src/tests/bundles/bionic-ussuri.yaml @@ -90,6 +90,9 @@ services: num_units: 1 options: openstack-origin: *source + nrpe: + charm: cs:nrpe + relations: - - 'ceph-mon' - 'ceph-osd' @@ -159,3 +162,5 @@ relations: - 'percona-cluster:shared-db' - - 'placement:identity-service' - 'keystone:identity-service' + - - 'nrpe:nrpe-external-master' + - 'manila:nrpe-external-master' diff --git a/src/tests/bundles/focal-ussuri.yaml b/src/tests/bundles/focal-ussuri.yaml index 3aa67c9..afc9e33 100644 --- a/src/tests/bundles/focal-ussuri.yaml +++ b/src/tests/bundles/focal-ussuri.yaml @@ -195,6 +195,9 @@ services: to: - '23' + nrpe: + charm: cs:nrpe + relations: - - 'ceph-mon' - 'ceph-osd' @@ -311,3 +314,6 @@ relations: - - 'placement:identity-service' - 'keystone:identity-service' + + - - 'nrpe:nrpe-external-master' + - 'manila:nrpe-external-master' diff --git a/src/tests/bundles/focal-victoria.yaml b/src/tests/bundles/focal-victoria.yaml index fa1c580..1ccf9cd 100644 --- a/src/tests/bundles/focal-victoria.yaml +++ b/src/tests/bundles/focal-victoria.yaml @@ -195,6 +195,9 @@ services: to: - '23' + nrpe: + charm: cs:nrpe + relations: - - 'ceph-mon' - 'ceph-osd' @@ -311,3 +314,6 @@ relations: - - 'placement:identity-service' - 'keystone:identity-service' + + - - 'nrpe:nrpe-external-master' + - 'manila:nrpe-external-master' diff --git a/src/tests/bundles/groovy-victoria.yaml b/src/tests/bundles/groovy-victoria.yaml index a2c83df..a3bb2d1 100644 --- a/src/tests/bundles/groovy-victoria.yaml +++ b/src/tests/bundles/groovy-victoria.yaml @@ -195,6 +195,9 @@ services: to: - '23' + nrpe: + charm: cs:nrpe + relations: - - 'ceph-mon' - 'ceph-osd' @@ -311,3 +314,6 @@ relations: - - 'placement:identity-service' - 'keystone:identity-service' + + - - 'nrpe:nrpe-external-master' + - 'manila:nrpe-external-master' diff --git a/src/tests/bundles/xenial-mitaka.yaml b/src/tests/bundles/xenial-mitaka.yaml index 01c9b3c..399df81 100644 --- a/src/tests/bundles/xenial-mitaka.yaml +++ b/src/tests/bundles/xenial-mitaka.yaml @@ -28,6 +28,8 @@ services: num_units: 1 options: openstack-origin: *source + nrpe: + charm: cs:nrpe relations: - - manila:manila-plugin @@ -40,3 +42,6 @@ relations: - percona-cluster - - manila - percona-cluster + - - nrpe:nrpe-external-master + - manila:nrpe-external-master + diff --git a/src/tests/bundles/xenial-ocata.yaml b/src/tests/bundles/xenial-ocata.yaml index 2a05a66..3cd9457 100644 --- a/src/tests/bundles/xenial-ocata.yaml +++ b/src/tests/bundles/xenial-ocata.yaml @@ -28,6 +28,8 @@ services: num_units: 1 options: openstack-origin: *source + nrpe: + charm: cs:nrpe relations: - - manila:manila-plugin @@ -39,4 +41,6 @@ relations: - - keystone - percona-cluster - - manila - - percona-cluster \ No newline at end of file + - percona-cluster + - - nrpe:nrpe-external-master + - manila:nrpe-external-master diff --git a/src/tests/bundles/xenial-pike.yaml b/src/tests/bundles/xenial-pike.yaml index e8925d6..791e7d1 100644 --- a/src/tests/bundles/xenial-pike.yaml +++ b/src/tests/bundles/xenial-pike.yaml @@ -28,6 +28,9 @@ services: num_units: 1 options: openstack-origin: *source + nrpe: + charm: cs:nrpe + relations: - - manila:manila-plugin - manila-generic @@ -38,4 +41,6 @@ relations: - - keystone - percona-cluster - - manila - - percona-cluster \ No newline at end of file + - percona-cluster + - - nrpe:nrpe-external-master + - manila:nrpe-external-master diff --git a/src/tests/bundles/xenial-queens.yaml b/src/tests/bundles/xenial-queens.yaml index 714a1ea..797dae4 100644 --- a/src/tests/bundles/xenial-queens.yaml +++ b/src/tests/bundles/xenial-queens.yaml @@ -28,6 +28,8 @@ services: num_units: 1 options: openstack-origin: *source + nrpe: + charm: cs:nrpe relations: - - manila:manila-plugin @@ -39,4 +41,6 @@ relations: - - keystone - percona-cluster - - manila - - percona-cluster \ No newline at end of file + - percona-cluster + - - nrpe:nrpe-external-master + - manila:nrpe-external-master diff --git a/src/tests/tests.yaml b/src/tests/tests.yaml index 263317a..e97f8b6 100644 --- a/src/tests/tests.yaml +++ b/src/tests/tests.yaml @@ -18,10 +18,14 @@ dev_bundles: - xenial-queens smoke_bundles: - ganesha: bionic-stein -target_deploy_status: {} +target_deploy_status: + nrpe: + workload-status: blocked + workload-status-message: "Nagios server not configured or related" tests: - zaza.openstack.charm_tests.manila.tests.ManilaTests - ganesha: + - zaza.openstack.charm_tests.manila.tests.ManilaTests - zaza.openstack.charm_tests.manila_ganesha.tests.ManilaGaneshaTests configure: - zaza.openstack.charm_tests.keystone.setup.add_demo_user @@ -32,6 +36,11 @@ configure: - zaza.openstack.charm_tests.nova.setup.manage_ssh_key - zaza.openstack.charm_tests.keystone.setup.add_demo_user - zaza.openstack.charm_tests.manila_ganesha.setup.setup_ganesha_share_type +configure_options: + configure_gateway_ext_port_use_juju_wait: False 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 3a5e9a3..566ae71 100644 --- a/unit_tests/__init__.py +++ b/unit_tests/__init__.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import mock import sys sys.path.append('src') @@ -20,3 +21,4 @@ sys.path.append('src/lib') # Mock out charmhelpers so that we can test without it. import charms_openstack.test_mocks # noqa charms_openstack.test_mocks.mock_charmhelpers() +sys.modules['charmhelpers.contrib.charmsupport.nrpe'] = mock.MagicMock() diff --git a/unit_tests/test_lib_charm_openstack_manila.py b/unit_tests/test_lib_charm_openstack_manila.py index 073a798..3e06cbc 100644 --- a/unit_tests/test_lib_charm_openstack_manila.py +++ b/unit_tests/test_lib_charm_openstack_manila.py @@ -253,3 +253,22 @@ class TestManilaCharm(Helper): self.assertEqual(c.config_lines_for('conf'), ["conf-string", '']) self.assertEqual(c.config_lines_for('conf2'), ["conf2-string", '']) self.assertEqual(c.config_lines_for('conf3'), ["conf3-string", '']) + + def test_render_nrpe_checks(self): + """Test NRPE renders correctly""" + self.patch_object(manila.nrpe, 'NRPE') + self.patch_object(manila.nrpe, 'add_init_service_checks') + + target = manila.ManilaCharm() + target.render_nrpe_checks() + + self.add_init_service_checks.assert_has_calls([ + mock.call().add_init_service_checks( + mock.ANY, + target.services, + mock.ANY + ), + ]) + self.NRPE.assert_has_calls([ + mock.call().write(), + ]) diff --git a/unit_tests/test_manila_handlers.py b/unit_tests/test_manila_handlers.py index 5193563..ca42f95 100644 --- a/unit_tests/test_manila_handlers.py +++ b/unit_tests/test_manila_handlers.py @@ -42,7 +42,8 @@ class TestRegisteredHooks(test_utils.TestRegisteredHooks): 'identity-service.available', 'amqp.available', ), 'config_rendered': ('db.synced', 'manila.config.rendered',), - 'cluster_connected': ('ha.connected',) + 'cluster_connected': ('ha.connected',), + 'configure_nrpe': ('config.rendered',) }, 'when_not': { 'register_endpoints': ('identity-service.available', ), @@ -55,8 +56,16 @@ class TestRegisteredHooks(test_utils.TestRegisteredHooks): 'remote-manila-plugin.changed', ), 'share_to_manila_plugins_auth': ( 'manila-plugin.connected', - 'remote-manila-plugin.connected', ) + 'remote-manila-plugin.connected', ), + 'configure_nrpe': ( + 'config.changed.nagios_context', + 'config.changed.nagios_servicegroups', + 'endpoint.nrpe-external-master.changed', + 'nrpe-external-master.available', ) }, + 'when_none': { + 'configure_nrpe': ('charm.paused', 'is-update-status-hook', ) + } } # test that the hooks were registered via the # reactive.barbican_handlers