refactor: consolidate metric collection into single SSH calls and add support for SSH key authentication

This commit is contained in:
infosave2007
2026-04-24 07:07:57 +03:00
parent 4c4b682256
commit 8eed687f66
3 changed files with 184 additions and 135 deletions
+14 -2
View File
@@ -25,15 +25,27 @@ ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', '/var/log/metrics_collector_errors.log');
// Prevent multiple instances using flock (#42)
$lockFile = '/var/run/collect_metrics.lock';
$lockFp = fopen($lockFile, 'w');
if (!$lockFp || !flock($lockFp, LOCK_EX | LOCK_NB)) {
echo "[" . date('Y-m-d H:i:s') . "] Another collector instance is already running. Exiting.\n";
exit(0);
}
// 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) {
// Register shutdown function to clean up PID and lock files
register_shutdown_function(function() use ($pidFile, $lockFp, $lockFile) {
if (file_exists($pidFile)) {
unlink($pidFile);
}
if ($lockFp) {
flock($lockFp, LOCK_UN);
fclose($lockFp);
}
});
echo "[" . date('Y-m-d H:i:s') . "] Metrics collector started (PID: " . getmypid() . ")\n";