Lab 11.4 - Monitoring Crons et Alertes
Objectifs
- Monitorer l'exécution des crons
- Détecter les échecs
- Configurer des alertes
- Créer des rapports
Exercice 1: Voir les Logs Cron
# Logs cron
sudo journalctl -u cron
# Filtrer par script
sudo journalctl -u cron | grep mon-script.sh
Exercice 2: Script de Monitoring
# Vérifier exécution
cat > check-cron.sh << 'EOF'
#!/bin/bash
SCRIPT="mon-script.sh"
TODAY=$(date +%b\ %d)
EXECUTED=$(sudo journalctl -u cron | grep "$SCRIPT" | grep "$TODAY" | wc -l)
if [ "$EXECUTED" -eq 0 ]; then
echo "ALERTE: $SCRIPT n'a pas été exécuté aujourd'hui"
fi
EOF
chmod +x check-cron.sh
Exercice 3: Alertes Email
# Configurer email dans crontab
crontab -e
# Ajouter: MAILTO=votre-email@example.com
# Script avec notification
cat > script-with-alert.sh << 'EOF'
#!/bin/bash
if ! mon-script.sh; then
echo "Échec" | mail -s "Alerte Cron" admin@example.com
fi
EOF
Lab créé le: Décembre 2024 - Testé sur Ubuntu 24.04