/usr/lib/nagios/plugins/check_httpd_status is in nagios-plugins-contrib 14.20141104.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 | #!/usr/bin/perl -w
# nagios: -epn
#
####################### check_httpd_status.pl #######################
# Version : 1.3
# Date : 06 Aug 2010
#
# V1.3 Rewrite using Nagios::Plugin, rename check_httpd_status.pl (Stéphane Urbanovski)
# V1.2 Updated perf data to be PNP compliant, added proxy option (Gerhard Lausser)
# V1.1 Works with lighttpd server-status as well, added accesses perfdata
# V1.0 Inital Release
#
#
# Authors : Dennis D. Spreen (dennis at spreendigital.de)
# Based on check_apachestatus.pl v1.4 by
# De Bodt Lieven (Lieven dot DeBodt at gmail.com)
# Updated by
# Karsten Behrens (karsten at behrens dot in)
# Geoff McQueen (geoff dot mcqueen at hiivesystems dot com )
# Dave Steinberg (dave at redterror dot net)
# Updated by
# Gerhard Lausser (gerhard dot lausser at consol dot de)
# Licence : GPL - http://www.fsf.org/licenses/gpl.txt
#############################################################
#
#
use strict;
use warnings;
#use Data::Dumper;
use File::Basename; # get basename()
use POSIX qw(setlocale);
use Locale::gettext;
use Nagios::Plugin ;
use LWP::UserAgent;
use HTTP::Status; # get status_message()
use Time::HiRes qw(gettimeofday tv_interval);
use Digest::MD5 qw(md5 md5_hex);
# Globals
my $VERSION = '1.3';
my $TIMEOUT = 10;
my $PROGNAME = basename($0);
# my $PROGNAME = 'check_httpd_status.pl';
# Retention files path (save previous values) :
# FIXME: Make this configurable
my $TempPath = '/tmp/';
# Check freshness og previous values
my $MaxUptimeDif = 60*30; # Maximum uptime difference (seconds), default 30 minutes
# i18n :
setlocale(LC_MESSAGES, '');
textdomain('nagios-plugins-perl');
# Translate Apache / lighthttpd scoreboard status
my %TranslationTable = (
'APACHE' => {
'.' => 'OpenSLot',
'_' => 'Waiting',
'I' => 'Idle',
'S' => 'Starting',
'R' => 'Reading',
'W' => 'Sending',
'K' => 'Keepalive',
'D' => 'DNS',
'C' => 'Closing',
'L' => 'Logging',
'G' => 'Finishing',
},
'LIGHTTPD' => {
'.' => 'Connect',
'C' => 'Close',
'E' => 'HardError',
'r' => 'Read',
'R' => 'ReadPost',
'W' => 'Write',
'h' => 'HandleRequest',
'q' => 'RequestStart',
'Q' => 'ReqestEnd',
's' => 'ResponseStart',
'S' => 'ResponseEnd',
},
);
my $np = Nagios::Plugin->new(
version => $VERSION,
blurb => _gt('Apache / Lighthttpd server status monitor for Nagios'),
usage => "Usage: %s [ -H <host> [-p <port>] [-t <timeout>] [-w <warn_level> -c <crit_level>] [-V] [-u <url>] [-U user -P pass -r realm]",
extra => &showExtra(),
timeout => $TIMEOUT+1
);
$np->add_arg (
spec => 'hostname|H=s',
help => _gt('Name or IP address of host to check'),
required => 1,
);
$np->add_arg (
spec => 'port|p=i',
help => _gt('Http port'),
);
$np->add_arg (
spec => 'url|u=s',
help => _gt('Specific URL to use, instead of the default http://<hostname>/server-status?auto'),
default => '/server-status?auto',
);
$np->add_arg (
spec => 'user|U=s',
help => _gt('Username for basic auth'),
);
$np->add_arg (
spec => 'pass|P=s',
help => _gt('Password for basic auth'),
);
$np->add_arg (
spec => 'realm|r=s',
help => _gt('Realm for basic auth'),
);
$np->add_arg (
spec => 'proxy|X=s',
help => _gt('Proxy-URL for http and https (mandatory)'),
);
$np->add_arg (
spec => 'warn|w=s',
help => _gt('Number of available slots that will cause a warning (Range format).'),
);
$np->add_arg (
spec => 'crit|c=s',
help => _gt('Number of available slots that will cause an error (Range format).'),
);
$np->getopts;
my $o_host = $np->opts->get('hostname');
my $o_port = $np->opts->get('port');
my $o_url = $np->opts->get('url');
my $o_user = $np->opts->get('user');
my $o_pass = $np->opts->get('pass');
my $o_realm = $np->opts->get('realm');
my $o_proxy = $np->opts->get('proxy');
my $o_warn_level = $np->opts->get('warn');
my $o_crit_level = $np->opts->get('crit');
# if (((defined($o_warn_level) && !defined($o_crit_level)) ||
# (!defined($o_warn_level) && defined($o_crit_level))) ||
# ((defined($o_warn_level) && defined($o_crit_level)) && (($o_warn_level != -1) && ($o_warn_level <= $o_crit_level)))
# ) {
# $np->nagios_exit(UNKNOWN, _gt("Check warn and crit!") );
# }
my $o_proto = 'http';
my $url = undef;
my $httpserver = 'APACHE'; #assume it is apache by default
if (($o_url =~ m/^http(s?)\:\/\//i) ){
$url = $o_url;
if ($1 eq 's') {
$o_proto = 'https';
}
} else {
$url = $o_proto.'://' . $o_host;
if ( defined($o_port)) {
$url .= ':' . $o_port;
}
if ( $o_url !~ /^\// ) {
$url .= '/';
}
$url .= $o_url;
}
if ( $url !~ /\?auto$/ ) {
$url .= '/server-status?auto';
}
my $ua = LWP::UserAgent->new( protocols_allowed => ['http', 'https'], timeout => $TIMEOUT);
$ua->agent($PROGNAME.'-'.$VERSION);
logD("Web URL : $url");
my $req = HTTP::Request->new( GET => $url );
if ( defined($o_user) ) {
$req->authorization_basic($o_user, $o_pass);
}
if ( defined($o_proxy) ) {
if ($o_proto eq 'https') {
if ($o_proxy =~ /^http:\/\/(.*?)\/?$/) {
$o_proxy = $1;
}
$ENV{HTTPS_PROXY} = $o_proxy;
} else {
$ua->proxy(['http'], $o_proxy);
}
}
my $timing0 = [gettimeofday];
my $response = $ua->request($req);
my $timeelapsed = tv_interval($timing0, [gettimeofday]);
if ( $response->is_error() ) {
my $err = $response->code." ".status_message($response->code)." (".$response->message.")";
my $status = CRITICAL;
$np->add_message(CRITICAL, );
if (defined($o_warn_level) || defined($o_crit_level)) {
$status = UNKNOWN;
}
$np->nagios_exit($status, _gt("HTTP error: ").$err );
} elsif ( ! $response->is_success() ) {
my $err = $response->code." ".status_message($response->code)." (".$response->message.")";
$np->add_message(CRITICAL, _gt("Internal error: ").$err );
}
my $webcontent = $response->content;
logD("Web content :\n----------------------------\n".$webcontent."\n----------------------------");
my $patternFound = 0;
my $Uptime = 0;
if ( $webcontent =~ m/Uptime: (.*?)\n/) {
$Uptime = $1;
$patternFound++;
}
my $TotalAccesses = 0;
if ( $webcontent =~ m/Total Accesses: (.*?)\n/) {
$TotalAccesses = $1;
$patternFound++;
} else {
$np->add_message(WARNING, _gt('"Total Accesses" not set ! (need extented status ?)') );
}
my $TotalKbytes = 0;
if ( $webcontent =~ m/Total kBytes: (.*?)\n/) {
$TotalKbytes = $1;
$patternFound++;
}
my $ScoreBoard = '';
if ( $webcontent =~ m/Scoreboard: (.*?)\n/) {
$ScoreBoard = $1;
$patternFound++;
} else {
$np->add_message(WARNING, _gt("Scoreboard not found in reponse !") );
}
my $BusyWorkers = 0;
if ( $webcontent =~ m/(BusyWorkers|BusyServers): (.*?)\n/) {
$BusyWorkers = $2;
$patternFound++;
if ($1 eq 'BusyServers') {
$httpserver = 'LIGHTTPD';
}
}
my $IdleWorkers = 0;
if ( $webcontent =~ m/(IdleWorkers|IdleServers): (.*?)\n/) {
$IdleWorkers = $2;
$patternFound++;
}
if ( $patternFound <= 1) {
$np->nagios_exit(CRITICAL, _gt("Server-status informations not found !") );
}
my $TempFile = $TempPath.$o_host.'_check_httpd_status'.md5_hex($url);
my $LastUptime = 0;
my $LastTotalAccesses = 0;
my $LastTotalKbytes = 0;
if ((-e $TempFile) && (-r $TempFile) && (-w $TempFile)) {
if ( !open (RETENTION_FILE, '<',$TempFile) ) {
$np->nagios_exit(CRITICAL, sprintf(_gt('Error while trying to read %s !'),$TempFile) );
}
$LastUptime = <RETENTION_FILE>;
$LastTotalAccesses = <RETENTION_FILE>;
$LastTotalKbytes = <RETENTION_FILE>;
close (RETENTION_FILE);
chomp($LastUptime);
chomp($LastTotalAccesses);
chomp($LastTotalKbytes);
logD("LastUptime=$LastUptime LastTotalAccesses=$LastTotalAccesses LastTotalKbytes=$LastTotalKbytes (from $TempFile)");
} else {
logD("Retention file '$TempFile' not found");
}
if ( !open (RETENTION_FILE, '>',$TempFile) ) {
$np->nagios_exit(CRITICAL, sprintf(_gt('Error while trying to write to %s !'),$TempFile) );
}
print RETENTION_FILE "$Uptime\n";
print RETENTION_FILE "$TotalAccesses\n";
print RETENTION_FILE "$TotalKbytes\n";
close (RETENTION_FILE);
my $ReqPerSec = 0;
my $BytesPerReq = 0;
my $BytesPerSec = 0;
my $DiffTime = $Uptime-$LastUptime;
if ( ($DiffTime > 0) && ($DiffTime < $MaxUptimeDif) && ($TotalAccesses >= $LastTotalAccesses) && ($TotalKbytes >= $LastTotalKbytes) ) {
$ReqPerSec = ($TotalAccesses-$LastTotalAccesses)/$DiffTime;
$np->add_perfdata(
'label' => 'ReqPerSec',
'value' => sprintf('%.3f',$ReqPerSec),
'uom' => 'req/s',
);
$BytesPerSec = (($TotalKbytes-$LastTotalKbytes)*1024)/$DiffTime;
$np->add_perfdata(
'label' => 'BytesPerSec',
'value' => sprintf('%.2f',$BytesPerSec),
'uom' => 'B/s',
);
my $Accesses = ($TotalAccesses-$LastTotalAccesses);
if ( $Accesses > 0 ) {
$BytesPerReq = (($TotalKbytes-$LastTotalKbytes)*1024)/$Accesses;
$np->add_perfdata(
'label' => 'BytesPerReq',
'value' => sprintf('%.2f',$BytesPerReq),
'uom' => 'B/req',
);
}
}
my $CountOpenSlots = ($ScoreBoard =~ tr/\.//);
my $TotalSlots = $CountOpenSlots+$IdleWorkers+$BusyWorkers;
my $InfoData = '';
my %WorkerStates = ();
map( $WorkerStates{$_}++ , split(//,$ScoreBoard) );
foreach my $slotState ( sort(keys(%{$TranslationTable{$httpserver}})) ) {
my $val = 0 ;
if ( defined($WorkerStates{$slotState}) ) {
$val = $WorkerStates{$slotState};
}
$np->add_perfdata(
'label' => $TranslationTable{$httpserver}{$slotState},
'value' => $val,
);
}
if ( $httpserver eq 'APACHE' ) {
$InfoData = sprintf ("%.3f s - %d/%d Busy/Idle, %d/%d Open/Total, %.1f requests/s, %.1f B/s, %d B/request",
$timeelapsed, $BusyWorkers, $IdleWorkers, $CountOpenSlots, $TotalSlots, $ReqPerSec, $BytesPerSec, $BytesPerReq);
} else {
$InfoData = sprintf ("%.3f sec. response time, Busy/Idle %d/%d, slots %d, ReqPerSec %.1f, BytesPerReq %d, BytesPerSec %d",
$timeelapsed, $BusyWorkers, $IdleWorkers, $TotalSlots, $ReqPerSec, $BytesPerReq, $BytesPerSec);
}
$np->add_message(OK, $InfoData);
my $fw = $CountOpenSlots + $IdleWorkers;
logD("FreeWorker = ".$fw);
my $tmp_status = $np->check_threshold(
check => $fw,
warning => $o_warn_level,
critical => $o_crit_level,
);
$np->add_perfdata(
'label' => 'FreeWorker',
'value' => $fw,
'min' => 0,
'threshold' => $np->threshold()
);
if ( $tmp_status ) {
$np->add_message($tmp_status, sprintf(_gt(" Not enough free worker (%d) !"),$fw) );
}
my ($status, $message) = $np->check_messages('join' => ' ');
$np->nagios_exit($status, $message );
sub logD {
print STDERR 'DEBUG: '.$_[0]."\n" if ($np->opts->verbose);
}
sub logW {
print STDERR 'WARNING: '.$_[0]."\n" if ($np->opts->verbose);
}
# Gettext wrapper
sub _gt {
return gettext($_[0]);
}
sub showExtra {
return <<EOT;
(c)2009 Dennis D. Spreen
(c)2010 Stéphane Urbanovski
Note :
This plugin check Apache or Lighthttpd server-status page (using mod_status). It require the ?auto option.
Warning and critical values follow Nagios::Plugins::Threshold format. See http://nagiosplug.sourceforge.net/developer-guidelines.html
Example (note the trailing colon):
check_httpd_status -H www.server.org -w 100: -c 10:
Warn if less than 100 workers are available
Crit if less than 10 workers are available
EOT
}
|