#!/usr/bin/perl -w

use strict;
my %CODE = (
	'UNDEF'         => -1,
	'OK'            => 0,
	'WARNING'       => 1,
	'CRITICAL'      => 2,
	'UNKNOWN'       => 3
);

my $f;

$SIG{__DIE__ } = sub() {
	print shift;
	exit $CODE{'UNKNOWN'};
};

sub check_age {
	my ($f) = @_;
	my @stat = stat($f) or die ("Cannot stat $f: $!\n");
	my $age = time - $stat[10];
	my $hage;
	if ($age > 48 * 3600) {
		$hage = sprintf("%.1f days", $age / 24 / 3600);
	} elsif ($age > 3600) {
		$hage = sprintf("%.1f hours", $age / 3600);
	} else {
		$hage = sprintf("%d minutes", $age / 60);
	};

	if ($age > 60*60) {
		return [$CODE{'WARNING'}, "WARNING: ud-ldap info is $hage old"];
	};
	return [$CODE{'OK'}, "ud-ldap info is $hage old"];
};

my @msg;
my @to_check = ();

if (-f '/var/lib/misc/thishost/last_update.trace') {
	# New style check
	push @to_check, '/var/lib/misc/thishost/last_update.trace';
} else {
	# Old style
	push @to_check, qw{/var/lib/misc/thishost/passwd.tdb /var/lib/misc/passwd.db};
}

for $f (@to_check) {
	unless (-e $f) {
		print "WARNING: $f does not exist.\n";
		exit $CODE{'WARNING'};
	};

	my $a = check_age($f);
	if ($a->[0] != 0) {
		print $a->[1], "\n";
		exit $a->[0];
	};
	push @msg, $a->[1];
};

print "OK: ", join(', ', @msg), "\n";
exit 0;
