mirror of
https://github.com/pacnpal/simpleguardhome.git
synced 2025-12-20 04:21:13 -05:00
feat(docker): add monitoring script and update Dockerfile and .dockerignore for enhanced backup monitoring and system resource tracking
This commit is contained in:
@@ -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__/
|
||||
|
||||
12
Dockerfile
12
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
|
||||
|
||||
66
monitor.py
Normal file
66
monitor.py
Normal file
@@ -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)
|
||||
Reference in New Issue
Block a user