Skip to content

Instantly share code, notes, and snippets.

@kmmndr
Created April 30, 2019 12:23
Show Gist options
  • Save kmmndr/1a3357ef97e48eb84b067cd12dcda58f to your computer and use it in GitHub Desktop.
Save kmmndr/1a3357ef97e48eb84b067cd12dcda58f to your computer and use it in GitHub Desktop.
monitor remaining days before ssl certificate expiration
#!/bin/bash
set -eu
if (( $# < 1 )); then
echo "usage: $0 <host:port> [<host:port> ...]"
exit 1
fi
me=$(basename "$0")
HOSTNAME="${COLLECTD_HOSTNAME:-localhost}"
INTERVAL="${COLLECTD_INTERVAL:-60}"
cache_file="/tmp/tmp.$me"
function ssl_end_days() {
host=$1
end_date_txt=$(echo | timeout 1 openssl s_client -connect "$host" 2> /dev/null | openssl x509 -noout -enddate 2> /dev/null | sed -e 's/.*notAfter=\(.*\)/\1/')
diff_days=$(( ($(date -d "$end_date_txt" +%s) - $(date +%s)) / 86400 ))
echo $diff_days
}
echo "$cache_file"
while true; do
echo -n '' > "$cache_file"
for arg in "$@"
do
VALUE=$(ssl_end_days "$arg")
echo "PUTVAL \"$HOSTNAME/exec-$me/$arg\" interval=$INTERVAL N:$VALUE" >> "$cache_file"
done
sleep 3600
done &
while sleep "$INTERVAL"; do
cat "$cache_file"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment