#!/bin/bash # NetGescon Sync Monitor # Script per monitorare lo stato della sincronizzazione e generare report # Versione: 1.0 # Data: 2025-01-17 set -euo pipefail # Configurazione SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" CONFIG_FILE="$SCRIPT_DIR/config/sync-config.conf" LOG_FILE="$HOME/netgescon/log/auto-sync.log" MONITOR_LOG="$HOME/netgescon/log/sync-monitor.log" REPORT_DIR="$HOME/netgescon/reports" # Colori RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' NC='\033[0m' # Carica configurazione se esiste if [[ -f "$CONFIG_FILE" ]]; then source "$CONFIG_FILE" fi # Funzioni utility log_monitor() { local message="$1" local level="${2:-INFO}" local timestamp=$(date "+%Y-%m-%d %H:%M:%S") local log_entry="[$timestamp] [$level] $message" mkdir -p "$(dirname "$MONITOR_LOG")" echo "$log_entry" >> "$MONITOR_LOG" case "$level" in "ERROR") echo -e "${RED}$log_entry${NC}" >&2 ;; "WARN") echo -e "${YELLOW}$log_entry${NC}" ;; "SUCCESS") echo -e "${GREEN}$log_entry${NC}" ;; *) echo -e "${NC}$log_entry${NC}" ;; esac } check_sync_status() { local status="UNKNOWN" local last_sync="N/A" local last_error="N/A" if [[ -f "$LOG_FILE" ]]; then # Trova ultima sincronizzazione last_sync=$(grep "NetGescon Auto-Sync Completed" "$LOG_FILE" 2>/dev/null | tail -1 | cut -d']' -f1-2 | tr -d '[]' || echo "N/A") # Trova ultimo errore last_error=$(grep "ERROR" "$LOG_FILE" 2>/dev/null | tail -1 | cut -d']' -f1-2 | tr -d '[]' || echo "N/A") # Determina stato if [[ "$last_sync" != "N/A" ]]; then local sync_time=$(date -d "$last_sync" +%s 2>/dev/null || echo "0") local error_time=$(date -d "$last_error" +%s 2>/dev/null || echo "0") if [[ "$sync_time" -gt "$error_time" ]]; then status="SUCCESS" else status="ERROR" fi fi fi echo "$status|$last_sync|$last_error" } get_sync_stats() { local today=$(date +%Y-%m-%d) local total_syncs=0 local successful_syncs=0 local failed_syncs=0 if [[ -f "$LOG_FILE" ]]; then total_syncs=$(grep "$today.*NetGescon Auto-Sync Started" "$LOG_FILE" 2>/dev/null | wc -l) successful_syncs=$(grep "$today.*NetGescon Auto-Sync Completed" "$LOG_FILE" 2>/dev/null | wc -l) failed_syncs=$((total_syncs - successful_syncs)) fi echo "$total_syncs|$successful_syncs|$failed_syncs" } check_system_health() { local local_health="OK" local remote_health="OK" # Controlla locale if [[ ! -d "$LOCAL_PATH" ]]; then local_health="ERROR: Local path not found" elif [[ ! -w "$LOCAL_PATH" ]]; then local_health="ERROR: Local path not writable" fi # Controlla remoto if ! ssh -o ConnectTimeout=5 -o BatchMode=yes "$REMOTE_USER@$REMOTE_HOST" "test -d $REMOTE_PATH" 2>/dev/null; then remote_health="ERROR: Remote connection failed" fi echo "$local_health|$remote_health" } generate_html_report() { local report_file="$REPORT_DIR/sync-dashboard-$(date +%Y%m%d-%H%M%S).html" mkdir -p "$REPORT_DIR" # Raccogli dati local sync_status_data=$(check_sync_status) local sync_stats_data=$(get_sync_stats) local health_data=$(check_system_health) IFS='|' read -r status last_sync last_error <<< "$sync_status_data" IFS='|' read -r total_syncs successful_syncs failed_syncs <<< "$sync_stats_data" IFS='|' read -r local_health remote_health <<< "$health_data" # Genera HTML cat > "$report_file" << EOF
Monitoraggio Sincronizzazione Automatica
Ultima sincronizzazione: $last_sync
Ultimo errore: $last_error