Merge lp:~gnuoy/charm-helpers/nrpe-service-functions into lp:charm-helpers

Proposed by Liam Young
Status: Merged
Merged at revision: 280
Proposed branch: lp:~gnuoy/charm-helpers/nrpe-service-functions
Merge into: lp:charm-helpers
Diff against target: 143 lines (+114/-0)
2 files modified
charmhelpers/contrib/charmsupport/nrpe.py (+62/-0)
tests/contrib/charmsupport/test_nrpe.py (+52/-0)
To merge this branch: bzr merge lp:~gnuoy/charm-helpers/nrpe-service-functions
Reviewer Review Type Date Requested Status
James Page Approve
Review via email: mp+246104@code.launchpad.net

Description of the change

Add functions to simplify setup of nrpe service checks

To post a comment you must log in.
Revision history for this message
James Page (james-page) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'charmhelpers/contrib/charmsupport/nrpe.py'
--- charmhelpers/contrib/charmsupport/nrpe.py 2014-11-26 14:09:26 +0000
+++ charmhelpers/contrib/charmsupport/nrpe.py 2015-01-12 10:12:45 +0000
@@ -18,6 +18,7 @@
18 log,18 log,
19 relation_ids,19 relation_ids,
20 relation_set,20 relation_set,
21 relations_of_type,
21)22)
2223
23from charmhelpers.core.host import service24from charmhelpers.core.host import service
@@ -233,3 +234,64 @@
233234
234 for rid in relation_ids("local-monitors"):235 for rid in relation_ids("local-monitors"):
235 relation_set(relation_id=rid, monitors=yaml.dump(monitors))236 relation_set(relation_id=rid, monitors=yaml.dump(monitors))
237
238
239def get_nagios_hostcontext(relation_name='nrpe-external-master'):
240 """
241 Query relation with nrpe subordinate, return the nagios_host_context
242
243 :param str relation_name: Name of relation nrpe sub joined to
244 """
245 for rel in relations_of_type(relation_name):
246 if 'nagios_hostname' in rel:
247 return rel['nagios_host_context']
248
249
250def get_nagios_unit_name(relation_name='nrpe-external-master'):
251 """
252 Return the nagios unit name prepended with host_context if needed
253
254 :param str relation_name: Name of relation nrpe sub joined to
255 """
256 host_context = get_nagios_hostcontext(relation_name)
257 if host_context:
258 unit = "%s:%s" % (host_context, local_unit())
259 else:
260 unit = local_unit()
261 return unit
262
263
264def add_init_service_checks(nrpe, services, unit_name):
265 """
266 Add checks for each service in list
267
268 :param NRPE nrpe: NRPE object to add check to
269 :param list services: List of services to check
270 :param str unit_name: Unit name to use in check description
271 """
272 for svc in services:
273 upstart_init = '/etc/init/%s.conf' % svc
274 sysv_init = '/etc/init.d/%s' % svc
275 if os.path.exists(upstart_init):
276 nrpe.add_check(
277 shortname=svc,
278 description='process check {%s}' % unit_name,
279 check_cmd='check_upstart_job %s' % svc
280 )
281 elif os.path.exists(sysv_init):
282 cronpath = '/etc/cron.d/nagios-service-check-%s' % svc
283 cron_file = ('*/5 * * * * root '
284 '/usr/local/lib/nagios/plugins/check_exit_status.pl '
285 '-s /etc/init.d/%s status > '
286 '/var/lib/nagios/service-check-%s.txt\n' % (svc,
287 svc)
288 )
289 f = open(cronpath, 'w')
290 f.write(cron_file)
291 f.close()
292 nrpe.add_check(
293 shortname=svc,
294 description='process check {%s}' % unit_name,
295 check_cmd='check_status_file.py -f '
296 '/var/lib/nagios/service-check-%s.txt' % svc,
297 )
236298
=== modified file 'tests/contrib/charmsupport/test_nrpe.py'
--- tests/contrib/charmsupport/test_nrpe.py 2014-11-26 14:18:27 +0000
+++ tests/contrib/charmsupport/test_nrpe.py 2015-01-12 10:12:45 +0000
@@ -24,6 +24,7 @@
24 'call': {'object': subprocess},24 'call': {'object': subprocess},
25 'relation_ids': {'object': nrpe},25 'relation_ids': {'object': nrpe},
26 'relation_set': {'object': nrpe},26 'relation_set': {'object': nrpe},
27 'relations_of_type': {'object': nrpe},
27 }28 }
2829
29 def setUp(self):30 def setUp(self):
@@ -241,3 +242,54 @@
241242
242 self.check_call_counts(exists=1, call=1)243 self.check_call_counts(exists=1, call=1)
243 self.assertEqual(command, self.patched['call'].call_args[0][0])244 self.assertEqual(command, self.patched['call'].call_args[0][0])
245
246
247class NRPEMiscTestCase(NRPEBaseTestCase):
248 def test_get_nagios_hostcontext(self):
249 rel_info = {
250 'nagios_hostname': 'bob-openstack-dashboard-0',
251 'private-address': '10.5.3.103',
252 '__unit__': u'dashboard-nrpe/1',
253 '__relid__': u'nrpe-external-master:2',
254 'nagios_host_context': u'bob',
255 }
256 self.patched['relations_of_type'].return_value = [rel_info]
257 self.assertEqual(nrpe.get_nagios_hostcontext(), 'bob')
258
259 def test_get_nagios_unit_name(self):
260 rel_info = {
261 'nagios_hostname': 'bob-openstack-dashboard-0',
262 'private-address': '10.5.3.103',
263 '__unit__': u'dashboard-nrpe/1',
264 '__relid__': u'nrpe-external-master:2',
265 'nagios_host_context': u'bob',
266 }
267 self.patched['relations_of_type'].return_value = [rel_info]
268 self.assertEqual(nrpe.get_nagios_unit_name(), 'bob:testunit')
269
270 def test_get_nagios_unit_name_no_hc(self):
271 self.patched['relations_of_type'].return_value = []
272 self.assertEqual(nrpe.get_nagios_unit_name(), 'testunit')
273
274 def test_add_init_service_checks(self):
275 def _exists(init_file):
276 files = ['/etc/init/apache2.conf',
277 '/usr/lib/nagios/plugins/check_upstart_job',
278 '/etc/init.d/haproxy',
279 '/usr/lib/nagios/plugins/check_status_file.py',
280 ]
281 return init_file in files
282
283 self.patched['exists'].side_effect = _exists
284 bill = nrpe.NRPE()
285 services = ['apache2', 'haproxy']
286 nrpe.add_init_service_checks(bill, services, 'testunit')
287 expect_cmds = {
288 'apache2': '/usr/lib/nagios/plugins/check_upstart_job apache2',
289 'haproxy': '/usr/lib/nagios/plugins/check_status_file.py -f '
290 '/var/lib/nagios/service-check-haproxy.txt',
291 }
292 self.assertEqual(bill.checks[0].shortname, 'apache2')
293 self.assertEqual(bill.checks[0].check_cmd, expect_cmds['apache2'])
294 self.assertEqual(bill.checks[1].shortname, 'haproxy')
295 self.assertEqual(bill.checks[1].check_cmd, expect_cmds['haproxy'])

Subscribers

People subscribed via source and target branches