root / plugins / ssl / ssl_ @ 292cfb95
Historique | Voir | Annoter | Télécharger (2,19 ko)
| 1 |
#!/bin/sh |
|---|---|
| 2 |
# -*- sh -*- |
| 3 |
|
| 4 |
: << =cut |
| 5 |
|
| 6 |
=head1 NAME |
| 7 |
|
| 8 |
ssl_ - Plugin to monitor certificate expiration |
| 9 |
|
| 10 |
=head1 CONFIGURATION |
| 11 |
|
| 12 |
This plugin does not normally require configuration. |
| 13 |
|
| 14 |
To set warning and critical levels do like this: |
| 15 |
|
| 16 |
[ssl_*] |
| 17 |
env.warning 30: |
| 18 |
|
| 19 |
=head1 AUTHOR |
| 20 |
|
| 21 |
Pactrick Domack |
| 22 |
|
| 23 |
Copyright (C) 2013 Patrick Domack <patrickdk@patrickdk.com> |
| 24 |
|
| 25 |
=head1 LICENSE |
| 26 |
|
| 27 |
=cut |
| 28 |
|
| 29 |
# shellcheck disable=SC1090 |
| 30 |
. "$MUNIN_LIBDIR/plugins/plugin.sh" |
| 31 |
|
| 32 |
ARGS=${0##*ssl_}
|
| 33 |
if echo "$ARGS" | grep -q "_"; then |
| 34 |
SITE=$(echo "$ARGS" | cut -f 1 -d "_") |
| 35 |
PORT=$(echo "$ARGS" | cut -f 2 -d "_") |
| 36 |
else |
| 37 |
SITE=$ARGS |
| 38 |
PORT=443 |
| 39 |
fi |
| 40 |
|
| 41 |
|
| 42 |
# Read data including a certificate from stdin and output the (fractional) number of days left |
| 43 |
# until the expiry of this certificate. The output is empty if parsing failed. |
| 44 |
parse_valid_days_from_certificate() {
|
| 45 |
local input_data |
| 46 |
local valid_until_string |
| 47 |
local valid_until_epoch |
| 48 |
local now_epoch |
| 49 |
local input_data |
| 50 |
input_data=$(cat) |
| 51 |
if echo "$input_data" | grep -q -- "-----BEGIN CERTIFICATE-----"; then |
| 52 |
valid_until_string=$(echo "$input_data" | openssl x509 -noout -enddate \ |
| 53 |
| grep "^notAfter=" | cut -f 2 -d "=") |
| 54 |
if [ -n "$valid_until_string" ]; then |
| 55 |
valid_until_epoch=$(date --date="$valid_until_string" +%s) |
| 56 |
if [ -n "$valid_until_epoch" ]; then |
| 57 |
now_epoch=$(date +%s) |
| 58 |
# calculate the number of days left |
| 59 |
echo "$valid_until_epoch" "$now_epoch" | awk '{ print(($1 - $2) / (24 * 3600)); }'
|
| 60 |
fi |
| 61 |
fi |
| 62 |
fi |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
case $1 in |
| 67 |
config) |
| 68 |
|
| 69 |
echo "graph_title $SITE SSL Certificate Expire" |
| 70 |
echo 'graph_args --base 1000' |
| 71 |
echo 'graph_vlabel days left' |
| 72 |
echo 'graph_category security' |
| 73 |
echo "graph_info This graph shows the days left for the certificate being served by $SITE" |
| 74 |
echo 'expire.label days' |
| 75 |
print_warning expire |
| 76 |
print_critical expire |
| 77 |
|
| 78 |
exit 0 |
| 79 |
;; |
| 80 |
esac |
| 81 |
|
| 82 |
cert=$(echo "" | openssl s_client -CApath /etc/ssl/certs -servername "${SITE}" -connect "${SITE}:${PORT}" 2>/dev/null);
|
| 83 |
|
| 84 |
days_left=$(echo "$cert" | parse_valid_days_from_certificate) |
| 85 |
[ -n "$days_left" ] || days_left="U" |
| 86 |
|
| 87 |
printf 'expire.value %s\n' "$days_left" |
