Merge lp:~paulgear/charm-helpers/nrpe-service-immediate-check into lp:charm-helpers

Proposed by Paul Gear
Status: Merged
Merged at revision: 700
Proposed branch: lp:~paulgear/charm-helpers/nrpe-service-immediate-check
Merge into: lp:charm-helpers
Diff against target: 71 lines (+21/-10)
2 files modified
charmhelpers/contrib/charmsupport/nrpe.py (+19/-10)
tests/contrib/charmsupport/test_nrpe.py (+2/-0)
To merge this branch: bzr merge lp:~paulgear/charm-helpers/nrpe-service-immediate-check
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
charmers Pending
Review via email: mp+300682@code.launchpad.net

Description of the change

Add an option for immediate execution of nrpe service checks. This is necessary because (for sysvinit-based services) the cron file installed only runs the check every 5 minutes.

If a nagios service check is created but the check does not fire before the end of a CI run which does nagios checks at the end, the nagios check will return an error and the CI run will fail.

This change works around that by allowing a charm to specify that a check should be run immediately, to ensure the check file is created.

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

Minor inline comments.

I think the default should be immediate_check=True. Charms need to explicitly call add_init_service_checks(), and any charm doing that for a bogus service that hangs on the status check is already broken. It is better to discover that now rather than later when there are a zillion hung /usr/local/lib/nagios/plugins/check_exit_status.pl processes. Or just add timeout(1) to the command line.

review: Abstain
613. By Paul Gear

Implement Stuart's recommendations

614. By Paul Gear

Merge devel trunk

Revision history for this message
Paul Gear (paulgear) wrote :

Updated with Stuart's recommendations.

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

======================================================================
ERROR: tests.contrib.charmsupport.test_nrpe.NRPEMiscTestCase.test_add_init_service_checks
----------------------------------------------------------------------
_StringException: Traceback (most recent call last):
  File "/home/stub/charms/charm-helpers/trunk/tests/contrib/charmsupport/test_nrpe.py", line 315, in test_add_init_service_checks
    nrpe.add_init_service_checks(bill, services, 'testunit')
  File "/home/stub/charms/charm-helpers/trunk/charmhelpers/contrib/charmsupport/nrpe.py", line 394, in add_init_service_checks
    os.chmod(checkpath, 0o644)
OSError: [Errno 2] No such file or directory: '/var/lib/nagios/service-check-haproxy.txt'

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

I've fixed the test

Revision history for this message
Ryan Beisner (1chb1n) wrote :

After syncing this into the Glance charm, we see failure:

2017-03-03 22:22:19 INFO worker.uniter.jujuc server.go:172 running hook tool "juju-log" ["Check command not found: check_status_file.py"]
2017-03-03 22:22:19 DEBUG worker.uniter.jujuc server.go:173 hook context id "glance/0-config-changed-2954441461654224842"; dir "/var/lib/juju/agents/unit-glance-0/charm"
2017-03-03 22:22:19 INFO juju-log Check command not found: check_status_file.py
2017-03-03 22:22:19 INFO config-changed Traceback (most recent call last):
2017-03-03 22:22:19 INFO config-changed File "/var/lib/juju/agents/unit-glance-0/charm/hooks/config-changed", line 632, in <module>
2017-03-03 22:22:19 INFO config-changed hooks.execute(sys.argv)
2017-03-03 22:22:19 INFO config-changed File "/var/lib/juju/agents/unit-glance-0/charm/hooks/charmhelpers/core/hookenv.py", line 731, in execute
2017-03-03 22:22:19 INFO config-changed self._hooks[hook_name]()
2017-03-03 22:22:19 INFO config-changed File "/var/lib/juju/agents/unit-glance-0/charm/hooks/charmhelpers/contrib/openstack/utils.py", line 1864, in wrapped_f
2017-03-03 22:22:19 INFO config-changed restart_functions)
2017-03-03 22:22:19 INFO config-changed File "/var/lib/juju/agents/unit-glance-0/charm/hooks/charmhelpers/core/host.py", line 655, in restart_on_change_helper
2017-03-03 22:22:19 INFO config-changed r = lambda_f()
2017-03-03 22:22:19 INFO config-changed File "/var/lib/juju/agents/unit-glance-0/charm/hooks/charmhelpers/contrib/openstack/utils.py", line 1863, in <lambda>
2017-03-03 22:22:19 INFO config-changed (lambda: f(*args, **kwargs)), restart_map, stopstart,
2017-03-03 22:22:19 INFO config-changed File "/var/lib/juju/agents/unit-glance-0/charm/hooks/charmhelpers/contrib/hardening/harden.py", line 79, in _harden_inner2
2017-03-03 22:22:19 INFO config-changed return f(*args, **kwargs)
2017-03-03 22:22:19 INFO config-changed File "/var/lib/juju/agents/unit-glance-0/charm/hooks/config-changed", line 405, in config_changed
2017-03-03 22:22:19 INFO config-changed update_nrpe_config()
2017-03-03 22:22:19 INFO config-changed File "/var/lib/juju/agents/unit-glance-0/charm/hooks/config-changed", line 593, in update_nrpe_config
2017-03-03 22:22:19 INFO config-changed nrpe.add_init_service_checks(nrpe_setup, services(), current_unit)
2017-03-03 22:22:19 INFO config-changed File "/var/lib/juju/agents/unit-glance-0/charm/hooks/charmhelpers/contrib/charmsupport/nrpe.py", line 387, in add_init_service_checks
2017-03-03 22:22:19 INFO config-changed f = open(checkpath, 'w')
2017-03-03 22:22:19 INFO config-changed IOError: [Errno 2] No such file or directory: '/var/lib/nagios/service-check-haproxy.txt'

Revision history for this message
Ryan Beisner (1chb1n) wrote :

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 2016-09-06 06:25:17 +0000
3+++ charmhelpers/contrib/charmsupport/nrpe.py 2017-03-02 07:53:57 +0000
4@@ -338,13 +338,14 @@
5 return unit
6
7
8-def add_init_service_checks(nrpe, services, unit_name):
9+def add_init_service_checks(nrpe, services, unit_name, immediate_check=True):
10 """
11 Add checks for each service in list
12
13 :param NRPE nrpe: NRPE object to add check to
14 :param list services: List of services to check
15 :param str unit_name: Unit name to use in check description
16+ :param bool immediate_check: For sysv init, run the service check immediately
17 """
18 for svc in services:
19 # Don't add a check for these services from neutron-gateway
20@@ -368,21 +369,29 @@
21 )
22 elif os.path.exists(sysv_init):
23 cronpath = '/etc/cron.d/nagios-service-check-%s' % svc
24- cron_file = ('*/5 * * * * root '
25- '/usr/local/lib/nagios/plugins/check_exit_status.pl '
26- '-s /etc/init.d/%s status > '
27- '/var/lib/nagios/service-check-%s.txt\n' % (svc,
28- svc)
29- )
30+ checkpath = '/var/lib/nagios/service-check-%s.txt' % svc
31+ croncmd = (
32+ '/usr/local/lib/nagios/plugins/check_exit_status.pl '
33+ '-s /etc/init.d/%s status' % svc
34+ )
35+ cron_file = '*/5 * * * * root %s > %s\n' % (croncmd, checkpath)
36 f = open(cronpath, 'w')
37 f.write(cron_file)
38 f.close()
39 nrpe.add_check(
40 shortname=svc,
41- description='process check {%s}' % unit_name,
42- check_cmd='check_status_file.py -f '
43- '/var/lib/nagios/service-check-%s.txt' % svc,
44+ description='service check {%s}' % unit_name,
45+ check_cmd='check_status_file.py -f %s' % checkpath,
46 )
47+ if immediate_check:
48+ f = open(checkpath, 'w')
49+ subprocess.call(
50+ croncmd.split(),
51+ stdout=f,
52+ stderr=subprocess.STDOUT
53+ )
54+ f.close()
55+ os.chmod(checkpath, 0o644)
56
57
58 def copy_nrpe_checks():
59
60=== modified file 'tests/contrib/charmsupport/test_nrpe.py'
61--- tests/contrib/charmsupport/test_nrpe.py 2016-09-06 06:25:17 +0000
62+++ tests/contrib/charmsupport/test_nrpe.py 2017-03-02 07:53:57 +0000
63@@ -300,6 +300,8 @@
64 '/usr/lib/nagios/plugins/check_upstart_job',
65 '/etc/init.d/haproxy',
66 '/usr/lib/nagios/plugins/check_status_file.py',
67+ '/etc/cron.d/nagios-service-check-haproxy',
68+ '/var/lib/nagios/service-check-haproxy.txt',
69 '/usr/lib/nagios/plugins/check_systemd.py'
70 ]
71 return init_file in files

Subscribers

People subscribed via source and target branches