Aller au contenu principal

Chapitre 11.3 - Monitoring Crons et Alertes

Objectifs d'Apprentissage​

À la fin de ce chapitre, vous serez capable de:

  • Monitorer l'exécution des crons
  • Détecter les échecs de tâches
  • Configurer des alertes pour crons
  • Notifier par email/Slack
  • Créer des dashboards de monitoring
  • Implémenter des health checks

Monitoring des Crons​

Vérifier Exécution​

# Voir dernières exécutions
sudo grep CRON /var/log/syslog | tail -20

# Par script
sudo grep "mon-script.sh" /var/log/syslog

# journalctl
sudo journalctl -u cron --since "1 hour ago"

# Vérifier si exécuté aujourd'hui
sudo grep "mon-script.sh" /var/log/syslog | grep "$(date +%b\ %d)"

Script de Vérification​

#!/bin/bash
# Vérifier exécution cron

SCRIPT="mon-script.sh"
LOG="/var/log/syslog"
EXPECTED_TIME="02:00" # Heure attendue

# Vérifier exécution aujourd'hui
TODAY=$(date +%b\ %d)
EXECUTED=$(grep "$SCRIPT" "$LOG" | grep "$TODAY" | wc -l)

if [ "$EXECUTED" -eq 0 ]; then
echo "ALERTE: $SCRIPT n'a pas été exécuté aujourd'hui"
# Envoyer alerte...
fi

Détection d'Échecs​

Code de Retour​

#!/bin/bash
# Script avec gestion erreurs

set -e # Arrêter sur erreur

# Traitement...
if ! commande_importante; then
echo "ERREUR: Échec commande" >&2
exit 1
fi

# Succès
exit 0

Vérifier Code de Retour​

#!/bin/bash
# Vérifier échec dans logs

SCRIPT="mon-script.sh"
LOG="/var/log/script.log"

# Chercher erreurs
ERRORS=$(grep -i "error\|failed\|échec" "$LOG" | wc -l)

if [ "$ERRORS" -gt 0 ]; then
echo "ALERTE: $ERRORS erreurs dans $SCRIPT"
# Envoyer alerte...
fi

Alertes par Email​

Configuration​

# Installer mail
sudo apt install mailutils # Ubuntu/Debian
sudo yum install mailx # Red Hat/CentOS

# Configuration
# /etc/ssmtp/ssmtp.conf (Ubuntu)
# ou
# /etc/mail.rc (Red Hat)

Script avec Email​

#!/bin/bash
# Script avec notification email

ALERT_EMAIL="admin@example.com"
SCRIPT_NAME="mon-script.sh"

# Exécuter script
if /usr/local/bin/mon-script.sh; then
echo "Succès: $SCRIPT_NAME exécuté" | \
mail -s "Succès: $SCRIPT_NAME" "$ALERT_EMAIL"
else
echo "ÉCHEC: $SCRIPT_NAME a échoué" | \
mail -s "ALERTE: $SCRIPT_NAME" "$ALERT_EMAIL"
exit 1
fi

Crontab avec Email​

# Crontab
# Envoyer sortie par email
MAILTO=admin@example.com
0 2 * * * /usr/local/bin/script.sh

# Ou rediriger vers email
0 2 * * * /usr/local/bin/script.sh 2>&1 | mail -s "Rapport" admin@example.com

Alertes Slack​

Webhook Slack​

#!/bin/bash
# Notification Slack

SLACK_WEBHOOK="https://hooks.slack.com/services/YOUR/WEBHOOK/URL"
SCRIPT_NAME="mon-script.sh"

# Message
MESSAGE="Script $SCRIPT_NAME: $(if [ $? -eq 0 ]; then echo 'Succès'; else echo 'ÉCHEC'; fi)"

# Envoyer
curl -X POST -H 'Content-type: application/json' \
--data "{\"text\":\"$MESSAGE\"}" \
"$SLACK_WEBHOOK"

Fonction Réutilisable​

#!/bin/bash
# Fonction notification Slack

notify_slack() {
local webhook="https://hooks.slack.com/services/YOUR/WEBHOOK/URL"
local message="$1"
local status="$2"

local color="good"
[ "$status" != "success" ] && color="danger"

curl -X POST -H 'Content-type: application/json' \
--data "{\"attachments\":[{\"color\":\"$color\",\"text\":\"$message\"}]}" \
"$webhook"
}

# Utilisation
if mon-script.sh; then
notify_slack "Script réussi" "success"
else
notify_slack "Script échoué" "failure"
fi

Health Checks​

Script de Health Check​

#!/bin/bash
# Health check pour cron

SCRIPT="mon-script.sh"
LOCK_FILE="/var/run/$SCRIPT.lock"
MAX_AGE=3600 # 1 heure en secondes

# Vérifier lock file
if [ -f "$LOCK_FILE" ]; then
AGE=$(($(date +%s) - $(stat -c %Y "$LOCK_FILE")))
if [ "$AGE" -gt "$MAX_AGE" ]; then
echo "ALERTE: Script bloqué depuis $AGE secondes"
# Envoyer alerte...
rm -f "$LOCK_FILE"
fi
fi

Heartbeat​

#!/bin/bash
# Heartbeat pour monitoring

HEARTBEAT_FILE="/var/run/mon-script.heartbeat"

# Mettre à jour timestamp
touch "$HEARTBEAT_FILE"

# Vérifier dans monitoring
# Si heartbeat > 2 heures, alerte

Dashboard de Monitoring​

Script de Rapport​

#!/bin/bash
# Rapport cron quotidien

REPORT="/var/log/cron-report-$(date +%Y%m%d).txt"

{
echo "=== RAPPORT CRON $(date) ==="
echo
echo "=== Exécutions réussies ==="
grep "mon-script.sh" /var/log/syslog | grep -v "error" | wc -l
echo
echo "=== Exécutions échouées ==="
grep "mon-script.sh" /var/log/syslog | grep -i "error\|failed" | wc -l
echo
echo "=== Dernières exécutions ==="
grep "mon-script.sh" /var/log/syslog | tail -10
echo
echo "=== Statistiques ==="
echo "Total exécutions: $(grep 'mon-script.sh' /var/log/syslog | wc -l)"
} > "$REPORT"

# Envoyer par email
mail -s "Rapport Cron $(date +%Y-%m-%d)" admin@example.com < "$REPORT"

Monitoring Avancé​

Script de Monitoring Complet​

#!/bin/bash
# Monitoring complet crons

LOG_DIR="/var/log/cron-monitoring"
mkdir -p "$LOG_DIR"

# Liste des crons à monitorer
CRONS=(
"backup.sh"
"cleanup.sh"
"report.sh"
)

for cron in "${CRONS[@]}"; do
# Vérifier exécution
EXECUTED=$(grep "$cron" /var/log/syslog | grep "$(date +%b\ %d)" | wc -l)

# Vérifier erreurs
ERRORS=$(grep "$cron" /var/log/syslog | grep -i "error\|failed" | wc -l)

# Log
echo "$(date): $cron - Exécutions: $EXECUTED, Erreurs: $ERRORS" >> \
"$LOG_DIR/monitoring.log"

# Alerte si problème
if [ "$EXECUTED" -eq 0 ] || [ "$ERRORS" -gt 0 ]; then
echo "ALERTE: $cron - Exécutions: $EXECUTED, Erreurs: $ERRORS" | \
mail -s "Alerte Cron" admin@example.com
fi
done

Résumé​

Dans ce chapitre, vous avez appris:

Monitoring: Vérifier exécutions, détecter échecs
Alertes: Email, Slack, notifications
Health checks: Lock files, heartbeats
Rapports: Dashboards, statistiques
Scripts: Automatisation monitoring
Pratique: Surveillance continue, alertes proactives


Prochaines Étapes​

Module 12: Administration Avancée (avec automatisation crons)
Fin du Cours: Récapitulatif et Projets


Chapitre créé le: Décembre 2024