Merge lp:~brad-marshall/charms/trusty/nrpe-external-master/add-daemon-checks into lp:charms/nrpe-external-master

Proposed by Brad Marshall
Status: Merged
Approved by: Brad Marshall
Approved revision: 37
Merged at revision: 37
Proposed branch: lp:~brad-marshall/charms/trusty/nrpe-external-master/add-daemon-checks
Merge into: lp:charms/nrpe-external-master
Diff against target: 341 lines (+317/-0)
4 files modified
files/check_exit_status.pl (+189/-0)
files/check_status_file.py (+54/-0)
files/check_upstart_job (+72/-0)
hooks/install (+2/-0)
To merge this branch: bzr merge lp:~brad-marshall/charms/trusty/nrpe-external-master/add-daemon-checks
Reviewer Review Type Date Requested Status
Review Queue (community) automated testing Needs Fixing
JuanJo Ciarlante (community) Approve
Review via email: mp+242037@code.launchpad.net

Description of the change

Added nagios checks for monitoring upstart and sysvinit daemons

To post a comment you must log in.
Revision history for this message
JuanJo Ciarlante (jjo) wrote :

As per inline comments - please remove the puppet header, also suggest adding a 'juju' one, LGTM afterwards.

37. By Brad Marshall

[bradm] Replaced puppet header with juju one

Revision history for this message
JuanJo Ciarlante (jjo) wrote :

LGTM.

review: Approve
Revision history for this message
Review Queue (review-queue) wrote :

This items has failed automated testing! Results available here http://reports.vapour.ws/charm-tests/charm-bundle-test-10337-results

review: Needs Fixing (automated testing)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'files/check_exit_status.pl'
--- files/check_exit_status.pl 1970-01-01 00:00:00 +0000
+++ files/check_exit_status.pl 2014-11-20 01:02:07 +0000
@@ -0,0 +1,189 @@
1#!/usr/bin/perl
2################################################################################
3# #
4# Copyright (C) 2011 Chad Columbus <ccolumbu@hotmail.com> #
5# #
6# This program is free software; you can redistribute it and/or modify #
7# it under the terms of the GNU General Public License as published by #
8# the Free Software Foundation; either version 2 of the License, or #
9# (at your option) any later version. #
10# #
11# This program is distributed in the hope that it will be useful, #
12# but WITHOUT ANY WARRANTY; without even the implied warranty of #
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14# GNU General Public License for more details. #
15# #
16# You should have received a copy of the GNU General Public License #
17# along with this program; if not, write to the Free Software #
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
19# #
20################################################################################
21
22use strict;
23use Getopt::Std;
24$| = 1;
25
26my %opts;
27getopts('heronp:s:', \%opts);
28
29my $VERSION = "Version 1.0";
30my $AUTHOR = '(c) 2011 Chad Columbus <ccolumbu@hotmail.com>';
31
32# Default values:
33my $script_to_check;
34my $pattern = 'is running';
35my $cmd;
36my $message;
37my $error;
38
39# Exit codes
40my $STATE_OK = 0;
41my $STATE_WARNING = 1;
42my $STATE_CRITICAL = 2;
43my $STATE_UNKNOWN = 3;
44
45# Parse command line options
46if ($opts{'h'} || scalar(%opts) == 0) {
47 &print_help();
48 exit($STATE_OK);
49}
50
51# Make sure scipt is provided:
52if ($opts{'s'} eq '') {
53 # Script to run not provided
54 print "\nYou must provide a script to run. Example: -s /etc/init.d/httpd\n";
55 exit($STATE_UNKNOWN);
56} else {
57 $script_to_check = $opts{'s'};
58}
59
60# Make sure only a-z, 0-9, /, _, and - are used in the script.
61if ($script_to_check =~ /[^a-z0-9\_\-\/\.]/) {
62 # Script contains illegal characters exit.
63 print "\nScript to check can only contain Letters, Numbers, Periods, Underscores, Hyphens, and/or Slashes\n";
64 exit($STATE_UNKNOWN);
65}
66
67# See if script is executable
68if (! -x "$script_to_check") {
69 print "\nIt appears you can't execute $script_to_check, $!\n";
70 exit($STATE_UNKNOWN);
71}
72
73# If a pattern is provided use it:
74if ($opts{'p'} ne '') {
75 $pattern = $opts{'p'};
76}
77
78# If -r run command via sudo as root:
79if ($opts{'r'}) {
80 $cmd = "sudo -n $script_to_check status" . ' 2>&1';
81} else {
82 $cmd = "$script_to_check status" . ' 2>&1';
83}
84
85my $cmd_result = `$cmd`;
86chomp($cmd_result);
87if ($cmd_result =~ /sudo/i) {
88 # This means it could not run the sudo command
89 $message = "$script_to_check CRITICAL - Could not run: 'sudo -n $script_to_check status'. Result is $cmd_result";
90 $error = $STATE_UNKNOWN;
91} else {
92 # Check exitstatus instead of output:
93 if ($opts{'e'} == 1) {
94 if ($? != 0) {
95 # error
96 $message = "$script_to_check CRITICAL - Exit code: $?\.";
97 if ($opts{'o'} == 0) {
98 $message .= " $cmd_result";
99 }
100 $error = $STATE_CRITICAL;
101 } else {
102 # success
103 $message = "$script_to_check OK - Exit code: $?\.";
104 if ($opts{'o'} == 0) {
105 $message .= " $cmd_result";
106 }
107 $error = $STATE_OK;
108 }
109 } else {
110 my $not_check = 1;
111 if ($opts{'n'} == 1) {
112 $not_check = 0;
113 }
114 if (($cmd_result =~ /$pattern/i) == $not_check) {
115 $message = "$script_to_check OK";
116 if ($opts{'o'} == 0) {
117 $message .= " - $cmd_result";
118 }
119 $error = $STATE_OK;
120 } else {
121 $message = "$script_to_check CRITICAL";
122 if ($opts{'o'} == 0) {
123 $message .= " - $cmd_result";
124 }
125 $error = $STATE_CRITICAL;
126 }
127 }
128}
129
130if ($message eq '') {
131 print "Error: program failed in an unknown way\n";
132 exit($STATE_UNKNOWN);
133}
134
135if ($error) {
136 print "$message\n";
137 exit($error);
138} else {
139 # If we get here we are OK
140 print "$message\n";
141 exit($STATE_OK);
142}
143
144####################################
145# Start Subs:
146####################################
147sub print_help() {
148 print << "EOF";
149Check the output or exit status of a script.
150$VERSION
151$AUTHOR
152
153Options:
154-h
155 Print detailed help screen
156
157-s
158 'FULL PATH TO SCRIPT' (required)
159 This is the script to run, the script is designed to run scripts in the
160 /etc/init.d dir (but can run any script) and will call the script with
161 a 'status' argument. So if you use another script make sure it will
162 work with /path/script status, example: /etc/init.d/httpd status
163
164-e
165 This is the "exitstaus" flag, it means check the exit status
166 code instead of looking for a pattern in the output of the script.
167
168-p 'REGEX'
169 This is a pattern to look for in the output of the script to confirm it
170 is running, default is 'is running', but not all init.d scripts output
171 (iptables), so you can specify an arbitrary pattern.
172 All patterns are case insensitive.
173
174-n
175 This is the "NOT" flag, it means not the -p pattern, so if you want to
176 make sure the output of the script does NOT contain -p 'REGEX'
177
178-r
179 This is the "ROOT" flag, it means run as root via sudo. You will need a
180 line in your /etc/sudoers file like:
181 nagios ALL=(root) NOPASSWD: /etc/init.d/* status
182
183-o
184 This is the "SUPPRESS OUTPUT" flag. Some programs have a long output
185 (like iptables), this flag suppresses that output so it is not printed
186 as a part of the nagios message.
187EOF
188}
189
0190
=== added file 'files/check_status_file.py'
--- files/check_status_file.py 1970-01-01 00:00:00 +0000
+++ files/check_status_file.py 2014-11-20 01:02:07 +0000
@@ -0,0 +1,54 @@
1#!/usr/bin/python
2#--------------------------------------------------------
3# This file is managed by Juju
4#--------------------------------------------------------
5
6#
7# Copyright 2014 Canonical Ltd.
8#
9# Author: Jacek Nykis <jacek.nykis@canonical.com>
10#
11
12import re
13import nagios_plugin
14
15
16def parse_args():
17 import argparse
18
19 parser = argparse.ArgumentParser(
20 description='Read file and return nagios status based on its content',
21 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
22 parser.add_argument('-f', '--status-file', required=True,
23 help='Status file path')
24 parser.add_argument('-c', '--critical-text', default='CRITICAL',
25 help='String indicating critical status')
26 parser.add_argument('-w', '--warning-text', default='WARNING',
27 help='String indicating warning status')
28 parser.add_argument('-o', '--ok-text', default='OK',
29 help='String indicating OK status')
30 parser.add_argument('-u', '--unknown-text', default='UNKNOWN',
31 help='String indicating unknown status')
32 return parser.parse_args()
33
34
35def check_status(args):
36 nagios_plugin.check_file_freshness(args.status_file, 43200)
37
38 with open(args.status_file, "r") as f:
39 content = [l.strip() for l in f.readlines()]
40
41 for line in content:
42 if re.search(args.critical_text, line):
43 raise nagios_plugin.CriticalError(line)
44 elif re.search(args.warning_text, line):
45 raise nagios_plugin.WarnError(line)
46 elif re.search(args.unknown_text, line):
47 raise nagios_plugin.UnknownError(line)
48 else:
49 print line
50
51
52if __name__ == '__main__':
53 args = parse_args()
54 nagios_plugin.try_check(check_status, args)
055
=== added file 'files/check_upstart_job'
--- files/check_upstart_job 1970-01-01 00:00:00 +0000
+++ files/check_upstart_job 2014-11-20 01:02:07 +0000
@@ -0,0 +1,72 @@
1#!/usr/bin/python
2
3#
4# Copyright 2012, 2013 Canonical Ltd.
5#
6# Author: Paul Collins <paul.collins@canonical.com>
7#
8# Based on http://www.eurion.net/python-snippets/snippet/Upstart%20service%20status.html
9#
10
11import sys
12
13import dbus
14
15
16class Upstart(object):
17 def __init__(self):
18 self._bus = dbus.SystemBus()
19 self._upstart = self._bus.get_object('com.ubuntu.Upstart',
20 '/com/ubuntu/Upstart')
21 def get_job(self, job_name):
22 path = self._upstart.GetJobByName(job_name,
23 dbus_interface='com.ubuntu.Upstart0_6')
24 return self._bus.get_object('com.ubuntu.Upstart', path)
25
26 def get_properties(self, job):
27 path = job.GetInstance([], dbus_interface='com.ubuntu.Upstart0_6.Job')
28 instance = self._bus.get_object('com.ubuntu.Upstart', path)
29 return instance.GetAll('com.ubuntu.Upstart0_6.Instance',
30 dbus_interface=dbus.PROPERTIES_IFACE)
31
32 def get_job_instances(self, job_name):
33 job = self.get_job(job_name)
34 paths = job.GetAllInstances([], dbus_interface='com.ubuntu.Upstart0_6.Job')
35 return [self._bus.get_object('com.ubuntu.Upstart', path) for path in paths]
36
37 def get_job_instance_properties(self, job):
38 return job.GetAll('com.ubuntu.Upstart0_6.Instance',
39 dbus_interface=dbus.PROPERTIES_IFACE)
40
41try:
42 upstart = Upstart()
43 try:
44 job = upstart.get_job(sys.argv[1])
45 props = upstart.get_properties(job)
46
47 if props['state'] == 'running':
48 print 'OK: %s is running' % sys.argv[1]
49 sys.exit(0)
50 else:
51 print 'CRITICAL: %s is not running' % sys.argv[1]
52 sys.exit(2)
53
54 except dbus.DBusException as e:
55 instances = upstart.get_job_instances(sys.argv[1])
56 propses = [upstart.get_job_instance_properties(instance) for instance in instances]
57 states = dict([(props['name'], props['state']) for props in propses])
58 if len(states) != states.values().count('running'):
59 not_running = []
60 for name in states.keys():
61 if states[name] != 'running':
62 not_running.append(name)
63 print 'CRITICAL: %d instances of %s not running: %s' % \
64 (len(not_running), sys.argv[1], not_running.join(', '))
65 sys.exit(2)
66 else:
67 print 'OK: %d instances of %s running' % (len(states), sys.argv[1])
68
69except dbus.DBusException as e:
70 print 'CRITICAL: failed to get properties of \'%s\' from upstart' % sys.argv[1]
71 sys.exit(2)
72
073
=== modified file 'hooks/install'
--- hooks/install 2014-11-06 07:40:09 +0000
+++ hooks/install 2014-11-20 01:02:07 +0000
@@ -14,6 +14,8 @@
14cp files/nagios_plugin.py /usr/lib/nagios/plugins/nagios_plugin.py14cp files/nagios_plugin.py /usr/lib/nagios/plugins/nagios_plugin.py
15ln -fs /usr/lib/nagios/plugins/nagios_plugin.py /usr/local/lib/nagios/plugins/nagios_plugin.py15ln -fs /usr/lib/nagios/plugins/nagios_plugin.py /usr/local/lib/nagios/plugins/nagios_plugin.py
1616
17cp files/check_* /usr/local/lib/nagios/plugins/
18
17cp files/default_rsync /etc/default/rsync19cp files/default_rsync /etc/default/rsync
18cp files/rsyncd.conf /etc/rsyncd.conf20cp files/rsyncd.conf /etc/rsyncd.conf
1921

Subscribers

People subscribed via source and target branches

to all changes: