Saltar al contenido principal

Lab 11.4 - Monitoreo de Crons y Alertas

Objetivos​

  • Monitorear la ejecución de los crons
  • Detectar fallos
  • Configurar alertas
  • Crear reportes

Ejercicio 1: Ver los Logs de Cron​

# Logs cron
sudo journalctl -u cron

# Filtrar por script
sudo journalctl -u cron | grep mon-script.sh

Ejercicio 2: Script de Monitoreo​

# Verificar ejecución
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 "ALERTA: $SCRIPT no se ha ejecutado hoy"
fi
EOF

chmod +x check-cron.sh

Ejercicio 3: Alertas por Correo Electrónico​

# Configurar email en crontab
crontab -e
# Agregar: MAILTO=su-email@example.com

# Script con notificación
cat > script-with-alert.sh << 'EOF'
#!/bin/bash
if ! mon-script.sh; then
echo "Fallo" | mail -s "Alerta Cron" admin@example.com
fi
EOF

Lab creado el: diciembre 2024 - Probado en Ubuntu 24.04