#!/bin/bash
# monitor-netgescon.sh - Script monitoraggio NetGescon

# Colori per output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

log_info() { echo -e "${GREEN}[OK]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
log_check() { echo -e "${BLUE}[CHECK]${NC} $1"; }

echo "🔍 NetGescon Health Check"
echo "========================="
echo "Data: $(date)"
echo "Host: $(hostname) ($(hostname -I | awk '{print $1}'))"
echo ""

# Controllo servizi
log_check "Stato servizi..."
NGINX_STATUS=$(systemctl is-active nginx 2>/dev/null || echo "inactive")
PHP_STATUS=$(systemctl is-active php8.2-fpm 2>/dev/null || echo "inactive")
MYSQL_STATUS=$(systemctl is-active mysql 2>/dev/null || echo "inactive")

if [ "$NGINX_STATUS" = "active" ]; then
    log_info "Nginx: $NGINX_STATUS"
else
    log_error "Nginx: $NGINX_STATUS"
fi

if [ "$PHP_STATUS" = "active" ]; then
    log_info "PHP-FPM: $PHP_STATUS"
else
    log_error "PHP-FPM: $PHP_STATUS"
fi

if [ "$MYSQL_STATUS" = "active" ]; then
    log_info "MySQL: $MYSQL_STATUS"
else
    log_error "MySQL: $MYSQL_STATUS"
fi

echo ""

# Controllo uso disco
log_check "Uso disco..."
DISK_USAGE=$(df /var/www/netgescon | tail -1 | awk '{print $5}' | sed 's/%//')
if [ $DISK_USAGE -lt 80 ]; then
    log_info "Spazio disco: ${DISK_USAGE}% utilizzato"
elif [ $DISK_USAGE -lt 90 ]; then
    log_warn "Spazio disco: ${DISK_USAGE}% utilizzato"
else
    log_error "Spazio disco: ${DISK_USAGE}% utilizzato (CRITICO)"
fi

# Controllo memoria
log_check "Uso memoria..."
MEMORY_USAGE=$(free | awk 'FNR==2{printf "%.0f", $3/($3+$4)*100}')
if [ $MEMORY_USAGE -lt 80 ]; then
    log_info "Memoria: ${MEMORY_USAGE}% utilizzata"
elif [ $MEMORY_USAGE -lt 90 ]; then
    log_warn "Memoria: ${MEMORY_USAGE}% utilizzata"
else
    log_error "Memoria: ${MEMORY_USAGE}% utilizzata (CRITICO)"
fi

echo ""

# Controllo connessione database
log_check "Connessione database..."
if cd /var/www/netgescon/netgescon-laravel && php artisan tinker --execute="echo \DB::connection()->getPdo() ? 'OK' : 'FAILED';" 2>/dev/null | grep -q "OK"; then
    log_info "Database: Connessione OK"
    
    # Conta utenti
    USER_COUNT=$(php artisan tinker --execute="echo \App\Models\User::count();" 2>/dev/null | tail -1)
    log_info "Database: $USER_COUNT utenti registrati"
else
    log_error "Database: Connessione FAILED"
fi

echo ""

# Controllo HTTP response
log_check "Risposta HTTP..."
SERVER_IP=$(hostname -I | awk '{print $1}')
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "http://$SERVER_IP" 2>/dev/null || echo "000")

case $HTTP_CODE in
    200|302) log_info "HTTP: $HTTP_CODE (OK)" ;;
    404) log_warn "HTTP: $HTTP_CODE (Not Found)" ;;
    500) log_error "HTTP: $HTTP_CODE (Internal Server Error)" ;;
    000) log_error "HTTP: Nessuna risposta" ;;
    *) log_warn "HTTP: $HTTP_CODE" ;;
esac

echo ""

# Controllo log errori
log_check "Log errori recenti..."
LARAVEL_ERRORS=$(tail -50 /var/www/netgescon/netgescon-laravel/storage/logs/laravel.log 2>/dev/null | grep -c "ERROR\|CRITICAL\|EMERGENCY" || echo "0")
NGINX_ERRORS=$(tail -50 /var/log/nginx/error.log 2>/dev/null | grep -c "error\|crit\|emerg" || echo "0")

if [ $LARAVEL_ERRORS -eq 0 ] && [ $NGINX_ERRORS -eq 0 ]; then
    log_info "Log: Nessun errore recente"
elif [ $LARAVEL_ERRORS -lt 5 ] && [ $NGINX_ERRORS -lt 5 ]; then
    log_warn "Log: $LARAVEL_ERRORS errori Laravel, $NGINX_ERRORS errori Nginx"
else
    log_error "Log: $LARAVEL_ERRORS errori Laravel, $NGINX_ERRORS errori Nginx (ATTENZIONE)"
fi

echo ""

# Controllo backup
log_check "Backup recenti..."
if [ -d "/var/backups/netgescon" ]; then
    LAST_BACKUP=$(find /var/backups/netgescon -name "netgescon_db_*.sql" -mtime -1 | wc -l)
    if [ $LAST_BACKUP -gt 0 ]; then
        log_info "Backup: Backup recente trovato (ultime 24h)"
    else
        log_warn "Backup: Nessun backup nelle ultime 24h"
    fi
else
    log_warn "Backup: Directory backup non configurata"
fi

echo ""

# Controllo performance
log_check "Performance..."
LOAD_AVG=$(uptime | awk -F'load average:' '{print $2}' | awk '{print $1}' | sed 's/,//')
LOAD_INT=$(echo "$LOAD_AVG" | cut -d'.' -f1)

if [ $LOAD_INT -lt 2 ]; then
    log_info "Load average: $LOAD_AVG (OK)"
elif [ $LOAD_INT -lt 4 ]; then
    log_warn "Load average: $LOAD_AVG (MEDIO)"
else
    log_error "Load average: $LOAD_AVG (ALTO)"
fi

# Controllo processi PHP
PHP_PROCESSES=$(ps aux | grep -c "php-fpm: pool www" || echo "0")
log_info "Processi PHP-FPM: $PHP_PROCESSES"

echo ""
echo "═══════════════════════════════════════════════════════"

# Calcolo stato generale
CRITICAL_COUNT=0
WARNING_COUNT=0

if [ "$NGINX_STATUS" != "active" ] || [ "$PHP_STATUS" != "active" ] || [ "$MYSQL_STATUS" != "active" ]; then
    CRITICAL_COUNT=$((CRITICAL_COUNT + 1))
fi

if [ $DISK_USAGE -ge 90 ] || [ $MEMORY_USAGE -ge 90 ]; then
    CRITICAL_COUNT=$((CRITICAL_COUNT + 1))
fi

if [ $HTTP_CODE = "500" ] || [ $HTTP_CODE = "000" ]; then
    CRITICAL_COUNT=$((CRITICAL_COUNT + 1))
fi

if [ $LARAVEL_ERRORS -ge 5 ] || [ $NGINX_ERRORS -ge 5 ]; then
    CRITICAL_COUNT=$((CRITICAL_COUNT + 1))
fi

# Warnings
if [ $DISK_USAGE -ge 80 ] || [ $MEMORY_USAGE -ge 80 ]; then
    WARNING_COUNT=$((WARNING_COUNT + 1))
fi

if [ $LAST_BACKUP -eq 0 ]; then
    WARNING_COUNT=$((WARNING_COUNT + 1))
fi

# Stato finale
if [ $CRITICAL_COUNT -eq 0 ] && [ $WARNING_COUNT -eq 0 ]; then
    echo "✅ STATO GENERALE: OTTIMO"
elif [ $CRITICAL_COUNT -eq 0 ] && [ $WARNING_COUNT -gt 0 ]; then
    echo "⚠️  STATO GENERALE: BUONO ($WARNING_COUNT warning)"
else
    echo "❌ STATO GENERALE: PROBLEMI ($CRITICAL_COUNT critici, $WARNING_COUNT warning)"
fi

echo "═══════════════════════════════════════════════════════"
echo ""

# Comandi rapidi
echo "🔧 COMANDI RAPIDI:"
echo ""
echo "📊 MONITORAGGIO:"
echo "   htop                    # Monitor processi"
echo "   df -h                   # Spazio disco"
echo "   free -h                 # Memoria"
echo ""
echo "🔄 RIAVVIO SERVIZI:"
echo "   sudo systemctl restart nginx php8.2-fpm mysql"
echo ""
echo "📝 LOG:"
echo "   tail -f /var/www/netgescon/netgescon-laravel/storage/logs/laravel.log"
echo "   sudo tail -f /var/log/nginx/error.log"
echo ""
echo "💾 BACKUP:"
echo "   /home/$(whoami)/backup-netgescon.sh"
echo ""

# Salva report
REPORT_DIR="/var/log/netgescon"
sudo mkdir -p $REPORT_DIR
echo "$(date): Critical=$CRITICAL_COUNT, Warning=$WARNING_COUNT, HTTP=$HTTP_CODE, Disk=${DISK_USAGE}%, Mem=${MEMORY_USAGE}%" | sudo tee -a $REPORT_DIR/health-check.log > /dev/null

echo "📋 Report salvato in: $REPORT_DIR/health-check.log"
