Vérifier la date d'expiration des certificats
Ce script en bash permet de verifier des certificats de confiance stockés dans un ordinateur utilisés pour NRPE par exemple (voir l'article sur NSClient) ou pour sécuriser les flux broker.
1 Le plugin
Voici le script bash tout simple. Les arguments sont le fichier à vérifier, les seuils Warning et Critical en jours avant la date d'expiration du certificat.
#!/bin/bash
#===============================================================================
# Centreon Plugin Bash Script - check_cert.sh
# Auteurs : Boris HUISGEN <bhuisgen@hbis.fr>, modified by kermith72
# Date : 26/03/2021
# But : This script checks certificat files
#===============================================================================
#
# Usage info
show_help() {
cat << EOF
This script checks certificat files
Usage: ${0##*/} [-f=<certificat files="">] [-w=<warning days="">] [-c=<critical days="">]
-f|--file certificat files
-w|--warning Threshold warning in days
-c|--critical Threshold critical in days
-h|--help help
EOF
}
for i in "$@"
do
case $i in
-f=*|--file=*)
FILE="${i#*=}"
shift # past argument=value
;;
-w=*|--warning=*)
WARNING="${i#*=}"
shift # past argument=value
;;
-c=*|--critical=*)
CRITICAL="${i#*=}"
shift # past argument=value
;;
-h|--help)
show_help
exit 2
;;
*)
# unknown option
;;
esac
done
# Check for missing parameters
if [[ -z "$WARNING" ]] || [[ -z "$CRITICAL" ]] || [[ -z "$FILE" ]] ; then
echo "UNKNOWN - Missing parameters!"
show_help
exit 3
fi
# Check warning is lower than critical
if [[ "$WARNING" -le "$CRITICAL" ]]; then
echo "UNKNOWN - CRITICAL must be lower than WARNING ! "
exit 3
fi
# Check files exist
if [ ! -e $FILE ] ; then
echo "UNKNOWN - $FILE file does not exist."
exit 3;
fi
# main read certificat
EXPIRE_DATE_STRING=$(openssl x509 -in $FILE -noout -enddate | cut -f2 -d=);
EXPIRE_DATE=$(date -d "${EXPIRE_DATE_STRING}" +%s);
TODAY=$(date +%s)
DELTA=$(($((${EXPIRE_DATE} - ${TODAY}))/86400))
# Check if delta is greater than WARNING threshold parameter
if [[ "$DELTA" -gt "$WARNING" ]]; then
echo "OK: Certificate expiration in days: ${DELTA} - Validity Date: ${EXPIRE_DATE_STRING} "
exit 0
fi
# Check if delta is lower than WARNING threshold parameter and greater than CRITICAL threshold parameter
if [[ "$DELTA" -le "$WARNING" ]] && [[ "$DELTA" -gt "$CRITICAL" ]]; then
echo "WARNING - Certificate expiration in days: ${DELTA} - Validity Date: ${EXPIRE_DATE_STRING}"
exit 1
fi
# Check if delta is lower than CRITICAL threshold parameter
if [[ "$DELTA" -le "$CRITICAL" ]]; then
echo "CRITICAL - Certificate expiration in days: ${DELTA} - Validity Date: ${EXPIRE_DATE_STRING}"
exit 2
fi2 Test du plugin
Voici le test du plugin avec toutes les possibilités. Attention, ne pas mettre d’espace entre le paramètre et sa valeur associée. Il faudra vous assurer que le user centreon-engine puisse lire le fichier certificat.
Commande passée sans argument provoque une erreur.
/usr/lib/nagios/plugins/check_cert.sh UNKNOWN - Missing parameters! This script checks certificat files Usage: check_cert.sh [-f=La valeur CRITICAL doit être inférieure à la valeur Warning.] [-w= ] [-c= ] -f|--file certificat files -w|--warning Threshold warning in days -c|--critical Threshold critical in days -h|--help help
/usr/lib/nagios/plugins/check_cert.sh -f=/etc/centreon/certificat/client_certs/client_cert.pem -w=15 -c=30 UNKNOWN - CRITICAL must be lower than WARNING !Retour OK
/usr/lib/nagios/plugins/check_cert.sh -f=/etc/centreon/certificat/client_certs/client_cert.pem -w=30 -c=15 OK: Certificate expiration in days: 362 - Validity Date: Dec 19 08:27:29 2020 GMTRetour Warning
/usr/lib/nagios/plugins/check_cert.sh -f=/etc/centreon/certificat/client_certs/client_cert.pem -w=365 -c=15 WARNING - Certificate expiration in days: 362 - Validity Date: Dec 19 08:27:29 2020 GMTRetour Critical
/usr/lib/nagios/plugins/check_cert.sh -f=/etc/centreon/certificat/client_certs/client_cert.pem -w=365 -c=364 CRITICAL - Certificate expiration in days: 362 - Validity Date: Dec 19 08:27:29 2020 GMT
3 Configuration Centreon
Le plugin sera à déposer dans le dossier /usr/lib/nagios/plugins.
3.1 Création de la commande check_certificat_cert
Création de la commande check_certificat_cert
Command line
$USER1$/check_cert.sh -f=$_SERVICEFILECERT$ -w=$_SERVICEWARNING$ -c=$_SERVICECRITICAL$
3.3 Création du modèle service stpl_check_certificat_cert
Création du modèle service stpl_check_certificat_cert
Pour la création du service, utilisez le modèle generic-service et la commande créée précedemment check_certificat_cert. Pour ma part, j’ai positionné les macro Warning à 30 et Critical à 15.
3.2 Création du service check_certificat_client
Il faut créer un service rattaché au serveur de supervision. On ajoutera le nom du fichier du certificat à vérifier.
création du service
Et voici le résultat pour le serveur centreon
Les services sont opérationnels
Pour supervision des certificats sur un serveur distant, plusieurs solutions :
- installer NRPE et le plugin sur le serveur distant,
- Installer le plugin sur le serveur distant, créer une liaison ssh entre le Central et le serveur distant et utiliser le plugin check_by_ssh.
- installer NRPE et le plugin sur le serveur distant,
- Installer le plugin sur le serveur distant, créer une liaison ssh entre le Central et le serveur distant et utiliser le plugin check_by_ssh.
