~brad-marshall/charms/trusty/quantum-gateway/add-nrpe-checks

« back to all changes in this revision

Viewing changes to files/nrpe-external-master/check_exit_status.pl

  • Committer: Brad Marshall
  • Date: 2014-11-18 01:26:17 UTC
  • Revision ID: brad.marshall@canonical.com-20141118012617-3a1d69r8nqc59q1h
[bradm] Removed nagios check files that were moved to nrpe-external-master charm

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
22
 
use strict;
23
 
use Getopt::Std;
24
 
$| = 1;
25
 
 
26
 
my %opts;
27
 
getopts('heronp:s:', \%opts);
28
 
 
29
 
my $VERSION = "Version 1.0";
30
 
my $AUTHOR = '(c) 2011 Chad Columbus <ccolumbu@hotmail.com>';
31
 
 
32
 
# Default values:
33
 
my $script_to_check;
34
 
my $pattern = 'is running';
35
 
my $cmd;
36
 
my $message;
37
 
my $error;
38
 
 
39
 
# Exit codes
40
 
my $STATE_OK = 0;
41
 
my $STATE_WARNING = 1;
42
 
my $STATE_CRITICAL = 2;
43
 
my $STATE_UNKNOWN = 3;
44
 
 
45
 
# Parse command line options
46
 
if ($opts{'h'} || scalar(%opts) == 0) {
47
 
        &print_help();
48
 
        exit($STATE_OK);
49
 
}
50
 
 
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";
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.
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";
64
 
        exit($STATE_UNKNOWN);
65
 
}
66
 
 
67
 
# See if script is executable
68
 
if (! -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:
74
 
if ($opts{'p'} ne '') {
75
 
        $pattern = $opts{'p'};
76
 
}
77
 
 
78
 
# If -r run command via sudo as root:
79
 
if ($opts{'r'}) {
80
 
        $cmd = "sudo -n $script_to_check status" . ' 2>&1';
81
 
} else {
82
 
        $cmd = "$script_to_check status" . ' 2>&1';
83
 
}
84
 
 
85
 
my $cmd_result = `$cmd`;
86
 
chomp($cmd_result);
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;
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
 
 
130
 
if ($message eq '') {
131
 
        print "Error: program failed in an unknown way\n";
132
 
        exit($STATE_UNKNOWN);
133
 
}
134
 
 
135
 
if ($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
 
####################################
147
 
sub print_help() {
148
 
        print << "EOF";
149
 
Check the output or exit status of a script.
150
 
$VERSION
151
 
$AUTHOR
152
 
 
153
 
Options:
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.
187
 
EOF
188
 
}
189