-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_apache_serverstatus.pl
328 lines (284 loc) · 9.4 KB
/
check_apache_serverstatus.pl
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/perl -w
#
# The MIT License (MIT)
#
# Copyright (c) 2016 Steffen Schoch - dsb it services GmbH & Co. KG
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
#
# Feel free to contact me via email: schoch@dsb-its.net
#
# 2018-02-06, sni: 1.2 - fixed some bugs and typos, add some output - Thank you!
# 2016-08-03, schoch: 1.1 - changes for new apache version 2.4.x
# 2016-01-##, schoch: 1.0 - Init...
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long qw(:config bundling); # insert debug for much more infos ;)
# define constants
use constant {
VERSION => '1.0',
# a simple inf variant - works for me ;-)
MAXINT => ~0,
NEGMAXINT => -1 * ~0,
STAT_OK => 0,
STAT_WARNING => 1,
STAT_CRITICAL => 2,
STAT_UNKNOWN => 3
};
# check arguments
my $options = { # define defaults here
'hostname' => 'localhost',
'verbose' => 0,
'wget' => '/usr/bin/wget',
'wget-options' => '-q --no-check-certificate -O-',
};
my $goodOpt = GetOptions(
'v+' => \$options->{'verbose'},
'verbose+' => \$options->{'verbose'},
'V' => \$options->{'version'},
'h' => \$options->{'help'},
'version' => \$options->{'version'},
'help' => \$options->{'help'},
'H=s' => \$options->{'hostname'},
'hostname=s' => \$options->{'hostname'},
'wc=s' => \@{$options->{'warncrit'}},
'warncrit=s' => \@{$options->{'warncrit'}},
'wget' => \$options->{'wget'},
'woptions=s' => \$options->{'wget-options'},
'u=s' => \$options->{'url'},
'url=s' => \$options->{'url'},
);
helpShort() unless $goodOpt;
helpLong() if $options->{'help'};
if($options->{'version'}) {
print 'Version: ', VERSION, "\n";
exit STAT_UNKNOWN;
}
print Data::Dumper->Dump([$options], ['options'])
if $options->{'verbose'};
# warncrit - get start and end for each pair
my $warncrit = {};
foreach my $item (@{$options->{'warncrit'}}) {
if($item =~ m/^([^,]+),([^,]+),([^,]+)$/o) {
$warncrit->{$1} = {'w' => $2, 'c' => $3};
} else {
mydie('Don\'t understand ' . $item);
}
}
print Data::Dumper->Dump([$warncrit], ['warncrit'])
if $options->{'verbose'};
# read stdin complete
local $/;
# which url to use? --url can overwrite the auto-creation
my $url = $options->{'url'}
? $options->{'url'}
: 'http://' . $options->{'hostname'} . '/server-status?auto';
printf "Url: %s\n", $url
if $options->{'verbose'};
# open server info
open PH, sprintf('%s %s %s |',
$options->{'wget'},
$options->{'wget-options'},
$url
) or mydie('Can not open server-status: ' . $!);
# read and cut data
my %lineData = map { (split /:\s*/)[0..1] } split /\n/, <PH>;
close PH;
print Data::Dumper->Dump([\%lineData], ['server-status'])
if $options->{'verbose'};
# Search for "Scoreboard" and analyze...
my $data = {};
if(exists $lineData{'Scoreboard'}) {
$data->{$1}++ while $lineData{'Scoreboard'} =~ m/(.)/og;
} else {
# Not found...
print 'No useful data found';
exit STAT_UNKNOWN;
}
print Data::Dumper->Dump([$data], ['scoreboard'])
if $options->{'verbose'};
# Sum up Scoreboard entries
my $sum = 0;
foreach(keys %$data) {
$sum += $data->{$_};
}
# print result
my $result = $lineData{'ServerMPM'} ? sprintf('MPM=%s', $lineData{'ServerMPM'}) : '';
my $perfData = '';
my @statList = qw(_ S R W K D C L G I .);
my $stats = {
'_' => 'Wait',
'S' => 'Start',
'R' => 'Read',
'W' => 'Send',
'K' => 'Keepalive',
'D' => 'DNS',
'C' => 'Close',
'L' => 'Logging',
'G' => 'Graceful',
'I' => 'Idle',
'.' => 'Free'
};
foreach my $item (@statList) {
$result .= ', ' if $result;
$perfData .= ' ' if $perfData;
$result .= sprintf '%s=%d', $stats->{$item}, ($data->{$item} or 0);
$perfData .= sprintf '%s=%d', $stats->{$item}, ($data->{$item} or 0);
}
$result .= ' ===> Total=' . $sum;
# add server rates - if exisiting (apache => 2.4)
if(
exists $lineData{'ReqPerSec'}
and exists $lineData{'BytesPerSec'}
and exists $lineData{'BytesPerReq'}
) {
$result .= sprintf ' RATES %s=%s, %s=%s, %s=%s',
(map { $_, $lineData{$_} } qw(ReqPerSec BytesPerSec BytesPerReq));
$perfData .= sprintf ' %s=%s %s=%s %s=%s',
(map { $_, $lineData{$_} } qw(ReqPerSec BytesPerSec BytesPerReq));
$perfData .= sprintf ' Total_Accesses=%sc', $lineData{"Total Accesses"};
$perfData .= sprintf ' Total_kBytes=%s', $lineData{"Total kBytes"};
}
# check for warning and critical
my $status = STAT_OK;
foreach my $field (keys %$warncrit) {
printf "checking warn/crit for \"%s\"...\n", $field
if $options->{'verbose'};
# value = if one letter scoreboard, else one of lineData (0 if not found)
my $fieldValue =
$field =~ m/^.$/o ? $data->{$field} || 0 : $lineData{$field} || 0;
printf " value: \"%s\"\n", $fieldValue
if $options->{'verbose'};
my $fieldStatus = checkStatus($fieldValue, $warncrit->{$field});
printf " result: %d\n", $fieldStatus
if $options->{'verbose'};
# last if CRITICAL, save WARNING, ignore OK
if($fieldStatus == STAT_CRITICAL) {
$status = STAT_CRITICAL;
last;
} elsif($fieldStatus == STAT_WARNING) {
$status = STAT_WARNING;
}
}
printf "Check overall status: %d\n", $status
if $options->{'verbose'};
# print result
printf "APACHE SERVER STATUS %s - %s|%s\n",
$status == 0 ? 'OK' : $status == 1
? 'WARNING' : $status == 2
? 'CRITICAL' : 'UNKNOWN',
$result, $perfData;
exit $status;
########### Functions #########################################################
# short help
sub helpShort {
print 'check_apache_serverstatus.pl -H <ip address> [-h] [-v]', "\n",
'[--wc=<field,warning,critical>] [--wget=<path to wget>] ', "\n",
'[--woption=<aditional wget options>] [-u <alternative url>]', "\n";
exit STAT_UNKNOWN;
}
# long help
sub helpLong {
print 'check_apache_serverstatus.pl (', VERSION, ")\n",
'Steffen Schoch <schoch@dsb-its.net>', "\n", "\n",
<<END;
check_apache_serverstatus.pl -H <ip address> [-h] [-v]
[--wc=<field,warning,critical>] [--wget=<path to wget>]
[--woption=<aditional wget options>] [-u <alternative url>]
Check apache server-status and builds performance data. Uses
wget to connect to the apache webserver.
Options:
-h, --help
Print help
-V, --version
Print version
-H, --hostname
Host name or IP address - will be used as
http://<hostname>/server-status. You can overwrite this
url by using -u/--url.
-v, --verbose
Be much more verbose.
--wget
Path to wget. Could also be used to use lynx or something
else instead of wget. Output must be send to stdout.
--woptions
Arguments passed to wget.
-u, --url
Use this url to connect to the apache server-status. Usefull
if the auto generated url out of the hostname is not correct.
--wc=<field,warning,critical>, --warncrit=<field,warning,critical>
Field could be any of the letters of the apache scoreboard or
of the other keys returned by server-status. Can be set multiple
times if you want to check more than one field.
END
exit STAT_UNKNOWN;
}
# die with STAT_UNKNOWN
sub mydie {
print @_, "\n";
exit STAT_UNKNOWN;
}
# checks if value is in defined limits for warning and critical
# see https://nagios-plugins.org/doc/guidelines.html for more details
# ARG1: value
# ARG2: hash with c and w limit
# RET: Nagios-State for this value
sub checkStatus {
my $value = shift;
my $limits = shift;
# first check critical - if not crit, then check warning. If not must be ok
for my $type (qw(c w)) {
printf " checking type %s = %s\n",
$type eq 'c' ? 'critcal' : 'warning',
$limits->{$type}
if $options->{'verbose'};
# Get min/max values, range is inside or outside?
my $inOrOut = 'out';
my $min;
my $max;
if($limits->{$type} =~ m/^(\@?)((~|\d*(\.\d+)?)?:)?(~|\d*(\.\d+)?)?$/o) {
# save min, max and inOrOut
$inOrOut = 'in' if $1;
$min = $3 || 0;
$max = $5 =~ m/^(.+)$/o ? $1 : MAXINT; # $max could be 0...
# neg infinity if ~
($min, $max) = map { $_ eq '~' ? NEGMAXINT : $_ } ($min, $max);
} else {
# Don't understand...
myexit('--> Strange range found: ', $limits->{$type});
}
printf " inside or outside: %s min: %s max: %s\n",
$inOrOut, $min, $max
if $options->{'verbose'};
# check for value outside range. Break if match, else check for inside.
if($inOrOut eq 'out') {
if(!($min < $value && $value < $max)) {
return $type eq 'c' ? STAT_CRITICAL : STAT_WARNING;
}
} elsif($inOrOut eq 'in') {
if($min <= $value && $value <= $max) {
return $type eq 'c' ? STAT_CRITICAL : STAT_WARNING;
}
}
}
# must be OK...
return STAT_OK;
}