OLD | NEW |
(Empty) | |
| 1 #!/bin/sh |
| 2 |
| 3 # Icinga plugin that checks how many days are left |
| 4 # until SSL certificate expires. |
| 5 # Usage: |
| 6 # $PLUGIN_DIR/check_ssl_cert.sh -H $HOST -P $PORT -c $CRITICAL -w $WARNING |
| 7 |
| 8 get_ssl_expiry_date() { |
| 9 openssl s_client -connect "$1:$2" </dev/null 2>/dev/null \ |
| 10 | openssl x509 -noout -enddate 2>/dev/null |
| 11 } |
| 12 |
| 13 CURRENT_DATE=`date +%y%m%d` |
| 14 HOST="$2" |
| 15 PORT="$4" |
| 16 CRITICAL="$6" |
| 17 WARNING="$8" |
| 18 STATE_OK=0 |
| 19 STATE_WARNING=1 |
| 20 STATE_CRITICAL=2 |
| 21 STATE_UNKNOWN=3 |
| 22 OUTPUT=`get_ssl_expiry_date "$HOST" "$PORT"` |
| 23 |
| 24 if [ -z "$OUTPUT" ] |
| 25 then |
| 26 echo "UNKNOWN - Could not connect to $HOST via port $PORT" |
| 27 exit "$STATE_UNKNOWN" |
| 28 fi |
| 29 |
| 30 DAY=`echo "$OUTPUT" | awk '{print $2}'` |
| 31 MONTH=`echo "$OUTPUT" | awk '{print $1}' | cut -c 10-` |
| 32 YEAR=`echo "$OUTPUT" | awk '{print $4}'` |
| 33 |
| 34 case "$MONTH" in |
| 35 |
| 36 "Jan") |
| 37 MONTH="01" |
| 38 ;; |
| 39 "Feb") |
| 40 MONTH="02" |
| 41 ;; |
| 42 "Mar") |
| 43 MONTH="03" |
| 44 ;; |
| 45 "Apr") |
| 46 MONTH="04" |
| 47 ;; |
| 48 "May") |
| 49 MONTH="05" |
| 50 ;; |
| 51 "Jun") |
| 52 MONTH="06" |
| 53 ;; |
| 54 "Jul") |
| 55 MONTH="07" |
| 56 ;; |
| 57 "Aug") |
| 58 MONTH="08" |
| 59 ;; |
| 60 "Sep") |
| 61 MONTH="09" |
| 62 ;; |
| 63 "Oct") |
| 64 MONTH="10" |
| 65 ;; |
| 66 "Nov") |
| 67 MONTH="11" |
| 68 ;; |
| 69 "Dec") |
| 70 MONTH="12" |
| 71 ;; |
| 72 "*") |
| 73 echo "An error occured" |
| 74 exit 1 |
| 75 ;; |
| 76 esac |
| 77 |
| 78 EXPIRY_DATE_IN_SEC=`date -d "${YEAR}${MONTH}${DAY}" +%s` |
| 79 CURRENT_DATE_IN_SEC=`date -d "$CURRENT_DATE" +%s` |
| 80 DIFFERENCE=`expr \( "$EXPIRY_DATE_IN_SEC" - "$CURRENT_DATE_IN_SEC" \) / 86400` |
| 81 |
| 82 if [ "$DIFFERENCE" -le "$CRITICAL" ] |
| 83 then |
| 84 echo "CRITICAL - $HOST: SSL certificate has been expired!" |
| 85 exit "$STATE_CRITICAL" |
| 86 elif [ "$DIFFERENCE" -le "$WARNING" ] |
| 87 then |
| 88 echo "WARNING - $HOST: SSL certificate will be expired in $DIFFERENCE days!" |
| 89 exit "$STATE_WARNING" |
| 90 elif [ "$DIFFERENCE" -gt "$WARNING" ] |
| 91 then |
| 92 echo "OK - $HOST: SSL certificate will be expired in $DIFFERENCE days" |
| 93 exit "$STATE_OK" |
| 94 else |
| 95 echo "UNKNOWN - $HOST: Could not retrieve data" |
| 96 exit "$STATE_UNKNOWN" |
| 97 fi |
OLD | NEW |