#! /usr/bin/perl -w
#
# $Id: check_frontpage 1112 2005-01-27 04:46:08Z stanleyhopcroft $
#
# Check that FrontPage extensions appear to be working on a specified host.
# Currently only checks that the hit counter is not returning an error.
#
# Probably not a good idea to use this on a host that someone's counting
# the hits on, so create a separate vhost for frontpage extensions testing,
# or just install the extensions on the default/root host for your server, and
# point it against that hostname, running it against all vhosts on a server is
# probably rather wasteful.
#
# Kev Green, oRe Net (http://www.orenet.co.uk/).


use strict;
use lib "/usr/lib/nagios/plugins";
use utils qw($TIMEOUT %ERRORS &print_revision &support);
use vars qw($PROGNAME);
use Getopt::Long;
use LWP;
use vars qw($opt_V $opt_h $verbose $opt_w $opt_c $opt_H);
my ($tt,$url,$response,$stime, $etime,$warning,$critical,$mimetype,$failtype,$temp,$message);
my $rt = 0;

$PROGNAME = "check_frontpage";
sub print_help ();
sub print_usage ();

$ENV{'PATH'}='';
$ENV{'BASH_ENV'}='';
$ENV{'ENV'}='';

Getopt::Long::Configure('bundling');
GetOptions
	("V"   => \$opt_V, "version"    => \$opt_V,
	 "h"   => \$opt_h, "help"       => \$opt_h,
	 "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);

if ($opt_V) {
	print_revision($PROGNAME,'$Revision: 1112 $'); #'
	exit $ERRORS{'OK'};
}

if ($opt_h) {
	print_help();
	exit $ERRORS{'OK'};
}

$opt_H = shift unless ($opt_H);
print_usage() unless $opt_H;
my $host = $1 if ($opt_H =~ m/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|[a-zA-Z][-a-zA-Z0-9]+(\.[a-zA-Z0-9][-a-zA-Z0-9]+)*)$/);
print_usage() unless $host;

($opt_c) || ($opt_c = shift) || ($opt_c = 120);
if ($opt_c =~ /([0-9]+)/) {
   $critical = $1;
} else {
   $critical = 10;
}

($opt_w) || ($opt_w = shift) || ($opt_w = 60);
if ($opt_w =~ /([0-9]+)/) {
   $warning = $1;
} else {
   $warning = 5;
}

# Guts go here, once we're through argument parsing and have warning and
# critical thresholds.
my $browser = LWP::UserAgent->new;

my @urls = ( 
 # This is the "Hit Counter", which continues to work if frontpage extensions
 # are 'uninstall'ed from the site, but not when they are 'fulluninstall'ed.
 {
   url => "_vti_bin/fpcount.exe?Page=_borders/right.htm|Image=4",
   mimetype => "image/gif",
   message => "None, or broken frontpage extensions on server, or virtual site 'fulluninstall'ed?",
   failtype => "CRITICAL"
 },
 # This is the "FrontPage Configuration Information" file, which is removed
 # when you 'uninstall' the extensions from a site.
 { 
   url => "_vti_inf.html",
   mimetype => "text/html",
   message => "Someone 'uninstall'ed extensions on virtual site?",
   failtype => "WARNING" 
 }
);

print "FRONTPAGE: ";

foreach $temp (@urls) {
   $url = $temp->{'url'};
   $mimetype = $temp->{'mimetype'};
   $failtype = $temp->{'failtype'};
   $message = $temp->{'message'};
   $stime = time();
   $response=$browser->get("http://".$host."/".$url);
   $etime = time();
   $tt = $etime - $stime;

# If we got a server error, or unknown output type, report back as critical.
   if ($response->status_line !~ "^200") {
      print $message." (".$response->status_line.")\r\n";
      exit $ERRORS{$failtype};
   } elsif ($response->content_type !~ $mimetype) {
      print $message." (Wrong Content-type: ".$response->content_type.")\r\n";
      exit $ERRORS{$failtype};
   } else {
      # Because we're dealing with multiple URL's
      $rt += $tt;
   }

# Decide if the response time was critical or not.
#
   if ($rt > $critical) {
      print "Response time ".$rt." over critical threshold ".$critical."\r\n";
      exit($ERRORS{'CRITICAL'});
   } elsif ($rt > $warning) {
      print "Response time ".$rt." over warning threshold ".$warning."\r\n";
      exit($ERRORS{'WARNING'});
   }
}
printf(" %s - %s second response time, ",$response->status_line, $rt);

# If all the required URL's give the right responses quick enough, then we 
# should be okay.
exit($ERRORS{'OK'});

sub print_usage () {
   print "Usage: $PROGNAME -H <host> [-w <warn>] [-c <crit>]\n";
   exit;
}

sub print_help () {
   print_revision($PROGNAME,'$Revision: 1112 $');
   print "Copyright (c) 2003 Kev Green\n";
   print "\n";
   print "FrontPage remains a copyright/trademark of Microsoft Corporation.\n";
   print_usage();
   print "\n";
   print "<warn> = Unknown.\n";
   print "<crit> = Server error from FrontPage extensions.\n\n";
   support();
}
