2
################################################################################
4
# Copyright (C) 2011 Chad Columbus <ccolumbu@hotmail.com> #
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. #
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. #
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 #
20
################################################################################
27
getopts('heronp:s:', \%opts);
29
my $VERSION = "Version 1.0";
30
my $AUTHOR = '(c) 2011 Chad Columbus <ccolumbu@hotmail.com>';
34
my $pattern = 'is running';
41
my $STATE_WARNING = 1;
42
my $STATE_CRITICAL = 2;
43
my $STATE_UNKNOWN = 3;
45
# Parse command line options
46
if ($opts{'h'} || scalar(%opts) == 0) {
51
# Make sure scipt is provided:
52
if ($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";
57
$script_to_check = $opts{'s'};
60
# Make sure only a-z, 0-9, /, _, and - are used in the script.
61
if ($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";
67
# See if script is executable
68
if (! -x "$script_to_check") {
69
print "\nIt appears you can't execute $script_to_check, $!\n";
73
# If a pattern is provided use it:
74
if ($opts{'p'} ne '') {
75
$pattern = $opts{'p'};
78
# If -r run command via sudo as root:
80
$cmd = "sudo -n $script_to_check status" . ' 2>&1';
82
$cmd = "$script_to_check status" . ' 2>&1';
85
my $cmd_result = `$cmd`;
87
if ($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;
92
# Check exitstatus instead of output:
93
if ($opts{'e'} == 1) {
96
$message = "$script_to_check CRITICAL - Exit code: $?\.";
97
if ($opts{'o'} == 0) {
98
$message .= " $cmd_result";
100
$error = $STATE_CRITICAL;
103
$message = "$script_to_check OK - Exit code: $?\.";
104
if ($opts{'o'} == 0) {
105
$message .= " $cmd_result";
111
if ($opts{'n'} == 1) {
114
if (($cmd_result =~ /$pattern/i) == $not_check) {
115
$message = "$script_to_check OK";
116
if ($opts{'o'} == 0) {
117
$message .= " - $cmd_result";
121
$message = "$script_to_check CRITICAL";
122
if ($opts{'o'} == 0) {
123
$message .= " - $cmd_result";
125
$error = $STATE_CRITICAL;
130
if ($message eq '') {
131
print "Error: program failed in an unknown way\n";
132
exit($STATE_UNKNOWN);
139
# If we get here we are OK
144
####################################
146
####################################
149
Check the output or exit status of a script.
155
Print detailed help screen
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
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.
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.
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'
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
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.