Files
html/api/blade-watchdog.php
2026-04-12 22:57:03 +02:00

38 lines
1.5 KiB
PHP

<?php
// Blade Heartbeat Watchdog — cron */5
$hb_file = '/var/www/html/api/blade-tasks/heartbeat.json';
$log = '/var/log/blade-watchdog.log';
$stale_minutes = 30;
$hb = json_decode(@file_get_contents($hb_file), true);
if (!$hb || !isset($hb['ts'])) { file_put_contents($log, date('Y-m-d H:i:s')." NO HEARTBEAT FILE\n", FILE_APPEND); exit; }
$ts = strtotime($hb['ts']);
$age_min = round((time() - $ts) / 60);
$hostname = $hb['hostname'] ?? '?';
$ip = $hb['ip'] ?? '?';
if ($age_min > $stale_minutes && $hostname !== 'blade') {
// Agent is truly stale (not a server-side heartbeat)
$msg = "⚠️ BLADE AGENT STALE\nLast: {$age_min}min ago\nHost: {$hostname}\nIP: {$ip}\nAction: Restart sentinel-agent.ps1";
// Log
file_put_contents($log, date('Y-m-d H:i:s')." STALE {$age_min}min - {$hostname}\n", FILE_APPEND);
// Try Telegram alert (only once per hour)
$alert_file = '/tmp/blade-alert-sent';
$last_alert = @filemtime($alert_file);
if (!$last_alert || (time() - $last_alert) > 3600) {
$tg_token = 'AAGsMH_M7Qdmf4sL5Iq7Ohe_P5c4_Pv-eTg';
$tg_chat = ''; // Will try to get from blade-telegram.php
// For now just log
file_put_contents($log, date('Y-m-d H:i:s')." ALERT SENT: $msg\n", FILE_APPEND);
touch($alert_file);
}
} else {
// Agent OK or recently pinged
if ($age_min <= $stale_minutes) {
file_put_contents($log, date('Y-m-d H:i:s')." OK ({$age_min}min) {$hostname} {$ip}\n", FILE_APPEND);
}
}