check_client_cert.pl
changeset 5 9041108a535b
parent 4 14408207746e
child 7 325b70b5b1e6
--- a/check_client_cert.pl	Mon Feb 21 15:14:42 2011 +0100
+++ b/check_client_cert.pl	Fri May 31 14:15:48 2013 +0200
@@ -41,7 +41,7 @@
 sub version($$);
 
 my %opt = (
-    file     => "/root/CLIENT-CERTS/status.dat",
+    file     => "status.dat",
     warning  => "1month",
     critical => "1week"
 );
@@ -67,7 +67,6 @@
     my %certs  = ();
     my $w_time = DateCalc( "today", "+ $opt{warning}" );
     my $c_time = DateCalc( "today", "+ $opt{critical}" );
-    my $rc     = 0;
 
     open( FILE, $file )
       or do {
@@ -76,24 +75,31 @@
       };
 
     while (<FILE>) {
+
         next if /^#/;
         next if /^\s+$/;
+
         my ( $client, $date ) = split( /;/, $_ );
-        my $pdate = ParseDate($date);
+
         chomp($date);
-        &Date_Cmp( $pdate, $w_time ) < 0 and $rc = 1;
-        &Date_Cmp( $pdate, $c_time ) < 0 and $rc = 2;
-        if ( $rc == 0 ) {
-            push( @{ $certs{$client} }, $date, "OK" );
+        my $pdate = ParseDate($date);
+
+	if (!$pdate) {
+	    push( @{$certs{$client} }, $date, "WRONG" );
+	    next;
+	}
+
+        if ( Date_Cmp($pdate, $c_time) <= 0) {
+            push( @{ $certs{$client} }, $date, "CRITICAL" );
+	    next;
         }
-        elsif ( $rc == 1 ) {
+
+        if ( Date_Cmp($pdate, $w_time) <= 0) {
             push( @{ $certs{$client} }, $date, "WARNING" );
-            $rc = 0;
+	    next;
         }
-        else {
-            push( @{ $certs{$client} }, $date, "CRITICAL" );
-            $rc = 0;
-        }
+
+        push( @{ $certs{$client} }, $date, "OK" );
     }
     close(FILE);
 
@@ -104,7 +110,7 @@
 
 sub report($) {
     my $certs = shift;
-    my ( @ok, @warning, @critical ) = ();
+    my ( @ok, @warning, @critical, @wrong ) = ();
 
     foreach ( sort keys %$certs ) {
         if ( $certs->{$_}[1] eq "WARNING" ) {
@@ -113,16 +119,24 @@
         elsif ( $certs->{$_}[1] eq "CRITICAL" ) {
             push( @critical, "$_ client certificate expires $certs->{$_}[0]" );
         }
+        elsif ( $certs->{$_}[1] eq "WRONG" ) {
+            push( @wrong, "$_ has a broken date in status.dat, please check:  $certs->{$_}[0]" );
+        }
         else {
             push( @ok, "$_ client certificate expires $certs->{$_}[0]" );
         }
     }
 
     ### @critical
+    ### @wrong
     ### @warning
     ### @ok
 
-    if (@critical) {
+    if (@wrong) {
+        print "WRONG DATE FORMAT: " . join( " ", @wrong );
+        exit $ERRORS{"CRITICAL"};
+    }
+    elsif (@critical) {
         print "CERT CRITICAL: " . join( " ", @critical );
         exit $ERRORS{"CRITICAL"};
     }