#!/bin/bash
###############################################################################
# Setup Syslog Parser Monitoring
# Installa cron job per health check automatico ogni 5 minuti
# Uso: sudo ./deployment/setup_parser_monitoring.sh
###############################################################################

set -e

echo "📊 Setup Syslog Parser Monitoring..."
echo

# Make health check script executable
chmod +x /opt/ids/deployment/check_parser_health.sh

# Setup cron job
CRON_JOB="*/5 * * * * /opt/ids/deployment/check_parser_health.sh >> /var/log/ids/parser-health-cron.log 2>&1"

# Check if cron job already exists
if crontab -l 2>/dev/null | grep -q "check_parser_health.sh"; then
    echo "✅ Cron job già configurato"
else
    # Add cron job
    (crontab -l 2>/dev/null; echo "$CRON_JOB") | crontab -
    echo "✅ Cron job aggiunto (esecuzione ogni 5 minuti)"
fi

echo
echo "📋 Configurazione completata:"
echo "  - Health check script: /opt/ids/deployment/check_parser_health.sh"
echo "  - Log file: /var/log/ids/parser-health.log"
echo "  - Cron log: /var/log/ids/parser-health-cron.log"
echo "  - Schedule: Every 5 minutes"
echo
echo "🔍 Monitoraggio attivo:"
echo "  - Controlla servizio running"
echo "  - Verifica log recenti (threshold: 5 min)"
echo "  - Auto-restart se necessario"
echo "  - Log errori recenti"
echo
echo "📊 Visualizza stato:"
echo "  tail -f /var/log/ids/parser-health.log"
echo
echo "✅ Setup completato!"
