feat: per-server notification thresholds with alert on agent metrics push
This commit is contained in:
@@ -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]);
|
||||
}
|
||||
|
||||
|
||||
@@ -1969,4 +1969,24 @@ class ServerController
|
||||
]);
|
||||
}
|
||||
|
||||
public function saveThresholds(int $id): void
|
||||
{
|
||||
$server = $this->requireServerAccess($id, 'manager');
|
||||
$role = $this->serverModel->getUserRole($id, (int) Session::get('user_id'));
|
||||
|
||||
if ($role !== 'owner') {
|
||||
$this->view->json(['success' => false, 'error' => 'Only the server owner can modify notification thresholds.'], 403);
|
||||
}
|
||||
|
||||
$fields = ['cpu_warning', 'cpu_critical', 'ram_warning', 'ram_critical', 'disk_warning', 'disk_critical'];
|
||||
$data = [];
|
||||
foreach ($fields as $f) {
|
||||
$key = 'threshold_' . $f;
|
||||
$data[$key] = isset($_POST[$f]) ? max(0, min(100, (float) $_POST[$f])) : null;
|
||||
}
|
||||
|
||||
$this->serverModel->update($id, $data);
|
||||
$this->auditService->log('thresholds_updated', 'server', $id, $data);
|
||||
$this->view->json(['success' => true, 'message' => 'Notification thresholds updated.']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,6 +88,11 @@ class Database
|
||||
return $this->query($sql, $params)->fetchAll();
|
||||
}
|
||||
|
||||
public function fetchCol(string $sql, array $params = []): array
|
||||
{
|
||||
return $this->query($sql, $params)->fetchAll(\PDO::FETCH_COLUMN);
|
||||
}
|
||||
|
||||
public function insert(string $table, array $data): int
|
||||
{
|
||||
$columns = implode(', ', array_keys($data));
|
||||
|
||||
@@ -410,6 +410,28 @@ class Server
|
||||
);
|
||||
}
|
||||
|
||||
public function getAccessibleUserIds(int $serverId): array
|
||||
{
|
||||
$direct = $this->db->fetchCol(
|
||||
'SELECT user_id FROM server_user WHERE server_id = ? AND status = \'active\'',
|
||||
[$serverId]
|
||||
);
|
||||
|
||||
$team = $this->db->fetchCol(
|
||||
'SELECT DISTINCT tu.user_id FROM team_user tu
|
||||
JOIN team_server ts ON tu.team_id = ts.team_id
|
||||
WHERE ts.server_id = ? AND tu.status = \'active\' AND ts.status = \'active\'',
|
||||
[$serverId]
|
||||
);
|
||||
|
||||
$owner = $this->db->fetchCol(
|
||||
'SELECT created_by FROM servers WHERE id = ? AND status = \'active\'',
|
||||
[$serverId]
|
||||
);
|
||||
|
||||
return array_unique(array_merge($direct, $team, $owner));
|
||||
}
|
||||
|
||||
public function findByAgentKey(string $key): ?array
|
||||
{
|
||||
$server = $this->db->fetch(
|
||||
|
||||
Reference in New Issue
Block a user