From 20308b53d690a988a65a96d53e35cbf551632b30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Br=C3=BCckner?= Date: Fri, 5 Jun 2026 08:57:52 +0200 Subject: [PATCH] fix(checkmk): correct columns query param format for /objects/host endpoint --- server.ts | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/server.ts b/server.ts index 1b1254e..e7da6d3 100644 --- a/server.ts +++ b/server.ts @@ -690,9 +690,9 @@ async function startServer() { // in Settings take effect on the next cycle without a server restart. // ------------------------------------------------------------- function checkmkHttpHint(status: number): string { - 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 === 404) return 'HTTP 404 Not Found — API URL incorrect or site name wrong'; + 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 === 404) return 'HTTP 404 Not Found - API URL incorrect or site name wrong'; return `HTTP ${status}`; } @@ -746,7 +746,6 @@ async function startServer() { // 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 counts = { online: 0, offline: 0, unknown: 0 }; - let loggedFirstExtensions = false; for (const dev of rows) { const cmkHost = ipToHostname.get(dev.ip); @@ -760,24 +759,14 @@ async function startServer() { continue; } 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( - `${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 } ); if (!hostRes.ok) throw new Error(checkmkHttpHint(hostRes.status)); const hostData = await hostRes.json(); - // Log full raw response once so we can see all available fields and paths. - 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 state: number = hostData?.extensions?.state ?? -1; 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); if (dev.status !== newStatus) {