#! perl -w

=head1 NAME

check_frs.pl - checks if the frs-test-file is too old.

=head1 VERSION

Version 1.0

=head1 AUTHOR

Copyright (c) Hannes Schulz 2003

=head1 SYNOPSIS

./check_frs.pl --file <file> --age <min> --wlevel <wmin>

 where <file> is the replicated file with the timestamp in it.
 and <min> is the max age of the file in minutes.
 If the file is older than <wmin>, a warning is issued.

=head1 DESCRIPTION

This tests the age of the frs.tst file on the netlogon share.
Older than a certain time => error.

Use L<select-the-one.pl> to update the file every hour or so.

=head1 CONFIGURATION

change paths in script if changed

=cut

use strict;
use Getopt::Long;

my $file;
my $mage;
my $wage;
printHelp() unless (GetOptions(
		'h|help' => sub{printHelp()},
                'a|age=i'  => \$mage,
                'w|wlevel=i' => \$wage,
		'f|file' => \$file));

# Set default values
$mage ||= 70;
$wage ||= 60;
unless($file){
  my $special = ($ENV{COMPUTERNAME} =~ /^saproot/i)?1:0;
  $file = $special?
	'c:\winnt\SYSVOL\sysvol\corp\scripts\frs.tst':
	'c:\winnt\SYSVOL\sysvol\sap.corp\scripts\frs.tst';
}

# Try reading the file
unless(open(FRS,"<$file")){
	print "could not open FRS check file: $!\n";
	exit 3; # Nagios: unknown
}

# get file time stamp
my $fts = <FRS>;
chomp($fts);
close(FRS);

my $fdiffmin = int((time - $fts)/60);

my ($ostr,$fstr,$oret,$fret);

if($fdiffmin>$mage){
	$fstr = "Test-File too old! (age=$fdiffmin min)";
	$fret = 2        # Nagios: Critical
}
elsif($fdiffmin>$wage){
        $fstr = "Test-File too old! (age=$fdiffmin min)";
        $fret = 1        # Nagios: Warning.
}
else{
	$fstr = "Test-File OK (age=$fdiffmin min)";
	$fret = 0        # Nagios: OK
}

print "$fstr | file=$fdiffmin\n";
exit $fret;

sub printHelp{
print <<EOT
$0 Version 1.0

Usage: $0 [ --help ] --file <file> --age <min> --wlevel <wmin>

 <file> -- the replicated file with the timestamp in it.
 <min>  -- the maximum age of the file that is tolerated, CRITICAL if older.
 <wmin> -- warning level age

EOT
;exit 3  # Nagios: unknown
}
