feat: implement automatic metrics collection and monitoring system
This commit is contained in:
+20
-1
@@ -21,8 +21,21 @@ date_default_timezone_set('UTC');
|
||||
// Enable error logging
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', 1);
|
||||
ini_set('log_errors', 1);
|
||||
ini_set('error_log', '/var/log/metrics_collector_errors.log');
|
||||
|
||||
echo "[" . date('Y-m-d H:i:s') . "] Metrics collector started\n";
|
||||
// Write PID file for monitoring
|
||||
$pidFile = '/var/run/collect_metrics.pid';
|
||||
file_put_contents($pidFile, getmypid());
|
||||
|
||||
// Register shutdown function to clean up PID file
|
||||
register_shutdown_function(function() use ($pidFile) {
|
||||
if (file_exists($pidFile)) {
|
||||
unlink($pidFile);
|
||||
}
|
||||
});
|
||||
|
||||
echo "[" . date('Y-m-d H:i:s') . "] Metrics collector started (PID: " . getmypid() . ")\n";
|
||||
|
||||
// Main loop
|
||||
while (true) {
|
||||
@@ -79,6 +92,12 @@ while (true) {
|
||||
|
||||
} catch (Exception $e) {
|
||||
echo "[" . date('Y-m-d H:i:s') . "] FATAL ERROR: " . $e->getMessage() . "\n";
|
||||
error_log("[FATAL] Metrics collector error: " . $e->getMessage());
|
||||
echo "Retrying in 30 seconds...\n\n";
|
||||
sleep(30);
|
||||
} catch (Error $e) {
|
||||
echo "[" . date('Y-m-d H:i:s') . "] CRITICAL ERROR: " . $e->getMessage() . "\n";
|
||||
error_log("[CRITICAL] Metrics collector error: " . $e->getMessage());
|
||||
echo "Retrying in 30 seconds...\n\n";
|
||||
sleep(30);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Monitor and restart metrics collector if it's not running
|
||||
# This script checks if collect_metrics.php is running and restarts it if needed
|
||||
|
||||
SCRIPT_PATH="/var/www/html/bin/collect_metrics.php"
|
||||
LOG_FILE="/var/log/metrics_monitor.log"
|
||||
PID_FILE="/var/run/collect_metrics.pid"
|
||||
|
||||
log_message() {
|
||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE"
|
||||
}
|
||||
|
||||
# Check if the process is running
|
||||
is_running() {
|
||||
if [ -f "$PID_FILE" ]; then
|
||||
PID=$(cat "$PID_FILE")
|
||||
if ps -p "$PID" > /dev/null 2>&1; then
|
||||
# Check if it's actually our script
|
||||
if ps -p "$PID" -o args= | grep -q "collect_metrics.php"; then
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
# Start the metrics collector
|
||||
start_collector() {
|
||||
log_message "Starting metrics collector..."
|
||||
/usr/local/bin/php "$SCRIPT_PATH" >> /var/log/metrics_collector.log 2>&1 &
|
||||
echo $! > "$PID_FILE"
|
||||
log_message "Metrics collector started with PID: $(cat $PID_FILE)"
|
||||
}
|
||||
|
||||
# Main logic
|
||||
if is_running; then
|
||||
log_message "Metrics collector is running (PID: $(cat $PID_FILE))"
|
||||
else
|
||||
log_message "Metrics collector is not running - starting it"
|
||||
start_collector
|
||||
fi
|
||||
Reference in New Issue
Block a user