-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcheck_ssl_cert.sh
executable file
·90 lines (85 loc) · 1.97 KB
/
check_ssl_cert.sh
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env bash
#
# Check SSL certificate
#
# Usage: check_ssl_cert.sh [-w warning] [-c critical] [-h host] [-p port] [-t timeout]
# -w, --warning Warning numbers of days left
# -c, --critical Critical numbers of days left
# -h, --host Hostname
# -p, --port Port, eg: 443
# -t, --timeout Command execution timeout, eg: 10s
# --help Display this screen
#
# (c) 2014, Benjamin Dos Santos <[email protected]>
# https://github.com/bdossantos/nagios-plugins
#
while [[ -n "$1" ]]; do
case $1 in
-h | --host)
host=$2
shift
;;
-p | --port)
port=$2
shift
;;
-t | --timeout)
timeout=$2
shift
;;
-w | --warning)
warn=$2
shift
;;
-c | --critical)
crit=$2
shift
;;
--help)
sed -n '2,12p' "$0" | tr -d '#'
exit 3
;;
*)
echo "Unknown argument: $1"
exec "$0" --help
exit 3
;;
esac
shift
done
host=${host:=localhost}
port=${port:=443}
timeout=${timeout:=30s}
warn=${warn:=15}
crit=${crit:=7}
if timeout "$timeout" \
openssl s_client -servername "$host" -connect "${host}:${port}" \
</dev/null 2>&1 |
openssl x509 -text -in /dev/stdin |
grep -q 'sha1WithRSAEncryption'; then
echo 'CRITICAL - SSL Certificate is SHA1'
exit 2
fi
expire=$(
timeout "$timeout" \
openssl s_client -servername "$host" -connect "${host}:${port}" \
</dev/null 2>&1 |
openssl x509 -enddate -noout |
cut -d '=' -f2
)
parsed_expire=$(date -d "$expire" +%s)
today=$(date +%s)
days_until=$(((parsed_expire - today) / (60 * 60 * 24)))
if [[ $days_until -lt 0 ]]; then
echo "CRITICAL - Expired ${days_until} days ago - ${host}:${port}"
exit 2
elif [[ $days_until -lt $crit ]]; then
echo "CRITICAL - ${days_until} days left - ${host}:${port}"
exit 2
elif [[ $days_until -lt $warn ]]; then
echo "WARNING - ${days_until} days left - ${host}:${port}"
exit 1
else
echo "OK - ${days_until} days left - ${host}:${port}"
exit 0
fi