#!/bin/bash # System monitoring script for Arrs Media Stack # Generated by Ansible LOG_DIR="{{ docker_root }}/logs/system" TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S') LOG_FILE="$LOG_DIR/system-monitor-$(date '+%Y%m%d').log" # Ensure log directory exists mkdir -p "$LOG_DIR" # Function to log with timestamp log_with_timestamp() { echo "[$TIMESTAMP] $1" >> "$LOG_FILE" } # System metrics CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1) MEMORY_USAGE=$(free | grep Mem | awk '{printf "%.1f", $3/$2 * 100.0}') DISK_USAGE=$(df {{ docker_root }} | tail -1 | awk '{print $5}' | cut -d'%' -f1) LOAD_AVG=$(uptime | awk -F'load average:' '{print $2}' | awk '{print $1}' | cut -d',' -f1) # Log system metrics log_with_timestamp "SYSTEM_METRICS CPU:${CPU_USAGE}% MEM:${MEMORY_USAGE}% DISK:${DISK_USAGE}% LOAD:${LOAD_AVG}" # Check Docker service if systemctl is-active --quiet docker; then log_with_timestamp "DOCKER_SERVICE OK" else log_with_timestamp "DOCKER_SERVICE FAILED" fi # Check Arrs services cd {{ docker_compose_dir }} SERVICES=("sonarr" "radarr" "lidarr" "bazarr" "prowlarr" "watchtower") for service in "${SERVICES[@]}"; do if docker-compose ps "$service" | grep -q "Up"; then log_with_timestamp "SERVICE_${service^^} OK" else log_with_timestamp "SERVICE_${service^^} FAILED" # Try to restart failed service docker-compose restart "$service" 2>/dev/null log_with_timestamp "SERVICE_${service^^} RESTART_ATTEMPTED" fi done # Check disk space warning (>80%) if [[ $DISK_USAGE -gt 80 ]]; then log_with_timestamp "DISK_WARNING Disk usage is ${DISK_USAGE}%" fi # Check memory warning (>90%) if (( $(echo "$MEMORY_USAGE > 90" | bc -l) )); then log_with_timestamp "MEMORY_WARNING Memory usage is ${MEMORY_USAGE}%" fi # Check load average warning (>2.0) if (( $(echo "$LOAD_AVG > 2.0" | bc -l) )); then log_with_timestamp "LOAD_WARNING Load average is $LOAD_AVG" fi # Cleanup old log files (keep 7 days) find "$LOG_DIR" -name "system-monitor-*.log" -mtime +7 -delete 2>/dev/null exit 0