fix(checkmk): correct columns query param format for /objects/host endpoint

This commit is contained in:
Brückner
2026-06-05 08:57:52 +02:00
parent 744468c13d
commit 20308b53d6

View File

@ -690,9 +690,9 @@ async function startServer() {
// in Settings take effect on the next cycle without a server restart. // in Settings take effect on the next cycle without a server restart.
// ------------------------------------------------------------- // -------------------------------------------------------------
function checkmkHttpHint(status: number): string { function checkmkHttpHint(status: number): string {
if (status === 401) return 'HTTP 401 Unauthorized wrong automation user or secret (check Settings → CheckMK)'; if (status === 401) return 'HTTP 401 Unauthorized - wrong automation user or secret (check Settings → CheckMK)';
if (status === 403) return 'HTTP 403 Forbidden automation user lacks permission in CheckMK'; if (status === 403) return 'HTTP 403 Forbidden - automation user lacks permission in CheckMK';
if (status === 404) return 'HTTP 404 Not Found API URL incorrect or site name wrong'; if (status === 404) return 'HTTP 404 Not Found - API URL incorrect or site name wrong';
return `HTTP ${status}`; return `HTTP ${status}`;
} }
@ -746,7 +746,6 @@ async function startServer() {
// minimal fields without state, so per-host calls are required. // minimal fields without state, so per-host calls are required.
const rows = db.prepare('SELECT id, hostname, ip, status FROM devices').all() as { id: string; hostname: string; ip: string; status: string }[]; const rows = db.prepare('SELECT id, hostname, ip, status FROM devices').all() as { id: string; hostname: string; ip: string; status: string }[];
const counts = { online: 0, offline: 0, unknown: 0 }; const counts = { online: 0, offline: 0, unknown: 0 };
let loggedFirstExtensions = false;
for (const dev of rows) { for (const dev of rows) {
const cmkHost = ipToHostname.get(dev.ip); const cmkHost = ipToHostname.get(dev.ip);
@ -760,24 +759,14 @@ async function startServer() {
continue; continue;
} }
try { try {
// Request explicit columns so CheckMK includes monitoring state in the response.
const colParam = encodeURIComponent(JSON.stringify(['name', 'state', 'has_been_checked']));
const hostRes = await fetch( const hostRes = await fetch(
`${CHECKMK_API_URL}/objects/host/${encodeURIComponent(cmkHost)}?columns=${colParam}`, `${CHECKMK_API_URL}/objects/host/${encodeURIComponent(cmkHost)}?columns=state&columns=hard_state&columns=has_been_checked`,
{ headers } { headers }
); );
if (!hostRes.ok) throw new Error(checkmkHttpHint(hostRes.status)); if (!hostRes.ok) throw new Error(checkmkHttpHint(hostRes.status));
const hostData = await hostRes.json(); const hostData = await hostRes.json();
// Log full raw response once so we can see all available fields and paths. const state: number = hostData?.extensions?.state ?? -1;
if (!loggedFirstExtensions) {
loggedFirstExtensions = true;
db.prepare('INSERT INTO logs (id, timestamp, type, message) VALUES (?, ?, ?, ?)')
.run(uid('log'), now, 'system',
`CheckMK per-host full response for "${cmkHost}": ${JSON.stringify(hostData).substring(0, 600)}`);
}
const state: number = hostData?.extensions?.state ?? hostData?.state ?? -1;
const newStatus = state === 0 ? 'online' : state === 1 || state === 2 ? 'offline' : 'unknown'; const newStatus = state === 0 ? 'online' : state === 1 || state === 2 ? 'offline' : 'unknown';
db.prepare('UPDATE devices SET status = ?, lastCheckedAt = ? WHERE id = ?').run(newStatus, now, dev.id); db.prepare('UPDATE devices SET status = ?, lastCheckedAt = ? WHERE id = ?').run(newStatus, now, dev.id);
if (dev.status !== newStatus) { if (dev.status !== newStatus) {