fix(checkmk): detect empty monitoring collection, log permission hint + host_config probe

This commit is contained in:
Brückner
2026-06-04 15:04:19 +02:00
parent 789fe1f8e0
commit 9fba11ccd6

View File

@ -758,12 +758,32 @@ async function startServer() {
if (name !== undefined && state !== undefined) hostnameToState.set(name, state);
}
// Diagnostic: log a sample so mismatches between config and monitoring IDs are visible
const cfgSample = [...ipToHostname.values()].slice(0, 3).join(', ');
const monSample = [...hostnameToState.keys()].slice(0, 3).join(', ');
if (hostnameToState.size === 0 && ipToHostname.size > 0) {
// Monitoring collection empty despite config hosts existing.
// Likely cause: automation user lacks "Use monitoring" permission in CheckMK.
// Falling back: probe one host via /objects/host_config/{name} and log its raw extensions
// so we can identify which field carries the reachability state.
db.prepare('INSERT INTO logs (id, timestamp, type, message) VALUES (?, ?, ?, ?)')
.run(uid('log'), now, 'system',
`CheckMK diagnostic — config hosts (${ipToHostname.size}): [${cfgSample}] | monitoring hosts (${hostnameToState.size}): [${monSample}]`);
'CheckMK: monitoring collection returned 0 hosts. The automation user may need ' +
'"Use monitoring" permission (Setup → Users → Roles). ' +
'Attempting per-host fallback via host_config endpoint.');
const firstHostname = [...ipToHostname.values()][0];
try {
const probeRes = await fetch(
`${CHECKMK_API_URL}/objects/host_config/${encodeURIComponent(firstHostname)}`,
{ headers }
);
if (probeRes.ok) {
const probeData = await probeRes.json();
const extJson = JSON.stringify(probeData?.extensions ?? {}).substring(0, 400);
db.prepare('INSERT INTO logs (id, timestamp, type, message) VALUES (?, ?, ?, ?)')
.run(uid('log'), now, 'system',
`CheckMK probe extensions for "${firstHostname}": ${extJson}`);
}
} catch { /* probe is best-effort */ }
}
} catch (err: any) {
const msg = `CheckMK sync failed — could not fetch monitoring states: ${err?.message ?? err}`;
console.error('[CheckMK]', msg);