Merge lp:~freyes/charm-helpers/lp1670223 into lp:charm-helpers

Proposed by Felipe Reyes
Status: Merged
Merged at revision: 711
Proposed branch: lp:~freyes/charm-helpers/lp1670223
Merge into: lp:charm-helpers
Diff against target: 90 lines (+25/-5)
2 files modified
charmhelpers/contrib/charmsupport/nrpe.py (+5/-2)
tests/contrib/charmsupport/test_nrpe.py (+20/-3)
To merge this branch: bzr merge lp:~freyes/charm-helpers/lp1670223
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
Ryan Beisner (community) Needs Information
Paul Gear (community) Needs Information
Review via email: mp+319207@code.launchpad.net

Description of the change

Verify if nagios user exists before attempting to run a check

There are cases where a charm may generate its configuration before
nagios-nrpe-server has been installed and attempting to run a check will
cause a failure due that /var/lib/nagios does not exist.

To post a comment you must log in.
Revision history for this message
Paul Gear (paulgear) wrote :

@freyes, is there a reason you're gating this on the user existing rather than the directory existing? It seems to me that just ensuring the directory exists regardless of nagios-nrpe-server's install state would be a more direct solution (assuming that's the only dependency between add_init_service_checks() and the nrpe package).

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

Agree @paulgear, was about to comment similarly. ie. Check that the dir exists before attempting to open a file for write within the dir.

Revision history for this message
Ryan Beisner (1chb1n) :
review: Needs Information
Revision history for this message
Felipe Reyes (freyes) wrote :

I chose to check the nagios use over the directory only because I was worried about the permissions, but now you mentioned, the cronjob runs as root just like the hook, so checking os.path.isdir('/var/lib/nagios') will do the trick as well and it's more explicit what it's needed.

I'll do the change.

lp:~freyes/charm-helpers/lp1670223 updated
710. By Felipe Reyes

Check if /var/lib/nagios exists

Instead of checking if the nagios user exists, this patch checks if
/var/lib/nagios directory is available, it makes the check more appropriate
for what it's needed to run the check

711. By Felipe Reyes

Updated comments to reflect the new logic of the check

Revision history for this message
Felipe Reyes (freyes) wrote :

done, ready for review

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

Looks good.

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 2017-03-02 07:53:12 +0000
3+++ charmhelpers/contrib/charmsupport/nrpe.py 2017-03-08 12:44:27 +0000
4@@ -227,6 +227,7 @@
5 nagios_logdir = '/var/log/nagios'
6 nagios_exportdir = '/var/lib/nagios/export'
7 nrpe_confdir = '/etc/nagios/nrpe.d'
8+ homedir = '/var/lib/nagios' # home dir provided by nagios-nrpe-server
9
10 def __init__(self, hostname=None, primary=True):
11 super(NRPE, self).__init__()
12@@ -369,7 +370,7 @@
13 )
14 elif os.path.exists(sysv_init):
15 cronpath = '/etc/cron.d/nagios-service-check-%s' % svc
16- checkpath = '/var/lib/nagios/service-check-%s.txt' % svc
17+ checkpath = '%s/service-check-%s.txt' % (nrpe.homedir, svc)
18 croncmd = (
19 '/usr/local/lib/nagios/plugins/check_exit_status.pl '
20 '-s /etc/init.d/%s status' % svc
21@@ -383,7 +384,9 @@
22 description='service check {%s}' % unit_name,
23 check_cmd='check_status_file.py -f %s' % checkpath,
24 )
25- if immediate_check:
26+ # if /var/lib/nagios doesn't exist open(checkpath, 'w') will fail
27+ # (LP: #1670223).
28+ if immediate_check and os.path.isdir(nrpe.homedir):
29 f = open(checkpath, 'w')
30 subprocess.call(
31 croncmd.split(),
32
33=== modified file 'tests/contrib/charmsupport/test_nrpe.py'
34--- tests/contrib/charmsupport/test_nrpe.py 2017-03-02 08:18:34 +0000
35+++ tests/contrib/charmsupport/test_nrpe.py 2017-03-08 12:44:27 +0000
36@@ -3,7 +3,7 @@
37 import subprocess
38
39 from testtools import TestCase
40-from mock import patch, call
41+from mock import patch, call, MagicMock
42
43 from charmhelpers.contrib.charmsupport import nrpe
44 from charmhelpers.core import host
45@@ -295,7 +295,8 @@
46 self.patched['relations_of_type'].return_value = []
47 self.assertEqual(nrpe.get_nagios_unit_name(), 'testunit')
48
49- def test_add_init_service_checks(self):
50+ @patch.object(os.path, 'isdir')
51+ def test_add_init_service_checks(self, mock_isdir):
52 def _exists(init_file):
53 files = ['/etc/init/apache2.conf',
54 '/usr/lib/nagios/plugins/check_upstart_job',
55@@ -309,11 +310,14 @@
56
57 self.patched['exists'].side_effect = _exists
58
59- # Test without systemd
60+ # Test without systemd and /var/lib/nagios does not exist
61 self.patched['init_is_systemd'].return_value = False
62+ mock_isdir.return_value = False
63 bill = nrpe.NRPE()
64 services = ['apache2', 'haproxy']
65 nrpe.add_init_service_checks(bill, services, 'testunit')
66+ mock_isdir.assert_called_with('/var/lib/nagios')
67+ self.patched['call'].assert_not_called()
68 expect_cmds = {
69 'apache2': '/usr/lib/nagios/plugins/check_upstart_job apache2',
70 'haproxy': '/usr/lib/nagios/plugins/check_status_file.py -f '
71@@ -324,6 +328,19 @@
72 self.assertEqual(bill.checks[1].shortname, 'haproxy')
73 self.assertEqual(bill.checks[1].check_cmd, expect_cmds['haproxy'])
74
75+ # without systemd and /var/lib/nagios does exist
76+ mock_isdir.return_value = True
77+ f = MagicMock()
78+ self.patched['open'].return_value = f
79+ bill = nrpe.NRPE()
80+ services = ['apache2', 'haproxy']
81+ nrpe.add_init_service_checks(bill, services, 'testunit')
82+ mock_isdir.assert_called_with('/var/lib/nagios')
83+ self.patched['call'].assert_called_with(
84+ ['/usr/local/lib/nagios/plugins/check_exit_status.pl', '-s',
85+ '/etc/init.d/haproxy', 'status'], stdout=f,
86+ stderr=subprocess.STDOUT)
87+
88 # Test with systemd
89 self.patched['init_is_systemd'].return_value = True
90 nrpe.add_init_service_checks(bill, services, 'testunit')

Subscribers

People subscribed via source and target branches