From 88e29c31a639c3249416d22df82da1257a704083 Mon Sep 17 00:00:00 2001 From: pacnpal <183241239+pacnpal@users.noreply.github.com> Date: Wed, 29 Jan 2025 00:09:25 -0500 Subject: [PATCH] feat(docker): add monitoring script and update Dockerfile and .dockerignore for enhanced backup monitoring and system resource tracking --- .dockerignore | 2 ++ Dockerfile | 12 ++++++---- monitor.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 monitor.py diff --git a/.dockerignore b/.dockerignore index c0af9be..76f3d7a 100644 --- a/.dockerignore +++ b/.dockerignore @@ -14,6 +14,7 @@ !requirements.txt !docker-entrypoint.sh !healthcheck.py +!monitor.py # VERIFICATION: Required files that MUST exist: # - src/simpleguardhome/__init__.py @@ -23,6 +24,7 @@ # - src/simpleguardhome/templates/index.html # - src/simpleguardhome/favicon.ico # - healthcheck.py +# - monitor.py # SAFETY: Never include these files even if allowed above **/__pycache__/ diff --git a/Dockerfile b/Dockerfile index 6507a3f..417e3ad 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,7 +25,8 @@ RUN for backup in main backup1 backup2 backup3 backup4 rescue emergency last_res mkdir -p "/app/$backup/monitor" && \ chmod -R 755 "/app/$backup" && \ echo "Created $backup hierarchy"; \ - done + done && \ + mkdir -p /app/monitor # STEP 2: Install Python packages with verification COPY requirements.txt . @@ -51,9 +52,10 @@ RUN echo "Creating verified backups..." && \ echo "✓ Created and verified $backup"; \ done -# STEP 4: Create monitoring script -RUN echo 'import os,sys,psutil,time,json,logging\nwhile True:\n stats={"cpu":psutil.cpu_percent(),"mem":psutil.virtual_memory().percent,"disk":psutil.disk_usage("/").percent}\n for backup in ["main","backup1","backup2","backup3","backup4","rescue","emergency","last_resort","ultrabackup"]:\n if not os.path.exists(f"/app/{backup}/src/simpleguardhome"): stats[f"{backup}_missing"]=True\n with open("/app/monitor/stats.json","w") as f: json.dump(stats,f)\n time.sleep(5)' > /app/monitor/monitor.py && \ - chmod +x /app/monitor/monitor.py +# STEP 4: Set up monitoring +COPY monitor.py /app/monitor/ +RUN chmod +x /app/monitor/monitor.py && \ + echo "✓ Installed monitoring system" # STEP 5: Set up health check script COPY healthcheck.py /usr/local/bin/ @@ -96,6 +98,8 @@ RUN echo "=== 🚀 ULTRA FINAL VERIFICATION ===" && \ echo "Verifying checksums for $backup:" && \ cd "/app/$backup" && md5sum -c checksums.md5; \ done && \ + echo "Testing monitoring system:" && \ + python3 -c "import psutil; print('Monitoring system ready')" && \ echo "✅ EVERYTHING IS VERIFIED, BACKED UP, AND MONITORED!" # Copy entrypoint script diff --git a/monitor.py b/monitor.py new file mode 100644 index 0000000..9ba27fd --- /dev/null +++ b/monitor.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 +import os +import sys +import psutil +import time +import json +import logging + +# Set up logging +logging.basicConfig( + level=logging.INFO, + format='%(asctime)s - %(levelname)s - %(message)s' +) + +def monitor_system(): + backups = [ + "main", "backup1", "backup2", "backup3", "backup4", + "rescue", "emergency", "last_resort", "ultrabackup" + ] + + while True: + try: + stats = { + "cpu": psutil.cpu_percent(), + "mem": psutil.virtual_memory().percent, + "disk": psutil.disk_usage("/").percent, + "timestamp": time.time() + } + + # Check all backup directories + for backup in backups: + path = f"/app/{backup}/src/simpleguardhome" + if not os.path.exists(path): + stats[f"{backup}_missing"] = True + logging.warning(f"Backup missing: {backup}") + + # Save stats + try: + with open("/app/monitor/stats.json", "w") as f: + json.dump(stats, f) + except Exception as e: + logging.error(f"Failed to write stats: {str(e)}") + + # Log warnings for high resource usage + if stats["cpu"] > 80: + logging.warning(f"High CPU usage: {stats['cpu']}%") + if stats["mem"] > 80: + logging.warning(f"High memory usage: {stats['mem']}%") + if stats["disk"] > 80: + logging.warning(f"High disk usage: {stats['disk']}%") + + except Exception as e: + logging.error(f"Monitoring error: {str(e)}") + + time.sleep(5) + +if __name__ == "__main__": + logging.info("Starting system monitor...") + try: + monitor_system() + except KeyboardInterrupt: + logging.info("Shutting down monitor...") + sys.exit(0) + except Exception as e: + logging.error(f"Fatal error: {str(e)}") + sys.exit(1) \ No newline at end of file