Move files into specific directories in source
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-udldap-freshness
1 #!/usr/bin/perl -w
2
3 use strict;
4 my %CODE = (
5         'UNDEF'         => -1,
6         'OK'            => 0,
7         'WARNING'       => 1,
8         'CRITICAL'      => 2,
9         'UNKNOWN'       => 3
10 );
11
12 my $f;
13
14 $SIG{__DIE__ } = sub() {
15         print shift;
16         exit $CODE{'UNKNOWN'};
17 };
18
19 if ( -e "/var/lib/misc/thishost/passwd.tdb" ) {
20         $f = "/var/lib/misc/thishost/passwd.tdb";
21 } else {
22         my $hostname = `hostname -f`;
23         chomp $hostname;
24         $hostname =~ m/^([a-z]+(?:\.[a-z]+)+)$/;
25         die "Weird hostname '$hostname'\n" unless defined $1;
26         $hostname = $1;
27         if ( -e "/var/lib/misc/$hostname/passwd.tdb" ) {
28                 $f = "/var/lib/misc/$hostname/passwd.tdb";
29         };
30 };
31
32 die "Could not find passwd.tdb" unless defined $f;
33
34 my @stat = stat($f) or die ("Cannot stat $f: $!\n");
35 my $age = time - $stat[10];
36 my $hage;
37 if ($age > 48 * 3600) {
38         $hage = sprintf("%.1f days", $age / 24 / 3600);
39 } elsif ($age > 3600) {
40         $hage = sprintf("%.1f hours", $age / 3600);
41 } else {
42         $hage = sprintf("%d minutes", $age / 60);
43 };
44
45 if ($age > 60*60) {
46         print "WARNING: ud-ldap info is $hage old\n";
47         exit $CODE{'WARNING'};
48 };
49 print "OK: ud-ldap info is $hage old\n";
50 exit $CODE{'OK'};