fix(checkmk): correct columns query param format for /objects/host endpoint
This commit is contained in:
21
server.ts
21
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) {
|
||||
|
||||
Reference in New Issue
Block a user