-
Notifications
You must be signed in to change notification settings - Fork 0
/
cce.sh
92 lines (83 loc) · 1.85 KB
/
cce.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
91
92
#!/bin/bash
# startup checks
if [ -z "$BASH" ]; then
echo "Please use BASH."
exit 3
fi
if [ ! -e "/usr/bin/which" ]; then
echo "/usr/bin/which is missing."
exit 3
fi
openssl=$(which openssl)
if [ $? -ne 0 ]; then
echo "Please install openssl"
exit 3
fi
# Usage Info
usage() {
echo '''
Usage: check_snmp [OPTIONS]
[OPTIONS]
-u URL URL
-p PORT Port (default: 443)
-w WARNING Days left threshold for warning (default: 5)
-c CRITICAL Days left threshold for critical (default: 1)
-o Output Only Only returns days left
'''
}
port=443
warning=5
critical=1
#get options
while getopts "p:u:w:c:o" opt; do
case $opt in
p)
port=$OPTARG
;;
u)
url=$OPTARG
;;
w)
warning=$OPTARG
;;
c)
critical=$OPTARG
;;
o)
outputonly=1
;;
*)
usage
exit 3
;;
esac
done
if [ -z "$url" ]; then
echo "Error: url is required"
usage
exit 3
fi
openssl=$(echo | openssl s_client -showcerts -servername $url -connect $url:$port 2>/dev/null)
if [ $? -ne 0 ]; then
echo "openssl did not return 0 - check domain name or run 'openssl s_client -showcerts -servername $1 -connect $1:443' for more details why"
exit 3
fi
notafter=$(echo "$openssl" | openssl x509 -inform pem -noout -text | grep "Not After")
notafter=$(echo $notafter | sed "s/Not After\s:\s//")
notafter=$(date -d "${notafter}" +%s)
now=$(date -d now +%s)
daysleft=$(( (notafter - now) / 86400 ))
if [[ $outputonly -eq 1 ]] ; then
echo $daysleft
else
if [ $critical -ge $daysleft ]; then
echo "CRITICAL: Certificate is only valid for another $daysleft days"
exit 2
fi
if [ $warning -ge $daysleft ]; then
echo "WARNING: Certificate is only valid for another $daysleft days"
exit 1
fi
echo "OK: Certificate still valid for $daysleft days"
exit 0
fi