#! perl -w

=head1 NAME

check_reps.pl - checks the output of C<repadmin /showreps> for errors.

=head1 VERSION

Version 1.0

=head1 SYNOPSIS

  ./check_reps.pl --wlevel <wlevel> --clevel <clevel>
  <wlevel>  number of consecutive failures for a warning (default:2)
  <clevel>  number of consecutive failures for a critical (default:20)

=head1 DESCRIPTION

This looks through the output of "repadmin /showreps" for failures and outputs
the failed hosts and the number of consecutive failures.

=head1 AUTHOR

Copyright (c) Hannes Schulz 2003

=cut

use strict;
use Getopt::Long;

my $cmdline = "repadmin /showreps";
#my $cmdline = "cat ~/rep";

my $clevel;
my $wlevel;

GetOptions(
	"w|wlevel=i", \$wlevel,
	"c|clevel=i", \$clevel
);


$clevel ||= 20;
$wlevel ||= 2;

my $t1 = time;
my $output = `$cmdline`;

my @out = split /\n/,$output;

my $act = "";
my $actfail = 0;
my @failed;
my $serv;
my $failures;
my $worst = 0;
foreach (2..$#out){
	if($out[$_]  =~ /^(DC|CN)=(\w+)/){
		$act =  $2;
		$actfail = 0;
	}
	if ($out[$_] =~ /Last attempt/ and $out[$_]=~/failed/i){
		if(($serv) = ($out[$_-2] =~ /Site-..-[\w\-]+\\(\w+)/o)){
			if(($failures) = ($out[$_+3] =~ m/^\s*(\d+)\scons/o)){
				push @failed, (!$actfail?"$act:":"")."$serv($failures)";
				$worst = ($worst<$failures)?$failures:$worst;
			}
		}
	}
}

my $duration = time-$t1;
my $numfailed = $#failed + 1;
if($numfailed < 1){print "Failed: none. | failed=0,time=$duration\n"}
else{print "Failed: ", join(", ",@failed), " | failed=$numfailed,time=$duration\n"}

exit 2 if($worst >= $clevel);  # Nagios: Critical
exit 1 if($worst >= $wlevel);  # Nagios: Warning
exit 0 ;               # Nagios: OK

sub printHelp{
print <<EOT
$0 Version 1.0
Copyright (c) Hannes Schulz 2003

Usage: $0 [--help] --wlevel <wlevel> --clevel <clevel>
  <wlevel>  number of consecutive failures for a warning (default:2)
  <clevel>  number of consecutive failures for a critical (default:20)

EOT
;exit 3 # Nagios: unknown
}
# vim:ts=2:sw=2
