[bradm] Removed nagios check files that were moved to nrpe-external-master charm

This commit is contained in:
Brad Marshall 2014-11-18 11:21:15 +10:00
parent 4f19ccceed
commit 9ff64eeb7d
5 changed files with 8 additions and 395 deletions

View File

@ -1,189 +0,0 @@
#!/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
}

View File

@ -1,60 +0,0 @@
#!/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)

View File

@ -1,72 +0,0 @@
#!/usr/bin/python
#
# Copyright 2012, 2013 Canonical Ltd.
#
# Author: Paul Collins <paul.collins@canonical.com>
#
# Based on http://www.eurion.net/python-snippets/snippet/Upstart%20service%20status.html
#
import sys
import dbus
class Upstart(object):
def __init__(self):
self._bus = dbus.SystemBus()
self._upstart = self._bus.get_object('com.ubuntu.Upstart',
'/com/ubuntu/Upstart')
def get_job(self, job_name):
path = self._upstart.GetJobByName(job_name,
dbus_interface='com.ubuntu.Upstart0_6')
return self._bus.get_object('com.ubuntu.Upstart', path)
def get_properties(self, job):
path = job.GetInstance([], dbus_interface='com.ubuntu.Upstart0_6.Job')
instance = self._bus.get_object('com.ubuntu.Upstart', path)
return instance.GetAll('com.ubuntu.Upstart0_6.Instance',
dbus_interface=dbus.PROPERTIES_IFACE)
def get_job_instances(self, job_name):
job = self.get_job(job_name)
paths = job.GetAllInstances([], dbus_interface='com.ubuntu.Upstart0_6.Job')
return [self._bus.get_object('com.ubuntu.Upstart', path) for path in paths]
def get_job_instance_properties(self, job):
return job.GetAll('com.ubuntu.Upstart0_6.Instance',
dbus_interface=dbus.PROPERTIES_IFACE)
try:
upstart = Upstart()
try:
job = upstart.get_job(sys.argv[1])
props = upstart.get_properties(job)
if props['state'] == 'running':
print 'OK: %s is running' % sys.argv[1]
sys.exit(0)
else:
print 'CRITICAL: %s is not running' % sys.argv[1]
sys.exit(2)
except dbus.DBusException as e:
instances = upstart.get_job_instances(sys.argv[1])
propses = [upstart.get_job_instance_properties(instance) for instance in instances]
states = dict([(props['name'], props['state']) for props in propses])
if len(states) != states.values().count('running'):
not_running = []
for name in states.keys():
if states[name] != 'running':
not_running.append(name)
print 'CRITICAL: %d instances of %s not running: %s' % \
(len(not_running), sys.argv[1], not_running.join(', '))
sys.exit(2)
else:
print 'OK: %d instances of %s running' % (len(states), sys.argv[1])
except dbus.DBusException as e:
print 'CRITICAL: failed to get properties of \'%s\' from upstart' % sys.argv[1]
sys.exit(2)

View File

@ -1,69 +0,0 @@
#!/usr/bin/env python
# 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)))
################################################################################

View File

@ -854,7 +854,8 @@ def neutron_api_relation_broken():
for rid in relation_ids('quantum-network-service'):
quantum_joined(rid=rid)
@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
@ -881,19 +882,21 @@ def update_nrpe_config():
nrpe.add_check(
shortname=service,
description='process check {%s}' % current_unit,
check_cmd = 'check_upstart_job %s' % service,
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)
cron_template = '*/5 * * * * root \
/usr/local/lib/nagios/plugins/check_exit_status.pl -s /etc/init.d/%s \
status > /var/lib/nagios/service-check-%s.txt\n' % (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,
check_cmd='check_status_file.py -f \
/var/lib/nagios/service-check-%s.txt' % service,
)
nrpe.write()