refactor(healthcheck): simplify health check process and remove monitoring script

This commit is contained in:
pacnpal
2025-01-29 09:22:53 -05:00
parent 47efc3c4b0
commit 31b66c6d65
4 changed files with 15 additions and 121 deletions

2
.gitignore vendored
View File

@@ -169,5 +169,5 @@ cython_debug/
# PyPI configuration file
.pypirc
rules*.json
rules_backup/*.json
.DS_Store

View File

@@ -1,63 +1,20 @@
#!/usr/bin/env python3
import os
import sys
import psutil
import requests
import hashlib
import json
from pathlib import Path
import httpx
import os
def verify_all_backups():
errors = []
backups = ['main', 'backup1', 'backup2', 'backup3', 'backup4',
'rescue', 'emergency', 'last_resort', 'ultrabackup']
# Check each backup
for backup in backups:
base = f'/app/{backup}/src/simpleguardhome'
if not os.path.exists(base):
errors.append(f'{backup} backup missing!')
continue
# Verify checksums
try:
with open(f'/app/{backup}/checksums.md5') as f:
for line in f:
checksum, file = line.strip().split()
file_path = os.path.join('/app', file)
if os.path.exists(file_path):
with open(file_path, 'rb') as f:
if hashlib.md5(f.read()).hexdigest() != checksum:
errors.append(f'Checksum mismatch in {backup}: {file}')
else:
errors.append(f'File missing in {backup}: {file}')
except Exception as e:
errors.append(f'Failed to verify {backup}: {str(e)}')
# Check monitoring
def check_health():
try:
with open('/app/monitor/stats.json') as f:
stats = json.load(f)
if stats['cpu'] > 90 or stats['mem'] > 90 or stats['disk'] > 90:
errors.append(f'Resource usage too high: CPU={stats["cpu"]}%, MEM={stats["mem"]}%, DISK={stats["disk"]}%')
except Exception as e:
errors.append(f'Monitoring system failure: {str(e)}')
return errors
def main():
try:
errors = verify_all_backups()
if errors:
print('❌ HEALTH CHECK FAILED:')
for error in errors:
print(f'{error}')
sys.exit(1)
print('✅ ALL SYSTEMS OPERATIONAL')
host = os.environ.get('ADGUARD_HOST', 'http://localhost')
port = os.environ.get('ADGUARD_PORT', '8000')
with httpx.Client() as client:
response = client.get('http://localhost:8000/health')
response.raise_for_status()
print('✅ Service is healthy')
sys.exit(0)
except Exception as e:
print(f'💥 FATAL ERROR: {str(e)}')
print(f'❌ Health check failed: {str(e)}')
sys.exit(1)
if __name__ == '__main__':
main()
check_health()

View File

@@ -1,66 +0,0 @@
#!/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)

View File

@@ -0,0 +1,3 @@
This directory is used for storing AdGuard rule backups.
Each backup is stored as a JSON file with a timestamp.
These backup files are git-ignored but the directory structure is maintained.