-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_cert_expire.pl
executable file
·73 lines (59 loc) · 1.88 KB
/
check_cert_expire.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env perl
#
#
use Net::SSL::ExpireDate;
use Getopt::Long;
use strict;
use warnings;
my $host = ""; # domain name from command line
my $port = ""; # tcp port
my $expire_warn_days = 30; # days to expire when warn
my $expire_critical_days = 7; # days to expire when crit
GetOptions( 'host=s' => \$host, 'port=s' => \$port );
my %ERRORS = (
'OK' => 0,
'WARNING' => 1,
'CRITICAL' => 2,
'UNKNOWN' => 3,
'DEPENDENT' => 4
);
my $exit_status = 'OK';
my $exit_message = "";
my $status = 0;
my $message = "";
if ( !$host || !$port ) {
print <<EOF;
Script to check ssl certificate expiration only, no validation tests
Usage: $0 --host ip --port ssl_port
Sample: $0 --host www.google.com --port 443 # check google cert
Sample: $0 --host smtp.gmail.com --port 465 # check google smtps cert
EOF
exit 2;
}
( $message, $status ) = &check_cert_exp();
$exit_message = $exit_message . "[ $message $status ]";
$exit_status = $status;
print "$exit_status ", $exit_message, "\n";
exit $ERRORS{$exit_status};
sub check_cert_exp() {
my $cur_date = DateTime->now->epoch;
my $ed = Net::SSL::ExpireDate->new( ssl => "$host:$port" ); # ssl
my $cert_date_exp = $ed->expire_date->epoch;
my $cert_sec_exp_left = $cert_date_exp - $cur_date;
my $cert_days_exp_left = int( $cert_sec_exp_left / ( 24 * 60 * 60 ) );
if ( $expire_critical_days >= $cert_days_exp_left ) {
return (
"certificate for $host:$port expires via $cert_days_exp_left days",
"CRITICAL"
);
}
if ( $expire_warn_days >= $cert_days_exp_left ) {
return (
"certificate for $host:$port expires via $cert_days_exp_left days",
"WARNING"
);
}
return (
"certificate for $host:$port expires via $cert_days_exp_left days",
"OK" );
}