#!/bin/bash # WEVIA Proactive Monitor v1.0 — */5 cron # Auto-check Docker + disk + RAM + services → auto-restart if needed LOG="/var/log/proactive-monitor.log" TS=$(date '+%Y-%m-%d %H:%M') echo "[$TS] PROACTIVE CHECK" >> $LOG # 1. Docker check — restart stopped containers STOPPED=$(docker ps -a --filter "status=exited" --format "{{.Names}}" | grep -v "chatwoot" | head -5) if [ -n "$STOPPED" ]; then for C in $STOPPED; do docker restart "$C" >> $LOG 2>&1 echo "[$TS] RESTARTED: $C" >> $LOG done fi # 2. Disk check — alert if > 90% DISK=$(df / | tail -1 | awk '{print $5}' | tr -d '%') if [ "$DISK" -gt 90 ]; then echo "[$TS] DISK ALERT: ${DISK}%" >> $LOG # Auto-clean old logs find /var/log -name "*.log" -size +50M -mtime +3 -delete 2>/dev/null docker system prune -f --filter "until=72h" >> $LOG 2>&1 echo "[$TS] AUTO-CLEAN done" >> $LOG fi # 3. RAM check — clear cache if > 85% RAM_PCT=$(free | grep Mem | awk '{printf "%.0f", $3/$2*100}') if [ "$RAM_PCT" -gt 85 ]; then sync && echo 3 > /proc/sys/vm/drop_caches echo "[$TS] RAM CLEAR: was ${RAM_PCT}%" >> $LOG fi # 4. Critical services check for SVC in "ollama:11434" "qdrant:6333" "searxng:8888" "mattermost:8065"; do NAME=$(echo $SVC | cut -d: -f1) PORT=$(echo $SVC | cut -d: -f2) if ! curl -s --max-time 2 "http://127.0.0.1:$PORT/" > /dev/null 2>&1; then echo "[$TS] DOWN: $NAME:$PORT — attempting restart" >> $LOG docker restart "$NAME" >> $LOG 2>&1 || true fi done # 5. Nginx check if ! nginx -t 2>/dev/null; then echo "[$TS] NGINX CONFIG ERROR" >> $LOG fi echo "[$TS] CHECK DONE — disk=${DISK}% ram=${RAM_PCT}%" >> $LOG