Cheers Ulrich,<br><br>That seems to work a treat for what im trying to check<br><br>Ben<br><br><div><span class="gmail_quote">On 3/15/06, <b class="gmail_sendername">Ulrich Wisser</b> <<a href="mailto:liste@publisher.de">
liste@publisher.de</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Hi,<br><br>but this is performance check only, no load check.
<br><br>Ulrich<br><br><br>#!/usr/bin/perl<br>#<br># (c) 2006 Ulrich Wisser, Relevant Traffic AB<br># <a href="mailto:ulrich.wisser@relevanttraffic.com">ulrich.wisser@relevanttraffic.com</a><br>#<br># Derived work from:
<br>#<br># (c)2001 Sebastian Hetze, Linux Information Systems AG<br># send bug reports to <<a href="mailto:S.Hetze@Linux-AG.com">S.Hetze@Linux-AG.com</a>><br>#<br># Some fixes by Rob.<br>#<br># This program is free software; you can redistribute it and/or
<br># modify it under the terms of the GNU General Public License<br># as published by the Free Software Foundation; version 2<br># of the License<br>#<br># This program is distributed in the hope that it will be useful,<br>
# but WITHOUT ANY WARRANTY; without even the implied warranty<br># of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br># GNU General Public License for more details.<br>#<br># you should have received a copy of the GNU General Public License
<br># along with this program (or with Nagios); if not, write to the<br># Free Software Foundation, Inc., 59 Temple Place - Suite 330,<br># Boston, MA 02111-1307, USA<br>#<br>#<br># Check apache status information provided by mod_status to find
<br># out about the load (number of servers working) and the<br># performance (average response time for recent requests).<br>#<br>use strict;<br>use LWP::UserAgent;<br>use URI::URL;<br>use Getopt::Long;<br>Getopt::Long::Configure('bundling');
<br>use vars qw($VERSION);<br><br>$VERSION=0.01;<br><br>my %ERRORS = ('UNKNOWN' , '-1',<br> 'OK' , '0',<br> 'WARNING', '1',<br> 'CRITICAL', '2');<br>#<br># global default values
<br>#<br>my $perf_w=500;<br>my $perf_c=1000;<br>my $load_w=80;<br>my $load_c=100;<br>my $timeout=15;<br>my $autostring="?auto";<br>#<br># get command line options the regular way<br>#<br>my ($opt_V, $opt_h, $opt_l, $opt_w, $opt_c, $opt_H, $opt_u, $verbose);
<br>GetOptions(<br> "V" => \$opt_V, "version" => \$opt_V,<br> "h" => \$opt_h, "help" => \$opt_h,<br> "v" => \$verbose, "verbose" => \$verbose,
<br> "w=s" => \$opt_w, "warning=s" => \$opt_w,<br> "c=s" => \$opt_c, "critical=s" => \$opt_c,<br> "H=s" => \$opt_H, "hostname=s" => \$opt_H,
<br> "u=s" => \$opt_u, "url=s" => \$opt_u<br>);<br><br>#<br># handle the verbose stuff first<br>#<br>if ($opt_V) {<br> print "\n";<br> print "check_apache_perf nagios plugin version $VERSION\n";
<br> print "\n";<br> print "The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n";<br> print "copies of the plugins under the terms of the GNU General Public License.\n";
<br> print "For more information about these matters, see the file named COPYING.\n";<br> print "\n";<br> exit $ERRORS{'UNKNOWN'};<br>}<br><br>if ($opt_h) {<br> print_help();<br> exit $ERRORS{'UNKNOWN'};
<br>}<br><br>#<br># now get options the weired way and set the defaults<br># if nothing else is provided<br>#<br>if (!$opt_H) {print_usage(); exit $ERRORS{'UNKNOWN'}; };<br>if (!$opt_u) {print_usage(); exit $ERRORS{'UNKNOWN'}; };
<br>my $warn = $1 if ($opt_w =~ /([0-9]+)/);<br>my $alert = $1 if ($opt_c =~ /([0-9]+)/);<br>if (!$warn) {print_usage(); exit $ERRORS{'UNKNOWN'}; };<br>if (!$alert) {print_usage(); exit $ERRORS{'UNKNOWN'}; };<br><br><br>
<br>#<br># dont let us wait forever...<br>#<br>$SIG{'ALRM'} = sub {<br> print ("ERROR: No response from HTTP server (alarm)n");<br> exit $ERRORS{"UNKNOWN"};<br>};<br>alarm($timeout);<br>#<br># now we set things up for the real work
<br># and fire up the request<br>#<br>my $ua = new LWP::UserAgent;<br>my $url = url("http://" . $opt_H . $opt_u . $autostring);<br>my $req = new HTTP::Request 'GET', $url;<br>my $res = $ua->request($req);<br>
#<br># hopefully we´ve got something usefull<br>#<br>if ($res->is_success) {<br> my ($accesses, $kbytes, $load, $uptime, $rps, $bps, $bpr, $busy, $idle, $performance, $score);<br> $performance = "| ";<br>
foreach $_ (split /^/m, $res->content) {<br> next if /^s*$/;<br> #<br> # this is the load checking section<br> # we parse the whole content, just in case someone<br> # wants to use this some day in the future
<br> #<br> if (/^Total Accesses:\s+([0-9.]+)/) { $accesses = $1; $performance .= "totalaccess=$accesses "; next; }<br> if (/^Total kBytes:\s+([0-9.]+)/) { $kbytes = $1; $performance .= "totalkB=".$kbytes." "; next; }
<br> if (/^CPULoad:\s+([0-9.]+)\s+/) { $load = $1; $performance .= "CPUload=".$load." "; next; }<br> if (/^CPULoad:\s+[0-9.]+e/) { $load = 'E'; $performance .= "CPUload=E "; next; }
<br> if (/^Uptime:\s+([0-9.]+)\s+/) { $uptime = $1; $performance .= "Uptime=$uptime "; next; }<br> if (/^ReqPerSec:\s+([0-9.]+)\s+/) { $rps = $1; $performance .= "ReqPerSec=".$rps." "; next; }
<br> if (/^ReqPerSec:\s+[0-9.]+e/) { $rps = 'E'; $performance .= "ReqPerSec=E "; next; }<br> if (/^BytesPerSec:\s+([0-9.]+)\s+/) { $bps = $1; $performance .= "kBytesPerSec=".($bps/1024)." "; next; }
<br> if (/^BytesPerSec:\s+[0-9.]+e/) { $bps = 'E'; $performance .= "kBytesPerSec=E "; next; }<br> if (/^BytesPerReq:\s+([0-9.]+)\s+/) { $bpr = $1; $performance .= "kBytesPerReq=".($bpr/1024)." "; next; }
<br> if (/^ReqPerSec:\s+[0-9.]+e/) { $bpr = 'E'; $performance .= "kBytesPerReq=E "; next; }<br> if (/^BusyServers:\s+([0-9.]+)\s+/) { $busy = $1; $performance .= "BusyServers=".$busy." "; next; }
<br> if (/^BusyWorkers:\s+([0-9.]+)\s+/) { $busy = $1; $performance .= "BusyServers=".$busy." "; next; }<br> if (/^IdleServers:\s+([0-9.]+)\s+/) { $idle = $1; $performance .= "IdleServers=".$idle." "; next; }
<br> if (/^IdleWorkers:\s+([0-9.]+)\s+/) { $idle = $1; $performance .= "IdleServers=".$idle." "; next; }<br> if (/^Scoreboard:\s+([CSRWKDLG_.]+)\s+/) { $score = $1; next; }
<br> print "Unknown Status: $_\n";<br> }<br> #<br> # now we even parse the whole scoreboard, just for fun<br> #<br> my %scores;<br> $scores{'.'} = 0; # Unused<br> $scores{'_'} = 0; # Waiting
<br> $scores{'S'} = 0; # Starting<br> $scores{'R'} = 0; # Reading<br> $scores{'W'} = 0; # Writing<br> $scores{'K'} = 0; # Keepalive<br> $scores{'D'} = 0; # DNS Lookup<br> $scores{'L'} = 0; # Logging<br> $scores{'G'} = 0; # Going
<br> $scores{'C'} = 0; # Closing<br> foreach my $scorepoint (split //m, $score) { $scores{$scorepoint} += 1; }<br> $performance .= "unused=$scores{'.'} wait=$scores{'_'} start=$scores{'S'} read=$scores{'R'} write=$scores{'W'} keepalive=$scores{'K'} dns=$scores{'D'} log=$scores{'L'} go=$scores{'G'} close=$scores{'C'}";
<br> #<br> # compute return code<br> #<br> my $status = 'OK';<br> $status = 'WARNING' if($busy > $warn);<br> $status = 'CRITICAL' if($busy > $alert);<br> print "HTTPD $status: servers running $busy, idle $idle $performance\n";
<br> exit $ERRORS{$status};<br>} else {<br> print "HTTP request failed\n";<br> exit $ERRORS{"CRITICAL"};<br>}<br>#<br># ok, now we are almost through<br># These last subroutines do the things for those that do not
<br># read source code.<br>#<br>sub print_usage () {<br> print "Usage: $0 [-hV] -H <host> -u <url> -w <warn> -c <crit>\n";<br>}<br><br>sub print_help () {<br> print "\n";<br>
print "\n";<br> print "check_apache nagios plugin version $VERSION\n";<br> print "\n";<br> print "The nagios plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n";
<br> print "copies of the plugins under the terms of the GNU General Public License.\n";<br> print "For more information about these matters, see the file named COPYING.\n";<br> print "\n";
<br> print "Copyright (c) 2001 Sebastian Hetze Linux Information Systems AG\n";<br> print "\n";<br> print "\n";<br> print "This plugin checks the apache HTTP service on the specified host.\n";
<br> print "It uses the mod_status facilities provided by the apache server.\n";<br> print "The monitoring server must be authorized in httpd.conf.\n";<br> print "\n";<br> print "\n";
<br> print_usage();<br> print "\n";<br> print "Options:\n";<br> print " -c, --critical=INTEGER\n";<br> print " performance level at which a critical message will be gererated.\n";
<br> print " -H, --hostname=ADDRESS\n";<br> print " host name argument for server.\n";<br> print " -h, --help\n";<br> print " print detailed help screen.\n";<br> print " -u, --url=PATH\n";
<br> print " location to call mod_status.\n";<br> print " -V, --version\n";<br> print " print version information.\n";<br> print " -w, --warning=INTEGER\n";<br> print " performance level at which a warning message will be gererated.\n";
<br> print "\n";<br> print "\n";<br>}<br>#<br># the end<br>#<br><br><br></blockquote></div><br><br clear="all"><br>-- <br>"A Scientist will earn a living by taking a really difficult problem and spends many years solving it, an engineer earns a living by finding really difficult problems and side stepping them"