[bradm] Fixes from pep8 run, added sysvinit daemon services monitoring, use services() to get services to monitor
This commit is contained in:
parent
a02bf5bf9e
commit
b839d5ec39
189
files/nrpe-external-master/check_exit_status.pl
Executable file
189
files/nrpe-external-master/check_exit_status.pl
Executable file
@ -0,0 +1,189 @@
|
||||
#!/usr/bin/perl
|
||||
################################################################################
|
||||
# #
|
||||
# Copyright (C) 2011 Chad Columbus <ccolumbu@hotmail.com> #
|
||||
# #
|
||||
# This program is free software; you can redistribute it and/or modify #
|
||||
# it under the terms of the GNU General Public License as published by #
|
||||
# the Free Software Foundation; either version 2 of the License, or #
|
||||
# (at your option) any later version. #
|
||||
# #
|
||||
# This program is distributed in the hope that it will be useful, #
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
|
||||
# GNU General Public License for more details. #
|
||||
# #
|
||||
# You should have received a copy of the GNU General Public License #
|
||||
# along with this program; if not, write to the Free Software #
|
||||
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
|
||||
# #
|
||||
################################################################################
|
||||
|
||||
use strict;
|
||||
use Getopt::Std;
|
||||
$| = 1;
|
||||
|
||||
my %opts;
|
||||
getopts('heronp:s:', \%opts);
|
||||
|
||||
my $VERSION = "Version 1.0";
|
||||
my $AUTHOR = '(c) 2011 Chad Columbus <ccolumbu@hotmail.com>';
|
||||
|
||||
# Default values:
|
||||
my $script_to_check;
|
||||
my $pattern = 'is running';
|
||||
my $cmd;
|
||||
my $message;
|
||||
my $error;
|
||||
|
||||
# Exit codes
|
||||
my $STATE_OK = 0;
|
||||
my $STATE_WARNING = 1;
|
||||
my $STATE_CRITICAL = 2;
|
||||
my $STATE_UNKNOWN = 3;
|
||||
|
||||
# Parse command line options
|
||||
if ($opts{'h'} || scalar(%opts) == 0) {
|
||||
&print_help();
|
||||
exit($STATE_OK);
|
||||
}
|
||||
|
||||
# Make sure scipt is provided:
|
||||
if ($opts{'s'} eq '') {
|
||||
# Script to run not provided
|
||||
print "\nYou must provide a script to run. Example: -s /etc/init.d/httpd\n";
|
||||
exit($STATE_UNKNOWN);
|
||||
} else {
|
||||
$script_to_check = $opts{'s'};
|
||||
}
|
||||
|
||||
# Make sure only a-z, 0-9, /, _, and - are used in the script.
|
||||
if ($script_to_check =~ /[^a-z0-9\_\-\/\.]/) {
|
||||
# Script contains illegal characters exit.
|
||||
print "\nScript to check can only contain Letters, Numbers, Periods, Underscores, Hyphens, and/or Slashes\n";
|
||||
exit($STATE_UNKNOWN);
|
||||
}
|
||||
|
||||
# See if script is executable
|
||||
if (! -x "$script_to_check") {
|
||||
print "\nIt appears you can't execute $script_to_check, $!\n";
|
||||
exit($STATE_UNKNOWN);
|
||||
}
|
||||
|
||||
# If a pattern is provided use it:
|
||||
if ($opts{'p'} ne '') {
|
||||
$pattern = $opts{'p'};
|
||||
}
|
||||
|
||||
# If -r run command via sudo as root:
|
||||
if ($opts{'r'}) {
|
||||
$cmd = "sudo -n $script_to_check status" . ' 2>&1';
|
||||
} else {
|
||||
$cmd = "$script_to_check status" . ' 2>&1';
|
||||
}
|
||||
|
||||
my $cmd_result = `$cmd`;
|
||||
chomp($cmd_result);
|
||||
if ($cmd_result =~ /sudo/i) {
|
||||
# This means it could not run the sudo command
|
||||
$message = "$script_to_check CRITICAL - Could not run: 'sudo -n $script_to_check status'. Result is $cmd_result";
|
||||
$error = $STATE_UNKNOWN;
|
||||
} else {
|
||||
# Check exitstatus instead of output:
|
||||
if ($opts{'e'} == 1) {
|
||||
if ($? != 0) {
|
||||
# error
|
||||
$message = "$script_to_check CRITICAL - Exit code: $?\.";
|
||||
if ($opts{'o'} == 0) {
|
||||
$message .= " $cmd_result";
|
||||
}
|
||||
$error = $STATE_CRITICAL;
|
||||
} else {
|
||||
# success
|
||||
$message = "$script_to_check OK - Exit code: $?\.";
|
||||
if ($opts{'o'} == 0) {
|
||||
$message .= " $cmd_result";
|
||||
}
|
||||
$error = $STATE_OK;
|
||||
}
|
||||
} else {
|
||||
my $not_check = 1;
|
||||
if ($opts{'n'} == 1) {
|
||||
$not_check = 0;
|
||||
}
|
||||
if (($cmd_result =~ /$pattern/i) == $not_check) {
|
||||
$message = "$script_to_check OK";
|
||||
if ($opts{'o'} == 0) {
|
||||
$message .= " - $cmd_result";
|
||||
}
|
||||
$error = $STATE_OK;
|
||||
} else {
|
||||
$message = "$script_to_check CRITICAL";
|
||||
if ($opts{'o'} == 0) {
|
||||
$message .= " - $cmd_result";
|
||||
}
|
||||
$error = $STATE_CRITICAL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($message eq '') {
|
||||
print "Error: program failed in an unknown way\n";
|
||||
exit($STATE_UNKNOWN);
|
||||
}
|
||||
|
||||
if ($error) {
|
||||
print "$message\n";
|
||||
exit($error);
|
||||
} else {
|
||||
# If we get here we are OK
|
||||
print "$message\n";
|
||||
exit($STATE_OK);
|
||||
}
|
||||
|
||||
####################################
|
||||
# Start Subs:
|
||||
####################################
|
||||
sub print_help() {
|
||||
print << "EOF";
|
||||
Check the output or exit status of a script.
|
||||
$VERSION
|
||||
$AUTHOR
|
||||
|
||||
Options:
|
||||
-h
|
||||
Print detailed help screen
|
||||
|
||||
-s
|
||||
'FULL PATH TO SCRIPT' (required)
|
||||
This is the script to run, the script is designed to run scripts in the
|
||||
/etc/init.d dir (but can run any script) and will call the script with
|
||||
a 'status' argument. So if you use another script make sure it will
|
||||
work with /path/script status, example: /etc/init.d/httpd status
|
||||
|
||||
-e
|
||||
This is the "exitstaus" flag, it means check the exit status
|
||||
code instead of looking for a pattern in the output of the script.
|
||||
|
||||
-p 'REGEX'
|
||||
This is a pattern to look for in the output of the script to confirm it
|
||||
is running, default is 'is running', but not all init.d scripts output
|
||||
(iptables), so you can specify an arbitrary pattern.
|
||||
All patterns are case insensitive.
|
||||
|
||||
-n
|
||||
This is the "NOT" flag, it means not the -p pattern, so if you want to
|
||||
make sure the output of the script does NOT contain -p 'REGEX'
|
||||
|
||||
-r
|
||||
This is the "ROOT" flag, it means run as root via sudo. You will need a
|
||||
line in your /etc/sudoers file like:
|
||||
nagios ALL=(root) NOPASSWD: /etc/init.d/* status
|
||||
|
||||
-o
|
||||
This is the "SUPPRESS OUTPUT" flag. Some programs have a long output
|
||||
(like iptables), this flag suppresses that output so it is not printed
|
||||
as a part of the nagios message.
|
||||
EOF
|
||||
}
|
||||
|
60
files/nrpe-external-master/check_status_file.py
Executable file
60
files/nrpe-external-master/check_status_file.py
Executable file
@ -0,0 +1,60 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
# m
|
||||
# mmmm m m mmmm mmmm mmm mm#mm
|
||||
# #" "# # # #" "# #" "# #" # #
|
||||
# # # # # # # # # #"""" #
|
||||
# ##m#" "mm"# ##m#" ##m#" "#mm" "mm
|
||||
# # # #
|
||||
# " " "
|
||||
# This file is managed by puppet. Do not make local changes.
|
||||
|
||||
#
|
||||
# Copyright 2014 Canonical Ltd.
|
||||
#
|
||||
# Author: Jacek Nykis <jacek.nykis@canonical.com>
|
||||
#
|
||||
|
||||
import re
|
||||
import nagios_plugin
|
||||
|
||||
|
||||
def parse_args():
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Read file and return nagios status based on its content',
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
||||
parser.add_argument('-f', '--status-file', required=True,
|
||||
help='Status file path')
|
||||
parser.add_argument('-c', '--critical-text', default='CRITICAL',
|
||||
help='String indicating critical status')
|
||||
parser.add_argument('-w', '--warning-text', default='WARNING',
|
||||
help='String indicating warning status')
|
||||
parser.add_argument('-o', '--ok-text', default='OK',
|
||||
help='String indicating OK status')
|
||||
parser.add_argument('-u', '--unknown-text', default='UNKNOWN',
|
||||
help='String indicating unknown status')
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def check_status(args):
|
||||
nagios_plugin.check_file_freshness(args.status_file, 43200)
|
||||
|
||||
with open(args.status_file, "r") as f:
|
||||
content = [l.strip() for l in f.readlines()]
|
||||
|
||||
for line in content:
|
||||
if re.search(args.critical_text, line):
|
||||
raise nagios_plugin.CriticalError(line)
|
||||
elif re.search(args.warning_text, line):
|
||||
raise nagios_plugin.WarnError(line)
|
||||
elif re.search(args.unknown_text, line):
|
||||
raise nagios_plugin.UnknownError(line)
|
||||
else:
|
||||
print line
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
args = parse_args()
|
||||
nagios_plugin.try_check(check_status, args)
|
78
files/nrpe-external-master/nagios_plugin.py
Executable file
78
files/nrpe-external-master/nagios_plugin.py
Executable file
@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env python
|
||||
# m
|
||||
# mmmm m m mmmm mmmm mmm mm#mm
|
||||
# #" "# # # #" "# #" "# #" # #
|
||||
# # # # # # # # # #"""" #
|
||||
# ##m#" "mm"# ##m#" ##m#" "#mm" "mm
|
||||
# # # #
|
||||
# " " "
|
||||
# This file is managed by puppet. Do not make local changes.
|
||||
|
||||
# Copyright (C) 2005, 2006, 2007, 2012 James Troup <james.troup@canonical.com>
|
||||
|
||||
import os
|
||||
import stat
|
||||
import time
|
||||
import traceback
|
||||
import sys
|
||||
|
||||
|
||||
################################################################################
|
||||
|
||||
class CriticalError(Exception):
|
||||
"""This indicates a critical error."""
|
||||
pass
|
||||
|
||||
|
||||
class WarnError(Exception):
|
||||
"""This indicates a warning condition."""
|
||||
pass
|
||||
|
||||
|
||||
class UnknownError(Exception):
|
||||
"""This indicates a unknown error was encountered."""
|
||||
pass
|
||||
|
||||
|
||||
def try_check(function, *args, **kwargs):
|
||||
"""Perform a check with error/warn/unknown handling."""
|
||||
try:
|
||||
function(*args, **kwargs)
|
||||
except UnknownError, msg:
|
||||
print msg
|
||||
sys.exit(3)
|
||||
except CriticalError, msg:
|
||||
print msg
|
||||
sys.exit(2)
|
||||
except WarnError, msg:
|
||||
print msg
|
||||
sys.exit(1)
|
||||
except:
|
||||
print "%s raised unknown exception '%s'" % (function, sys.exc_info()[0])
|
||||
print '=' * 60
|
||||
traceback.print_exc(file=sys.stdout)
|
||||
print '=' * 60
|
||||
sys.exit(3)
|
||||
|
||||
|
||||
################################################################################
|
||||
|
||||
def check_file_freshness(filename, newer_than=600):
|
||||
"""Check a file exists, is readable and is newer than <n> seconds (where <n> defaults to 600)."""
|
||||
# First check the file exists and is readable
|
||||
if not os.path.exists(filename):
|
||||
raise CriticalError("%s: does not exist." % (filename))
|
||||
if os.access(filename, os.R_OK) == 0:
|
||||
raise CriticalError("%s: is not readable." % (filename))
|
||||
|
||||
# Then ensure the file is up-to-date enough
|
||||
mtime = os.stat(filename)[stat.ST_MTIME]
|
||||
last_modified = time.time() - mtime
|
||||
if last_modified > newer_than:
|
||||
raise CriticalError("%s: was last modified on %s and is too old (> %s seconds)."
|
||||
% (filename, time.ctime(mtime), newer_than))
|
||||
if last_modified < 0:
|
||||
raise CriticalError("%s: was last modified on %s which is in the future."
|
||||
% (filename, time.ctime(mtime)))
|
||||
|
||||
################################################################################
|
@ -1,5 +1,6 @@
|
||||
#!/usr/bin/python
|
||||
import sys
|
||||
import os
|
||||
|
||||
from glance_utils import (
|
||||
do_openstack_upgrade,
|
||||
@ -7,6 +8,7 @@ from glance_utils import (
|
||||
migrate_database,
|
||||
register_configs,
|
||||
restart_map,
|
||||
services,
|
||||
CLUSTER_RES,
|
||||
PACKAGES,
|
||||
SERVICES,
|
||||
@ -452,7 +454,9 @@ def amqp_changed():
|
||||
return
|
||||
CONFIGS.write(GLANCE_API_CONF)
|
||||
|
||||
@hooks.hook('nrpe-external-master-relation-joined', 'nrpe-external-master-relation-changed')
|
||||
|
||||
@hooks.hook('nrpe-external-master-relation-joined',
|
||||
'nrpe-external-master-relation-changed')
|
||||
def update_nrpe_config():
|
||||
# Find out if nrpe set nagios_hostname
|
||||
hostname = None
|
||||
@ -470,16 +474,35 @@ def update_nrpe_config():
|
||||
else:
|
||||
current_unit = local_unit()
|
||||
|
||||
nrpe.add_check(
|
||||
shortname='glance-api',
|
||||
description='process check {%s}' % current_unit,
|
||||
check_cmd = 'check_upstart_job glance-api',
|
||||
)
|
||||
nrpe.add_check(
|
||||
shortname='glance-registry',
|
||||
description='process check {%s}' % current_unit,
|
||||
check_cmd = 'check_upstart_job glance-registry',
|
||||
)
|
||||
services_to_monitor = services()
|
||||
|
||||
for service in services_to_monitor:
|
||||
upstart_init = '/etc/init/%s.conf' % service
|
||||
sysv_init = '/etc/init.d/%s' % service
|
||||
|
||||
if os.path.exists(upstart_init):
|
||||
nrpe.add_check(
|
||||
shortname=service,
|
||||
description='process check {%s}' % current_unit,
|
||||
check_cmd='check_upstart_job %s' % service,
|
||||
)
|
||||
elif os.path.exists(sysv_init):
|
||||
cronpath = '/etc/cron.d/nagios-service-check-%s' % service
|
||||
checkpath = os.path.join(os.environ['CHARM_DIR'],
|
||||
'files/nrpe-external-master',
|
||||
'check_exit_status.pl'),
|
||||
cron_template = '*/5 * * * * root \
|
||||
%s -s /etc/init.d/%s status > /var/lib/nagios/service-check-%s.txt\n' \
|
||||
% (checkpath[0], service, service)
|
||||
f = open(cronpath, 'w')
|
||||
f.write(cron_template)
|
||||
f.close()
|
||||
nrpe.add_check(
|
||||
shortname=service,
|
||||
description='process check {%s}' % current_unit,
|
||||
check_cmd='check_status_file.py -f \
|
||||
/var/lib/nagios/service-check-%s.txt' % service,
|
||||
)
|
||||
|
||||
nrpe.write()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user