Server side ssl certificate check, zabbix

By executing this code on Zabbix server side it is possible to retrieve how much days left till certificates will expire.

ssl-cert-check.sh
#!/bin/dash
SERVER=$1
PORT=${2:-443}
TIMEOUT=25
end_date="$(/usr/bin/timeout $TIMEOUT /usr/bin/openssl s_client -host $SERVER -port $PORT -showcerts < /dev/null 2>/dev/null | sed -n '/BEGIN CERTIFICATE/,/END CERT/p' | openssl x509 -enddate -noout 2>/dev/null | sed -e 's/^.*\=//')"

if [ -n "$end_date" ]
then
end_date_seconds=$(date "+%s" --date "$end_date")
now_seconds=$(date "+%s")
CALC=$((($end_date_seconds-$now_seconds)/24/3600))
echo $CALC
else
exit 124
fi
Make the script executable:
chmod +x ssl-cert-check.sh
Usage:
./ssl-cert-check.sh www.google.com
or:
./ssl-cert-check.sh www.google.com 443

No comments: