1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
#!/usr/bin/perl
#
# (c)2001 Sebastian Hetze, Linux Information Systems AG
# send bug reports to <S.Hetze@Linux-AG.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 (or with Nagios); if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA
#
#
# Check apache status information provided by mod_status to find
# out about the load (number of servers working) and the
# performance (average response time for recent requests).
#
# Usage:
# check_apache -H <host> [-lhV] [-w <warn>] [-c <crit>] [-u <url>]
#
# check_apache <host> <warn> <crit> <url> (if you cannot avoid it)
#
use LWP::UserAgent;
use URI::URL;
use Getopt::Long;
Getopt::Long::Configure('bundling');
$version=0.01;
my %ERRORS = ('UNKNOWN' , '-1',
'OK' , '0',
'WARNING', '1',
'CRITICAL', '2');
#
# some default values
#
$perf_w=500;
$perf_c=1000;
$load_w=20;
$load_c=30;
$TIMEOUT=15;
#
# get command line options the regular way
#
GetOptions
("V" => \$opt_V, "version" => \$opt_V,
"h" => \$opt_h, "help" => \$opt_h,
"l" => \$opt_l, "load" => \$opt_l,
"v" => \$verbose, "verbose" => \$verbose,
"w=s" => \$opt_w, "warning=s" => \$opt_w,
"c=s" => \$opt_c, "critical=s" => \$opt_c,
"H=s" => \$opt_H, "hostname=s" => \$opt_H,
"u=s" => \$opt_u, "url=s" => \$opt_u);
#
# handle the verbose stuff first
#
if ($opt_V) {
print "\n";
print "check_apache nagios plugin version $version\n";
print "\n";
print "The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n";
print "copies of the plugins under the terms of the GNU General Public License.\n";
print "For more information about these matters, see the file named COPYING.\n";
print "\n";
print "Copyright (c) 2001 Sebastian Hetze Linux Information Systems AG\n";
print "\n";
print "\n";
exit $ERRORS{'UNKNOWN'};
}
if ($opt_h) {
print_help();
exit $ERRORS{'UNKNOWN'};
}
#
# now get options the weired way and set the defaults
# if nothing else is provided
#
$opt_H = shift unless ($opt_H);
print_usage() unless ($opt_H);
if($opt_l) {
$autostring="?auto";
($opt_w) || ($opt_w = shift) || ($opt_w = $load_w);
$warn = $1 if ($opt_w =~ /([0-9]+)/);
($opt_c) || ($opt_c = shift) || ($opt_c = $load_c);
$alert = $1 if ($opt_c =~ /([0-9]+)/);
} else {
$autostring="";
($opt_w) || ($opt_w = shift) || ($opt_w = $perf_w);
$warn = $1 if ($opt_w =~ /([0-9]+)/);
($opt_c) || ($opt_c = shift) || ($opt_c = $perf_c);
$alert = $1 if ($opt_c =~ /([0-9]+)/);
}
($opt_u) || ($opt_u = shift) || ($opt_u = "/server-status");
#
# dont let us wait forever...
#
$SIG{'ALRM'} = sub {
print ("ERROR: No response from HTTP server (alarm)\n");
exit $ERRORS{"UNKNOWN"};
};
alarm($TIMEOUT);
#
# now we set things up for the real work
# and fire up the request
#
$ua = new LWP::UserAgent;
$ua->agent("Nagios/0.1 " . $ua->agent);
$urlstring = "http://" . $opt_H . $opt_u . $autostring;
$url = url($urlstring);
my $req = new HTTP::Request 'GET', $url;
my $res = $ua->request($req);
#
# hopefully we´ve got something usefull
#
if ($res->is_success) {
if($opt_l) {
foreach $_ (split /^/m, $res->content) {
next if /^\s*$/;
#
# this is the load checking section
# we parse the whole content, just in case someone
# wants to use this some day in the future
#
if (/^Total Accesses:\s+([0-9.]+)/) { $accesses = $1; next; }
if (/^Total kBytes:\s+([0-9.]+)/) { $kbytes = $1; next; }
if (/^CPULoad:\s+([0-9.]+)\s+/) { $load = $1; next; }
if (/^Uptime:\s+([0-9.]+)\s+/) { $uptime = $1; next; }
if (/^ReqPerSec:\s+([0-9.]+)\s+/) { $rps = $1; next; }
if (/^BytesPerSec:\s+([0-9.]+)\s+/) { $bps = $1; next; }
if (/^BytesPerReq:\s+([0-9.]+)\s+/) { $bpr = $1; next; }
if (/^BusyServers:\s+([0-9.]+)\s+/) { $busy = $1; next; }
if (/^IdleServers:\s+([0-9.]+)\s+/) { $idle = $1; next; }
if (/^Scoreboard:\s+([SRWKDLG_.]+)\s+/) { $score = $1; next; }
print "Unknown Status\n";
exit $ERRORS{"UNKNOWN"};
}
#
# now we even parse the whole scoreboard, just for fun
#
foreach $scorepoint (split //m, $score) {
if($scorepoint eq '.') { $scores{'.'}+=1; next; } # Unused
if($scorepoint eq '_') { $scores{'_'}+=1; next; } # Waiting
if($scorepoint eq 'S') { $scores{'S'}+=1; next; } # Starting
if($scorepoint eq 'R') { $scores{'R'}+=1; next; } # Reading
if($scorepoint eq 'W') { $scores{'W'}+=1; next; } # Writing
if($scorepoint eq 'K') { $scores{'K'}+=1; next; } # Keepalive
if($scorepoint eq 'D') { $scores{'D'}+=1; next; } # DNS Lookup
if($scorepoint eq 'L') { $scores{'L'}+=1; next; } # Logging
if($scorepoint eq 'G') { $scores{'G'}+=1; next; } # Going
}
if($busy>$alert) {
printf "HTTPD CRITICAL: %.0f servers running\n", $busy;
exit $ERRORS{"CRITICAL"};
}
if($busy>$warn) {
printf "HTTPD WARNING: %.0f servers running\n", $busy;
exit $ERRORS{"WARNING"};
}
printf "HTTPD ok: %.0f servers running, %d idle\n", $busy, $idle;
exit $ERRORS{"OK"};
} else {
#
# this is the performance check section
# We are a bit lazy here, no parsing of the initial data
# block and the scoreboard.
# However, you have the whole set of per server
# information to play with ;-)
# The actual performance is measured by adding up the
# milliseconds required to process the most recent
# requests of all instances and then taking the average.
#
foreach $tablerow (split /<tr>/m, $res->content) {
($empty,$Srv,$PID,$Acc,$M,$CPU,$SS,$Req,$Conn,$Child,$Slot,$Client,$VHost,$Request)
= split /<td>/, $tablerow;
if($Req) {
$lines+=1;
$req_sum+=$Req;
}
undef $Req;
}
$average=$req_sum/$lines;
if($average>$alert) {
printf "HTTPD CRITICAL: average response time %.0f
milliseconds\n", $average;
exit $ERRORS{"CRITICAL"};
}
if($average>$warn) {
printf "HTTPD WARNING: average response time %.0f
milliseconds\n", $average;
exit $ERRORS{"WARNING"};
}
if($average>0) {
printf "HTTPD ok: average response time %.0f milliseconds\n",
$average;
exit $ERRORS{"OK"};
}
print "Unknown Status\n";
exit $ERRORS{"UNKNOWN"};
}
} else {
print "HTTP request failed\n";
exit $ERRORS{"CRITICAL"};
}
#
# ok, now we are almost through
# These last subroutines do the things for those that do not
# read source code.
#
sub print_usage () {
print "Usage: $0 -H <host> [-lhV] [-w <warn>] [-c <crit>] [-u <url>]\n"; }
sub print_help () {
print "\n";
print "\n";
print "check_apache nagios plugin version $version\n";
print "\n";
print "The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n";
print "copies of the plugins under the terms of the GNU General Public License.\n";
print "For more information about these matters, see the file named COPYING.\n";
print "\n";
print "Copyright (c) 2001 Sebastian Hetze Linux Information Systems AG\n";
print "\n";
print "\n";
print "This plugin checks the apache HTTP service on the specified host.\n";
print "It uses the mod_status facilities provided by the apache server.\n";
print "The monitoring server must be authorized in httpd.conf.\n";
print "\n";
print "\n";
print_usage();
print "\n";
print "Options:\n";
print " -H, --hostname=ADDRESS\n";
print " host name argument for server.\n";
print " -l, --load\n";
print " check load instead of performance.\n";
print " -h, --help\n";
print " print detailed help screen.\n";
print " -V, --version\n";
print " print version information.\n";
print " -w, --warning=INTEGER\n";
print " load / performance level at which a warning message will be gererated.\n";
print " -c, --critical=INTEGER\n";
print " load / performance level at which a critical message will be gererated.\n";
print " -u, --url=PATH\n";
print " location to call mod_status.\n";
print "\n";
print " Defaults for performance checking are $perf_w/$perf_c msec.\n";
print " Defaults for load checking are $load_w/$load_c servers running.\n";
print "\n";
print "\n";
}
#
# the end
#
|