/[nagios-plugins-perl]/trunk/plugins/check_apachestatus_auto.pl
ViewVC logotype

Contents of /trunk/plugins/check_apachestatus_auto.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 193 - (show annotations) (download)
Mon Dec 18 14:45:38 2017 UTC (2 years, 11 months ago) by racvision
File MIME type: text/plain
File size: 10603 byte(s)
Remise en ordre XH 201712
1 #!/usr/bin/perl -w
2 ####################### check_apachestatus_auto.pl #######################
3 # Version : 1.2
4 # Date : 14 Jul 2009
5 # Author : Dennis D. Spreen (dennis at spreendigital.de)
6 # Based on check_apachestatus.pl v1.4 by
7 # De Bodt Lieven (Lieven dot DeBodt at gmail.com)
8 # Updated by
9 # Karsten Behrens (karsten at behrens dot in)
10 # Geoff McQueen (geoff dot mcqueen at hiivesystems dot com )
11 # Dave Steinberg (dave at redterror dot net)
12 # Updated by
13 # Gerhard Lausser (gerhard dot lausser at consol dot de)
14 # Licence : GPL - http://www.fsf.org/licenses/gpl.txt
15 #############################################################
16 #
17 # help : ./check_apachestatus_auto.pl -h
18 #
19 # V1.0 Inital Release
20 # V1.1 Works with lighttpd server-status as well, added accesses perfdata
21 # V1.2 Updated perf data to be PNP compliant, added proxy option (Gerhard Lausser)
22
23 use strict;
24 use Getopt::Long;
25 use LWP::UserAgent;
26 use Time::HiRes qw(gettimeofday tv_interval);
27 use Digest::MD5 qw(md5 md5_hex);
28
29 use Nagios::Plugin;
30
31 # Nagios specific
32
33 # use lib "/usr/lib/nagios/plugins";
34 # use utils qw(%ERRORS $TIMEOUT);
35 # my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
36
37
38 # Globals
39
40 my $Version='1.2';
41 my $Name=$0;
42
43 my $o_host = undef; # hostname
44 my $o_help= undef; # want some help ?
45 my $o_port = undef; # port
46 my $o_url = ''; # url to use, if not the default
47 my $o_user = undef; # user for auth
48 my $o_pass = ''; # password for auth
49 my $o_realm = ''; # password for auth
50 my $o_version= undef; # print version
51 my $o_warn_level= undef; # Number of available slots that will cause a warning
52 my $o_crit_level= undef; # Number of available slots that will cause an error
53 my $o_timeout= 15; # Default 15s Timeout
54 my $o_proxy= undef; # proxy-url for both http and https
55
56 my $TempPath = '/tmp/'; # temp path
57 my $MaxUptimeDif = 60*30; # Maximum uptime difference (seconds), default 30 minutes
58
59 # functions
60
61 sub show_versioninfo { print "$Name version : $Version\n"; }
62
63 sub print_usage {
64 print "Usage: $Name -H <host> [-p <port>] [-t <timeout>] [-w <warn_level> -c <crit_level>] [-V] [-u <url>] [-U user -P pass -r realm]\n";
65 }
66
67 # Get the alarm signal
68 $SIG{'ALRM'} = sub {
69 print ("ERROR: Alarm signal (Nagios time-out)\n");
70 exit CRITICAL;
71 };
72
73 sub help {
74 print "Apache Monitor for Nagios version ",$Version,"\n";
75 print "GPL licence, (c)2009 Dennis D. Spreen\n\n";
76 print_usage();
77 print <<EOT;
78 -h, --help
79 print this help message
80 -H, --hostname=HOST
81 name or IP address of host to check
82 -p, --port=PORT
83 Http port
84 -u, --url=URL
85 Specific URL to use, instead of the default http://hostname/server-status
86 -U, --user=user
87 Username for basic auth
88 -P, --pass=PASS
89 Password for basic auth
90 -r, --realm=REALM
91 Realm for basic auth
92 -X, --proxy=PROXY
93 Proxy-URL for http and https (mandatory)
94 -t, --timeout=INTEGER
95 timeout in seconds (Default: $o_timeout)
96 -w, --warn=MIN
97 number of available slots that will cause a warning
98 -1 for no warning
99 -c, --critical=MIN
100 number of available slots that will cause an error
101 -V, --version
102 prints version number
103 Note :
104 The script will return
105 * Without warn and critical options:
106 OK if we are able to connect to the apache server's status page,
107 CRITICAL if we aren't able to connect to the apache server's status page,,
108 * With warn and critical options:
109 OK if we are able to connect to the apache server's status page and #available slots > <warn_level>,
110 WARNING if we are able to connect to the apache server's status page and #available slots <= <warn_level>,
111 CRITICAL if we are able to connect to the apache server's status page and #available slots <= <crit_level>,
112 UNKNOWN if we aren't able to connect to the apache server's status page
113
114 EOT
115 }
116
117 sub check_options {
118 Getopt::Long::Configure ("bundling");
119 GetOptions(
120 'h' => \$o_help, 'help' => \$o_help,
121 'H:s' => \$o_host, 'hostname:s' => \$o_host,
122 'u:s' => \$o_url, 'url:s' => \$o_url,
123 'U:s' => \$o_user, 'user:s' => \$o_user,
124 'P:s' => \$o_pass, 'pass:s' => \$o_pass,
125 'r:s' => \$o_realm, 'realm:s' => \$o_realm,
126 'p:i' => \$o_port, 'port:i' => \$o_port,
127 'V' => \$o_version, 'version' => \$o_version,
128 'w:i' => \$o_warn_level, 'warn:i' => \$o_warn_level,
129 'c:i' => \$o_crit_level, 'critical:i' => \$o_crit_level,
130 't:i' => \$o_timeout, 'timeout:i' => \$o_timeout,
131 'X:s' => \$o_proxy, 'proxy:s' => \$o_proxy,
132
133 );
134
135
136 if (defined ($o_help)) { help(); exit UNKNOWN};
137 if (defined($o_version)) { show_versioninfo(); exit UNKNOWN};
138 if (((defined($o_warn_level) && !defined($o_crit_level)) ||
139 (!defined($o_warn_level) && defined($o_crit_level))) ||
140 ((defined($o_warn_level) && defined($o_crit_level)) &&
141 (($o_warn_level != -1) && ($o_warn_level <= $o_crit_level))
142 )
143 ) {
144 print "Check warn and crit!\n"; print_usage(); exit UNKNOWN
145 }
146 # Check compulsory attributes
147 if (!defined($o_host)) { print_usage(); exit UNKNOWN};
148 }
149
150 ########## MAIN ##########
151
152 check_options();
153
154 my $ua = LWP::UserAgent->new( protocols_allowed => ['http', 'https'], timeout => $o_timeout);
155 my $timing0 = [gettimeofday];
156 my $response = undef;
157 my $url = undef;
158 my $httpserver = 'APACHE'; #assume it is apache by default
159
160 if (($o_url =~ m/^http(s?)\:\/\//i) ){
161 $url = $o_url;
162 } else {
163 if (!defined($o_port)) {
164 $url = 'http://' . $o_host . $o_url.'/server-status?auto';
165 } else {
166 $url = 'http://' . $o_host . ':' . $o_port . $o_url.'/server-status?auto';
167 }
168 }
169
170 my $req = HTTP::Request->new( GET => $url );
171 if (defined($o_user)) {
172 $req->authorization_basic($o_user, $o_pass);
173 }
174
175 if (defined($o_proxy)) {
176 if ($url =~ /^https/) {
177 if ($o_proxy =~ /^http:\/\/(.*?)\/?$/) {
178 $o_proxy = $1;
179 }
180 $ENV{HTTPS_PROXY} = $o_proxy;
181 } else {
182 $ua->proxy(['http'], $o_proxy);
183 }
184 }
185
186 $response = $ua->request($req);
187 my $timeelapsed = tv_interval ($timing0, [gettimeofday]);
188
189 my $webcontent = undef;
190 if ($response->is_success) {
191 $webcontent=$response->content;
192
193 my $Uptime = 0;
194 if($webcontent =~ m/Uptime: (.*?)\n/) {
195 $Uptime = $1;
196 }
197
198 my $TotalAccesses = 0;
199 if($webcontent =~ m/Total Accesses: (.*?)\n/) {
200 $TotalAccesses = $1;
201 }
202
203 my $TotalKbytes = 0;
204 if($webcontent =~ m/Total kBytes: (.*?)\n/) {
205 $TotalKbytes = $1;
206 }
207
208 my $ScoreBoard = '';
209 if($webcontent =~ m/Scoreboard: (.*?)\n/) {
210 $ScoreBoard = $1;
211 }
212
213 my $BusyWorkers= 0;
214 if($webcontent =~ m/(BusyWorkers|BusyServers): (.*?)\n/) {
215 $BusyWorkers = $2;
216 if ($1 eq 'BusyServers') {
217 $httpserver = 'LIGHTTPD';
218 }
219 }
220
221 my $IdleWorkers=0;
222 if($webcontent =~ m/(IdleWorkers|IdleServers): (.*?)\n/) {
223 $IdleWorkers = $2;
224 }
225
226 my $TempFile = $TempPath.$o_host.'_check_apachestatus_auto'.md5_hex($url);
227
228 my $LastUptime = 0;
229 my $LastTotalAccesses = 0;
230 my $LastTotalKbytes = 0;
231
232 if ((-e $TempFile) && (-r $TempFile) && (-w $TempFile))
233 {
234 open (FH, '<',$TempFile) or exit CRITICAL;
235 $LastUptime = <FH>;
236 $LastTotalAccesses = <FH>;
237 $LastTotalKbytes = <FH>;
238 close (FH);
239 }
240
241 open (FH, '>'.$TempFile) or exit CRITICAL;
242 print FH "$Uptime\n";
243 print FH "$TotalAccesses\n";
244 print FH "$TotalKbytes\n";
245 close (FH);
246
247 my $ReqPerSec = 0;
248 my $BytesPerReq = 0;
249 my $BytesPerSec = 0;
250 my $Accesses = 0;
251
252 if (($Uptime>$LastUptime) && ($Uptime-$LastUptime<$MaxUptimeDif) && ($TotalAccesses>=$LastTotalAccesses) && ($TotalKbytes>=$LastTotalKbytes)) {
253 $ReqPerSec = ($TotalAccesses-$LastTotalAccesses)/($Uptime-$LastUptime);
254 $BytesPerSec = (($TotalKbytes-$LastTotalKbytes)*1024)/($Uptime-$LastUptime);
255
256 $Accesses = ($TotalAccesses-$LastTotalAccesses);
257
258 if ($TotalAccesses>$LastTotalAccesses) {
259 $BytesPerReq = (($TotalKbytes-$LastTotalKbytes)*1024)/($TotalAccesses-$LastTotalAccesses);
260 }
261 }
262
263 my $CountOpenSlots = ($ScoreBoard =~ tr/\.//);
264 my $TotalSlots = $CountOpenSlots+$IdleWorkers+$BusyWorkers;
265 my $InfoData = '';
266 my $PerfData = '';
267
268
269 if ($httpserver eq 'APACHE') {
270 $InfoData = sprintf ("- %.3f sec. response time, Busy/Idle %d/%d, open %d/%d, ReqPerSec %.1f, BytesPerReq %d, ".
271 "BytesPerSec %d", $timeelapsed, $BusyWorkers, $IdleWorkers, $CountOpenSlots, $TotalSlots,
272 $ReqPerSec, $BytesPerReq, $BytesPerSec);
273
274 $PerfData = sprintf ("Idle=%d Busy=%d OpenSlots=%d Slots=%d Starting=%d Reading=%d Sending=%d Keepalive=%d ".
275 "DNS=%d Closing=%d Logging=%d Finishing=%d ReqPerSec=%f BytesPerReq=%d BytesPerSec=%f Accesses=%d",
276 ($IdleWorkers),($BusyWorkers),($CountOpenSlots),($TotalSlots),($ScoreBoard =~ tr/S//),
277 ($ScoreBoard =~ tr/R//),($ScoreBoard =~ tr/W//),($ScoreBoard =~ tr/K//),($ScoreBoard =~ tr/D//),
278 ($ScoreBoard =~ tr/C//),($ScoreBoard =~ tr/L//),($ScoreBoard =~ tr/G//),
279 $ReqPerSec, $BytesPerReq,$BytesPerSec,$Accesses);
280 } else {
281 $InfoData = sprintf ("- %.3f sec. response time, Busy/Idle %d/%d, slots %d, ReqPerSec %.1f, BytesPerReq %d, ".
282 "BytesPerSec %d", $timeelapsed, $BusyWorkers, $IdleWorkers, $TotalSlots,
283 $ReqPerSec, $BytesPerReq, $BytesPerSec);
284
285 $PerfData = sprintf ("Idle=%d Busy=%d Slots=%d ".
286 "Connect=%d Close=%d HardError=%d ".
287 "Read=%d ReadPost=%d Write=%d ".
288 "HandleRequest=%d RequestStart=%d ReqestEnd=%d ".
289 "ResponseStart=%d ResponseEnd=%d ".
290 "ReqPerSec=%f BytesPerReq=%d BytesPerSec=%f Accesses=%d",
291 ($IdleWorkers),($BusyWorkers),($TotalSlots),
292 ($ScoreBoard =~ tr/\.//),($ScoreBoard =~ tr/C//),($ScoreBoard =~ tr/E//),
293 ($ScoreBoard =~ tr/r//),($ScoreBoard =~ tr/R//),($ScoreBoard =~ tr/W//),
294 ($ScoreBoard =~ tr/h//),($ScoreBoard =~ tr/q//),($ScoreBoard =~ tr/q//),
295 ($ScoreBoard =~ tr/s//),($ScoreBoard =~ tr/s//),
296 $ReqPerSec, $BytesPerReq,$BytesPerSec,$Accesses);
297 }
298
299
300
301 if (defined($o_crit_level) && ($o_crit_level != -1)) {
302 if (($CountOpenSlots + $IdleWorkers) <= $o_crit_level) {
303 print($httpserver." CRITICAL ".$InfoData.'|'.$PerfData);
304 exit CRITICAL
305 }
306 }
307 if (defined($o_warn_level) && ($o_warn_level != -1)) {
308 if (($CountOpenSlots + $IdleWorkers) <= $o_warn_level) {
309 print($httpserver." WARNING ".$InfoData.'|'.$PerfData);
310 exit WARNING;
311 }
312 }
313
314 print($httpserver." OK ".$InfoData.'|'.$PerfData);
315 exit OK;
316
317 } else {
318 if (defined($o_warn_level) || defined($o_crit_level)) {
319 printf("UNKNOWN %s\n", $response->status_line);
320 exit UNKNOWN;
321
322 } else {
323 printf("CRITICAL %s\n", $response->status_line);
324 exit CRITICAL;
325 }
326 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.8