feat: per-server notification thresholds with alert on agent metrics push

This commit is contained in:
2026-06-13 16:12:36 -04:00
parent b6c7275437
commit c63c5ffcfd
8 changed files with 181 additions and 1 deletions

View File

@@ -15,6 +15,7 @@ use ServerManager\Services\AuditService;
use ServerManager\Services\PermissionValidator;
use ServerManager\Services\FCMService;
use ServerManager\Models\CommandHistory;
use ServerManager\Models\Notification;
use ServerManager\Core\Validator;
class ApiController
@@ -434,6 +435,38 @@ class ApiController
$serverModel->updateAgentLastSeen((int) $server['id']);
$checks = [
'cpu' => ['val' => $cpu, 'warn' => $server['threshold_cpu_warning'], 'crit' => $server['threshold_cpu_critical']],
'ram' => ['val' => $ram, 'warn' => $server['threshold_ram_warning'], 'crit' => $server['threshold_ram_critical']],
'disk' => ['val' => $disk, 'warn' => $server['threshold_disk_warning'], 'crit' => $server['threshold_disk_critical']],
];
$breaches = [];
foreach ($checks as $metric => $c) {
if ($c['crit'] !== null && $c['val'] >= (float) $c['crit']) {
$breaches[] = ['metric' => $metric, 'level' => 'critical', 'current' => $c['val'], 'threshold' => $c['crit']];
} elseif ($c['warn'] !== null && $c['val'] >= (float) $c['warn']) {
$breaches[] = ['metric' => $metric, 'level' => 'warning', 'current' => $c['val'], 'threshold' => $c['warn']];
}
}
if (!empty($breaches)) {
$userIds = $serverModel->getAccessibleUserIds((int) $server['id']);
$notificationModel = new Notification();
foreach ($breaches as $b) {
$title = strtoupper($b['metric']) . " {$b['level']} on {$server['name']}";
$message = "{$server['name']}: {$b['metric']} at {$b['current']}% ({$b['level']} threshold: {$b['threshold']}%)";
foreach ($userIds as $uid) {
$notificationModel->create([
'user_id' => (int) $uid,
'title' => $title,
'message' => $message,
'type' => $b['level'],
]);
}
}
}
$this->view->json(['success' => true]);
}