Merge lp:~thedac/charm-helpers/set-primary-charm-on-nrpe-relation into lp:charm-helpers

Proposed by David Ames
Status: Needs review
Proposed branch: lp:~thedac/charm-helpers/set-primary-charm-on-nrpe-relation
Merge into: lp:charm-helpers
Diff against target: 66 lines (+18/-4)
2 files modified
charmhelpers/contrib/charmsupport/nrpe.py (+15/-1)
tests/contrib/charmsupport/test_nrpe.py (+3/-3)
To merge this branch: bzr merge lp:~thedac/charm-helpers/set-primary-charm-on-nrpe-relation
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Disapprove
Review via email: mp+256057@code.launchpad.net

Description of the change

In IS we have scenarios where a unit may have its primary charm and multiple subordinate charms related to nrpe{,-external-master}.

The nrpe-external-master and nrpe charms determine the hostname to use by the charm service name when using nagios_hostname_type unit. This process is non-deterministic and the ordering of which charm is related to nrpe is significant.

This leads to situations where the hostname generated uses the subordinate charm's service name rather than the primary charm.

The only way to guarantee that the primary charm is used, is to set the information on the relation in the charm and then test for it in the nrpe-external-master charm.

This MP is one half of the solution. Primary charms will default to True and subordinate charms can set primary=False.

Explicitly set when a charm is the primary charm (vs a subordinate charm) in the npre-external-master relation.
For use when multiple charms primary and subordinate are related to nrpe{,-external-master} on the same unit.
The default is set to True as this is the case 99% of the time.
Only subordinate charms which are related to nrpe{,-external-master} need set primary=False on the relation.

To post a comment you must log in.
Revision history for this message
Stuart Bishop (stub) wrote :

The code looks good. The test changes are more dubious - you have added a relation_set call, but the new code path is not being tested. relation_set is already mocked in the test harness, so something like the following:

    def test_init_primary(self):
        from unittest.mock import sentinel
        checker = nrpe.NRPE(primary=sentinel.primary)
        self.assertEqual(checker.primary, sentinel.primary)
        self.patched['relation_set'].assert_called_once_with(relation_settings={'primary': sentinel.primary})

Revision history for this message
Stuart Bishop (stub) wrote :

No longer needed with the new hookenv.principal_unit() call and modern Juju

review: Disapprove

Unmerged revisions

351. By David Ames

Document the primary setting on NRPE

350. By David Ames

Explicitly set when a charm is the primary charm (vs a subordinate charm) in the npre-external-master relation.
For use when multiple charms primary and subordinate are related to nrpe{,-external-master} on the same unit.
The default is set to True as this is the case 99% of the time.
Only subordinate charms which are related to nrpe{,-external-master} need set primary=False on the relation.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/contrib/charmsupport/nrpe.py'
2--- charmhelpers/contrib/charmsupport/nrpe.py 2015-02-19 07:49:41 +0000
3+++ charmhelpers/contrib/charmsupport/nrpe.py 2015-04-13 23:10:18 +0000
4@@ -110,6 +110,13 @@
5 # def local_monitors_relation_changed():
6 # update_nrpe_config()
7 #
8+# 4.a If your charm is a subordinate charm set primary=False
9+#
10+# from charmsupport.nrpe import NRPE
11+# (...)
12+# def update_nrpe_config():
13+# nrpe_compat = NRPE(primary=False)
14+#
15 # 5. ln -s hooks.py nrpe-external-master-relation-changed
16 # ln -s hooks.py local-monitors-relation-changed
17
18@@ -206,9 +213,10 @@
19 nagios_exportdir = '/var/lib/nagios/export'
20 nrpe_confdir = '/etc/nagios/nrpe.d'
21
22- def __init__(self, hostname=None):
23+ def __init__(self, hostname=None, primary=True):
24 super(NRPE, self).__init__()
25 self.config = config()
26+ self.primary = primary
27 self.nagios_context = self.config['nagios_context']
28 if 'nagios_servicegroups' in self.config and self.config['nagios_servicegroups']:
29 self.nagios_servicegroups = self.config['nagios_servicegroups']
30@@ -220,6 +228,12 @@
31 else:
32 self.hostname = "{}-{}".format(self.nagios_context, self.unit_name)
33 self.checks = []
34+ # Iff in an nrpe-external-master relation hook set primary status
35+ relation = relation_ids()
36+ if relation:
37+ if relation[0].startswith('nrpe-external-master'):
38+ log("Setting charm primary status {}".format(primary))
39+ relation_set(relation_settings={'primary': self.primary})
40
41 def add_check(self, *args, **kwargs):
42 self.checks.append(Check(*args, **kwargs))
43
44=== modified file 'tests/contrib/charmsupport/test_nrpe.py'
45--- tests/contrib/charmsupport/test_nrpe.py 2015-02-19 08:41:54 +0000
46+++ tests/contrib/charmsupport/test_nrpe.py 2015-04-13 23:10:18 +0000
47@@ -87,8 +87,8 @@
48 self.assertEqual(None, checker.write())
49
50 expected = 'Nagios user not set up, nrpe checks not updated'
51- self.patched['log'].assert_called_once_with(expected)
52- self.check_call_counts(log=1, config=1, getpwnam=1, getgrnam=1)
53+ self.patched['log'].assert_called_with(expected)
54+ self.check_call_counts(log=2, config=1, getpwnam=1, getgrnam=1)
55
56 def test_write_no_checker(self):
57 self.patched['config'].return_value = {'nagios_context': 'test',
58@@ -163,7 +163,7 @@
59 relation_id="local-monitors:1", monitors=monitors)
60 self.check_call_counts(config=1, getpwnam=1, getgrnam=1,
61 exists=3, open=2, listdir=1,
62- relation_ids=1, relation_set=1)
63+ relation_ids=2, relation_set=1)
64
65
66 class NRPECheckTestCase(NRPEBaseTestCase):

Subscribers

People subscribed via source and target branches