Merge lp:~sidnei/charmsupport/nrpe-notify-monitors into lp:~ubuntuone-pqm-team/charmsupport/trunk

Proposed by Sidnei da Silva
Status: Merged
Approved by: Sidnei da Silva
Approved revision: 40
Merged at revision: 40
Proposed branch: lp:~sidnei/charmsupport/nrpe-notify-monitors
Merge into: lp:~ubuntuone-pqm-team/charmsupport/trunk
Diff against target: 236 lines (+71/-27)
3 files modified
charmsupport/hookenv.py (+9/-0)
charmsupport/nrpe.py (+42/-17)
tests/test_nrpe.py (+20/-10)
To merge this branch: bzr merge lp:~sidnei/charmsupport/nrpe-notify-monitors
Reviewer Review Type Date Requested Status
Anthony Lenton (community) Approve
Review via email: mp+153172@code.launchpad.net

Commit message

Move notification of local-monitors to nrpe.write()

Description of the change

Move notification of local-monitors to nrpe.write()

To post a comment you must log in.
Revision history for this message
Anthony Lenton (elachuni) wrote :

Neat!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmsupport/hookenv.py'
2--- charmsupport/hookenv.py 2013-03-08 16:22:41 +0000
3+++ charmsupport/hookenv.py 2013-03-13 15:44:27 +0000
4@@ -104,6 +104,15 @@
5 return Serializable(config_data)
6
7
8+def relation_set(relation_id=None, **kwargs):
9+ relation_cmd_line = ['relation-set']
10+ if relation_id is not None:
11+ relation_cmd_line.extend(('-r', relation_id))
12+ for k, v in kwargs.items():
13+ relation_cmd_line.append('{}={}'.format(k, v))
14+ subprocess.check_call(relation_cmd_line)
15+
16+
17 def relation_ids(reltype=None):
18 "A list of relation_ids"
19 reltype = reltype or relation_type()
20
21=== modified file 'charmsupport/nrpe.py'
22--- charmsupport/nrpe.py 2013-03-12 19:22:11 +0000
23+++ charmsupport/nrpe.py 2013-03-13 15:44:27 +0000
24@@ -10,11 +10,13 @@
25 import os
26 import re
27 import shlex
28-
29-from hookenv import config, local_unit, log
30-
31-# This module adds compatibility with the nrpe_external_master
32-# subordinate charm. To use it in your charm:
33+import yaml
34+
35+from hookenv import config, local_unit, log, relation_ids, relation_set
36+from host import service
37+
38+# This module adds compatibility with the nrpe-external-master and plain nrpe
39+# subordinate charms. To use it in your charm:
40 #
41 # 1. Update metadata.yaml
42 #
43@@ -24,13 +26,22 @@
44 # interface: nrpe-external-master
45 # scope: container
46 #
47+# and/or
48+#
49+# provides:
50+# (...)
51+# local-monitors:
52+# interface: local-monitors
53+# scope: container
54+
55+#
56 # 2. Add the following to config.yaml
57 #
58 # nagios_context:
59 # default: "juju"
60 # type: string
61 # description: |
62-# Used by the nrpe-external-master subordinate charm.
63+# Used by the nrpe subordinate charms.
64 # A string that will be prepended to instance name to set the host name
65 # in nagios. So for instance the hostname would be something like:
66 # juju-myservice-0
67@@ -60,10 +71,15 @@
68 # def config_changed():
69 # (...)
70 # update_nrpe_config()
71+#
72 # def nrpe_external_master_relation_changed():
73 # update_nrpe_config()
74 #
75+# def local_monitors_relation_changed():
76+# update_nrpe_config()
77+#
78 # 5. ln -s hooks.py nrpe-external-master-relation-changed
79+# ln -s hooks.py local-monitors-relation-changed
80
81
82 class CheckException(Exception):
83@@ -81,7 +97,7 @@
84 host_name {nagios_hostname}
85 service_description {nagios_hostname}[{shortname}] """
86 """{description}
87- check_command check_nrpe!check_{shortname}
88+ check_command check_nrpe!{command}
89 servicegroups {nagios_servicegroup}
90 }}
91 """)
92@@ -93,6 +109,7 @@
93 raise CheckException("shortname must match {}".format(
94 Check.shortname_re))
95 self.shortname = shortname
96+ self.command = "check_{}".format(shortname)
97 # Note: a set of invalid characters is defined by the
98 # Nagios server config
99 # The default is: illegal_object_name_chars=`~!$%^&*"|'<>?,()=
100@@ -117,12 +134,12 @@
101 return ''
102
103 def write(self, nagios_context, hostname):
104- nrpe_check_file = '/etc/nagios/nrpe.d/check_{}.cfg'.format(
105- self.shortname)
106+ nrpe_check_file = '/etc/nagios/nrpe.d/{}.cfg'.format(
107+ self.command)
108 with open(nrpe_check_file, 'w') as nrpe_check_config:
109 nrpe_check_config.write("# check {}\n".format(self.shortname))
110- nrpe_check_config.write("command[check_{}]={}\n".format(
111- self.shortname, self.check_cmd))
112+ nrpe_check_config.write("command[{}]={}\n".format(
113+ self.command, self.check_cmd))
114
115 if not os.path.exists(NRPE.nagios_exportdir):
116 log('Not writing service config as {} is not accessible'.format(
117@@ -132,7 +149,7 @@
118
119 def write_service_config(self, nagios_context, hostname):
120 for f in os.listdir(NRPE.nagios_exportdir):
121- if re.search('.*check_{}.cfg'.format(self.shortname), f):
122+ if re.search('.*{}.cfg'.format(self.command), f):
123 os.remove(os.path.join(NRPE.nagios_exportdir, f))
124
125 templ_vars = {
126@@ -140,10 +157,11 @@
127 'nagios_servicegroup': nagios_context,
128 'description': self.description,
129 'shortname': self.shortname,
130+ 'command': self.command,
131 }
132 nrpe_service_text = Check.service_template.format(**templ_vars)
133- nrpe_service_file = '{}/service__{}_check_{}.cfg'.format(
134- NRPE.nagios_exportdir, hostname, self.shortname)
135+ nrpe_service_file = '{}/service__{}_{}.cfg'.format(
136+ NRPE.nagios_exportdir, hostname, self.command)
137 with open(nrpe_service_file, 'w') as nrpe_service_config:
138 nrpe_service_config.write(str(nrpe_service_text))
139
140@@ -179,8 +197,15 @@
141 os.mkdir(NRPE.nagios_logdir)
142 os.chown(NRPE.nagios_logdir, nagios_uid, nagios_gid)
143
144+ nrpe_monitors = {}
145+ monitors = {"monitors": {"remote": {"nrpe": nrpe_monitors}}}
146 for nrpecheck in self.checks:
147 nrpecheck.write(self.nagios_context, self.hostname)
148-
149- if os.path.isfile('/etc/init.d/nagios-nrpe-server'):
150- subprocess.call(['service', 'nagios-nrpe-server', 'reload'])
151+ nrpe_monitors[nrpecheck.shortname] = {
152+ "command": nrpecheck.command,
153+ }
154+
155+ service('nagios-nrpe-server', 'restart')
156+
157+ for rid in relation_ids("local-monitors"):
158+ relation_set(relation_id=rid, monitors=yaml.dump(monitors))
159
160=== modified file 'tests/test_nrpe.py'
161--- tests/test_nrpe.py 2013-03-12 20:22:13 +0000
162+++ tests/test_nrpe.py 2013-03-13 15:44:27 +0000
163@@ -1,5 +1,7 @@
164 import os
165+import yaml
166 import subprocess
167+
168 from testtools import TestCase
169 from mock import patch, call
170
171@@ -20,6 +22,8 @@
172 'open': {'object': nrpe, 'create': True},
173 'isfile': {'object': os.path},
174 'call': {'object': subprocess},
175+ 'relation_ids': {'object': nrpe},
176+ 'relation_set': {'object': nrpe},
177 }
178
179 def setUp(self):
180@@ -68,31 +72,29 @@
181 def test_write_no_checker(self):
182 self.patched['config'].return_value = {'nagios_context': 'test'}
183 self.patched['exists'].return_value = True
184- self.patched['isfile'].return_value = False
185 checker = nrpe.NRPE()
186
187 self.assertEqual(None, checker.write())
188
189- self.check_call_counts(config=1, getpwnam=1, getgrnam=1, isfile=1,
190- exists=1)
191+ self.check_call_counts(config=1, getpwnam=1, getgrnam=1, exists=2)
192
193 def test_write_restarts_service(self):
194 self.patched['config'].return_value = {'nagios_context': 'test'}
195 self.patched['exists'].return_value = True
196- self.patched['isfile'].return_value = True
197 checker = nrpe.NRPE()
198
199 self.assertEqual(None, checker.write())
200
201- expected = ['service', 'nagios-nrpe-server', 'reload']
202+ expected = ['initctl', 'nagios-nrpe-server', 'restart']
203 self.assertEqual(expected, self.patched['call'].call_args[0][0])
204- self.check_call_counts(config=1, getpwnam=1, getgrnam=1, isfile=1,
205- exists=1, call=1)
206+ self.check_call_counts(config=1, getpwnam=1, getgrnam=1,
207+ exists=2, call=1)
208
209 def test_update_nrpe(self):
210 self.patched['config'].return_value = {'nagios_context': 'a'}
211 self.patched['exists'].return_value = True
212- self.patched['isfile'].return_value = False
213+ self.patched['relation_ids'].return_value = ['local-monitors:1']
214+
215 checker = nrpe.NRPE()
216 checker.add_check(shortname="myservice",
217 description="Check MyService",
218@@ -128,8 +130,16 @@
219 ]
220 actual = [x[0][0] for x in outfile.write.call_args_list]
221 self.assertEqual(expected, actual)
222- self.check_call_counts(config=1, getpwnam=1, getgrnam=1, isfile=1,
223- exists=3, open=2, listdir=1)
224+
225+ nrpe_monitors = {'myservice':
226+ {'command': 'check_myservice'}}
227+ monitors = yaml.dump(
228+ {"monitors": {"remote": {"nrpe": nrpe_monitors}}})
229+ self.patched['relation_set'].assert_called_once_with(
230+ relation_id="local-monitors:1", monitors=monitors)
231+ self.check_call_counts(config=1, getpwnam=1, getgrnam=1,
232+ exists=4, open=2, listdir=1,
233+ relation_ids=1, relation_set=1)
234
235
236 class NRPECheckTestCase(NRPEBaseTestCase):

Subscribers

People subscribed via source and target branches