#! perl -w

=head1 NAME

check_objage.pl - checks if the test-object is too old.

=head1 VERSION

Version 1.0

=head1 AUTHOR

Copyright (c) Hannes Schulz 2003

=head1 SYNOPSIS

./check_objage.pl --age <min> --wlevel <wmin>

 <min> is the max age of the object in minutes.
 If the object is older than <wmin>, a warning is issued.

=head1 DESCRIPTION

This tests the age of a test object in the Active Directory.

Use L<select-the-one.pl> to update the object every hour.

The object differs from normal DCs to saproot[01]!

=head1 CONFIGURATION

change paths in script if changed

=cut

use strict;
use Getopt::Long;
use Win32::OLE;

my $mage;
my $wage;
printHelp() unless (GetOptions(
		'h|help' => sub{printHelp()},
                'a|age=i'  => \$mage,
                'w|wlevel=i' => \$wage));

# Set default values
$mage ||= 70;
$wage ||= 60;

my $special = ($ENV{COMPUTERNAME} =~ /^saproot/i)?1:0;

my $obj = $special?
	"LDAP://CN=MSDSMONCMP,OU=MSDS,DC=corp":
	"LDAP://CN=MsdsMonCmp,OU=MSDS,OU=Resources,DC=sap,DC=corp";

$obj = Win32::OLE->GetObject($obj) or do{
	print "Could not get Oject!\n";
	exit 3; # Nagios Critical
};
my ($ots) = $obj->{Description} =~ m/.*\n(\d+)$/;
if(!defined $ots){
	print "Wrong Object Description Format!\n";
	exit 2
}

my $odiffmin = int((time - $ots)/60);

my ($ostr,$fstr,$oret,$fret);

if($odiffmin>$mage){
	$ostr = "Object too old! (age=$odiffmin min)";
	$oret = 2        # Nagios: Critical
}
elsif($odiffmin>$wage){
        $ostr = "Object too old! (age=$odiffmin min)";
        $oret = 1        # Nagios: Warning.
}
else{
	$ostr = "Object OK (age=$odiffmin min)";
	$oret = 0        # Nagios: OK
}

print "$ostr | obj=$odiffmin\n";
exit $oret;

sub printHelp{
print <<EOT
$0 Version 1.0

Usage: $0 [ --help ] --age <min> --wlevel <wmin>

 <min>  -- the maximum age of the file that is tolerated, CRITICAL if older.
 <wmin> -- warning level age

EOT
;exit 3  # Nagios: unknown
}
