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
1=== modified file 'charmhelpers/contrib/charmsupport/nrpe.py'
2--- charmhelpers/contrib/charmsupport/nrpe.py 2014-11-26 14:09:26 +0000
3+++ charmhelpers/contrib/charmsupport/nrpe.py 2015-01-12 10:12:45 +0000
4@@ -18,6 +18,7 @@
5 log,
6 relation_ids,
7 relation_set,
8+ relations_of_type,
9 )
10
11 from charmhelpers.core.host import service
12@@ -233,3 +234,64 @@
13
14 for rid in relation_ids("local-monitors"):
15 relation_set(relation_id=rid, monitors=yaml.dump(monitors))
16+
17+
18+def get_nagios_hostcontext(relation_name='nrpe-external-master'):
19+ """
20+ Query relation with nrpe subordinate, return the nagios_host_context
21+
22+ :param str relation_name: Name of relation nrpe sub joined to
23+ """
24+ for rel in relations_of_type(relation_name):
25+ if 'nagios_hostname' in rel:
26+ return rel['nagios_host_context']
27+
28+
29+def get_nagios_unit_name(relation_name='nrpe-external-master'):
30+ """
31+ Return the nagios unit name prepended with host_context if needed
32+
33+ :param str relation_name: Name of relation nrpe sub joined to
34+ """
35+ host_context = get_nagios_hostcontext(relation_name)
36+ if host_context:
37+ unit = "%s:%s" % (host_context, local_unit())
38+ else:
39+ unit = local_unit()
40+ return unit
41+
42+
43+def add_init_service_checks(nrpe, services, unit_name):
44+ """
45+ Add checks for each service in list
46+
47+ :param NRPE nrpe: NRPE object to add check to
48+ :param list services: List of services to check
49+ :param str unit_name: Unit name to use in check description
50+ """
51+ for svc in services:
52+ upstart_init = '/etc/init/%s.conf' % svc
53+ sysv_init = '/etc/init.d/%s' % svc
54+ if os.path.exists(upstart_init):
55+ nrpe.add_check(
56+ shortname=svc,
57+ description='process check {%s}' % unit_name,
58+ check_cmd='check_upstart_job %s' % svc
59+ )
60+ elif os.path.exists(sysv_init):
61+ cronpath = '/etc/cron.d/nagios-service-check-%s' % svc
62+ cron_file = ('*/5 * * * * root '
63+ '/usr/local/lib/nagios/plugins/check_exit_status.pl '
64+ '-s /etc/init.d/%s status > '
65+ '/var/lib/nagios/service-check-%s.txt\n' % (svc,
66+ svc)
67+ )
68+ f = open(cronpath, 'w')
69+ f.write(cron_file)
70+ f.close()
71+ nrpe.add_check(
72+ shortname=svc,
73+ description='process check {%s}' % unit_name,
74+ check_cmd='check_status_file.py -f '
75+ '/var/lib/nagios/service-check-%s.txt' % svc,
76+ )
77
78=== modified file 'tests/contrib/charmsupport/test_nrpe.py'
79--- tests/contrib/charmsupport/test_nrpe.py 2014-11-26 14:18:27 +0000
80+++ tests/contrib/charmsupport/test_nrpe.py 2015-01-12 10:12:45 +0000
81@@ -24,6 +24,7 @@
82 'call': {'object': subprocess},
83 'relation_ids': {'object': nrpe},
84 'relation_set': {'object': nrpe},
85+ 'relations_of_type': {'object': nrpe},
86 }
87
88 def setUp(self):
89@@ -241,3 +242,54 @@
90
91 self.check_call_counts(exists=1, call=1)
92 self.assertEqual(command, self.patched['call'].call_args[0][0])
93+
94+
95+class NRPEMiscTestCase(NRPEBaseTestCase):
96+ def test_get_nagios_hostcontext(self):
97+ rel_info = {
98+ 'nagios_hostname': 'bob-openstack-dashboard-0',
99+ 'private-address': '10.5.3.103',
100+ '__unit__': u'dashboard-nrpe/1',
101+ '__relid__': u'nrpe-external-master:2',
102+ 'nagios_host_context': u'bob',
103+ }
104+ self.patched['relations_of_type'].return_value = [rel_info]
105+ self.assertEqual(nrpe.get_nagios_hostcontext(), 'bob')
106+
107+ def test_get_nagios_unit_name(self):
108+ rel_info = {
109+ 'nagios_hostname': 'bob-openstack-dashboard-0',
110+ 'private-address': '10.5.3.103',
111+ '__unit__': u'dashboard-nrpe/1',
112+ '__relid__': u'nrpe-external-master:2',
113+ 'nagios_host_context': u'bob',
114+ }
115+ self.patched['relations_of_type'].return_value = [rel_info]
116+ self.assertEqual(nrpe.get_nagios_unit_name(), 'bob:testunit')
117+
118+ def test_get_nagios_unit_name_no_hc(self):
119+ self.patched['relations_of_type'].return_value = []
120+ self.assertEqual(nrpe.get_nagios_unit_name(), 'testunit')
121+
122+ def test_add_init_service_checks(self):
123+ def _exists(init_file):
124+ files = ['/etc/init/apache2.conf',
125+ '/usr/lib/nagios/plugins/check_upstart_job',
126+ '/etc/init.d/haproxy',
127+ '/usr/lib/nagios/plugins/check_status_file.py',
128+ ]
129+ return init_file in files
130+
131+ self.patched['exists'].side_effect = _exists
132+ bill = nrpe.NRPE()
133+ services = ['apache2', 'haproxy']
134+ nrpe.add_init_service_checks(bill, services, 'testunit')
135+ expect_cmds = {
136+ 'apache2': '/usr/lib/nagios/plugins/check_upstart_job apache2',
137+ 'haproxy': '/usr/lib/nagios/plugins/check_status_file.py -f '
138+ '/var/lib/nagios/service-check-haproxy.txt',
139+ }
140+ self.assertEqual(bill.checks[0].shortname, 'apache2')
141+ self.assertEqual(bill.checks[0].check_cmd, expect_cmds['apache2'])
142+ self.assertEqual(bill.checks[1].shortname, 'haproxy')
143+ self.assertEqual(bill.checks[1].check_cmd, expect_cmds['haproxy'])

Subscribers

People subscribed via source and target branches