check_client_cert.pl
changeset 5 9041108a535b
parent 4 14408207746e
child 7 325b70b5b1e6
equal deleted inserted replaced
4:14408207746e 5:9041108a535b
    39 sub get_status($);
    39 sub get_status($);
    40 sub report($);
    40 sub report($);
    41 sub version($$);
    41 sub version($$);
    42 
    42 
    43 my %opt = (
    43 my %opt = (
    44     file     => "/root/CLIENT-CERTS/status.dat",
    44     file     => "status.dat",
    45     warning  => "1month",
    45     warning  => "1month",
    46     critical => "1week"
    46     critical => "1week"
    47 );
    47 );
    48 
    48 
    49 MAIN: {
    49 MAIN: {
    65 sub get_status($) {
    65 sub get_status($) {
    66     my $file   = shift;
    66     my $file   = shift;
    67     my %certs  = ();
    67     my %certs  = ();
    68     my $w_time = DateCalc( "today", "+ $opt{warning}" );
    68     my $w_time = DateCalc( "today", "+ $opt{warning}" );
    69     my $c_time = DateCalc( "today", "+ $opt{critical}" );
    69     my $c_time = DateCalc( "today", "+ $opt{critical}" );
    70     my $rc     = 0;
       
    71 
    70 
    72     open( FILE, $file )
    71     open( FILE, $file )
    73       or do {
    72       or do {
    74         print "CERT CRITICAL: $file $!\n";
    73         print "CERT CRITICAL: $file $!\n";
    75         exit $ERRORS{CRITICAL};
    74         exit $ERRORS{CRITICAL};
    76       };
    75       };
    77 
    76 
    78     while (<FILE>) {
    77     while (<FILE>) {
       
    78 
    79         next if /^#/;
    79         next if /^#/;
    80         next if /^\s+$/;
    80         next if /^\s+$/;
       
    81 
    81         my ( $client, $date ) = split( /;/, $_ );
    82         my ( $client, $date ) = split( /;/, $_ );
       
    83 
       
    84         chomp($date);
    82         my $pdate = ParseDate($date);
    85         my $pdate = ParseDate($date);
    83         chomp($date);
    86 
    84         &Date_Cmp( $pdate, $w_time ) < 0 and $rc = 1;
    87 	if (!$pdate) {
    85         &Date_Cmp( $pdate, $c_time ) < 0 and $rc = 2;
    88 	    push( @{$certs{$client} }, $date, "WRONG" );
    86         if ( $rc == 0 ) {
    89 	    next;
    87             push( @{ $certs{$client} }, $date, "OK" );
    90 	}
    88         }
    91 
    89         elsif ( $rc == 1 ) {
    92         if ( Date_Cmp($pdate, $c_time) <= 0) {
       
    93             push( @{ $certs{$client} }, $date, "CRITICAL" );
       
    94 	    next;
       
    95         }
       
    96 
       
    97         if ( Date_Cmp($pdate, $w_time) <= 0) {
    90             push( @{ $certs{$client} }, $date, "WARNING" );
    98             push( @{ $certs{$client} }, $date, "WARNING" );
    91             $rc = 0;
    99 	    next;
    92         }
   100         }
    93         else {
   101 
    94             push( @{ $certs{$client} }, $date, "CRITICAL" );
   102         push( @{ $certs{$client} }, $date, "OK" );
    95             $rc = 0;
       
    96         }
       
    97     }
   103     }
    98     close(FILE);
   104     close(FILE);
    99 
   105 
   100     ### %certs
   106     ### %certs
   101 
   107 
   102     return \%certs;
   108     return \%certs;
   103 }
   109 }
   104 
   110 
   105 sub report($) {
   111 sub report($) {
   106     my $certs = shift;
   112     my $certs = shift;
   107     my ( @ok, @warning, @critical ) = ();
   113     my ( @ok, @warning, @critical, @wrong ) = ();
   108 
   114 
   109     foreach ( sort keys %$certs ) {
   115     foreach ( sort keys %$certs ) {
   110         if ( $certs->{$_}[1] eq "WARNING" ) {
   116         if ( $certs->{$_}[1] eq "WARNING" ) {
   111             push( @warning, "$_ client certificate expires $certs->{$_}[0]" );
   117             push( @warning, "$_ client certificate expires $certs->{$_}[0]" );
   112         }
   118         }
   113         elsif ( $certs->{$_}[1] eq "CRITICAL" ) {
   119         elsif ( $certs->{$_}[1] eq "CRITICAL" ) {
   114             push( @critical, "$_ client certificate expires $certs->{$_}[0]" );
   120             push( @critical, "$_ client certificate expires $certs->{$_}[0]" );
   115         }
   121         }
       
   122         elsif ( $certs->{$_}[1] eq "WRONG" ) {
       
   123             push( @wrong, "$_ has a broken date in status.dat, please check:  $certs->{$_}[0]" );
       
   124         }
   116         else {
   125         else {
   117             push( @ok, "$_ client certificate expires $certs->{$_}[0]" );
   126             push( @ok, "$_ client certificate expires $certs->{$_}[0]" );
   118         }
   127         }
   119     }
   128     }
   120 
   129 
   121     ### @critical
   130     ### @critical
       
   131     ### @wrong
   122     ### @warning
   132     ### @warning
   123     ### @ok
   133     ### @ok
   124 
   134 
   125     if (@critical) {
   135     if (@wrong) {
       
   136         print "WRONG DATE FORMAT: " . join( " ", @wrong );
       
   137         exit $ERRORS{"CRITICAL"};
       
   138     }
       
   139     elsif (@critical) {
   126         print "CERT CRITICAL: " . join( " ", @critical );
   140         print "CERT CRITICAL: " . join( " ", @critical );
   127         exit $ERRORS{"CRITICAL"};
   141         exit $ERRORS{"CRITICAL"};
   128     }
   142     }
   129     elsif (@warning) {
   143     elsif (@warning) {
   130         print "CERT WARNING: " . join( " ", @warning );
   144         print "CERT WARNING: " . join( " ", @warning );