mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-11-27 16:54:04 +00:00
Refactor how strings/numerics are parsed
Fixes issue `toString() radix argument must be between 2 and 36` due to `.toString("ascii")` conversion. This issue was introduced in 704ffd3f4b
.
This commit is contained in:
parent
f059d54349
commit
8e56a81ef1
1 changed files with 11 additions and 15 deletions
|
@ -52,40 +52,36 @@ class SNMPMonitorType extends MonitorType {
|
||||||
throw new Error(`No varbinds returned from SNMP session (OID: ${monitor.snmpOid})`);
|
throw new Error(`No varbinds returned from SNMP session (OID: ${monitor.snmpOid})`);
|
||||||
} else {
|
} else {
|
||||||
const value = varbinds[0].value;
|
const value = varbinds[0].value;
|
||||||
const numericValue = parseInt(value);
|
|
||||||
const stringValue = value.toString("ascii");
|
// Check if inputs are numeric. If not, re-parse as strings. This ensures comparisons are handled correctly.
|
||||||
|
let snmpValue = isNaN(value) ? value.toString() : parseFloat(value);
|
||||||
|
let snmpControlValue = isNaN(monitor.snmpControlValue) ? monitor.snmpControlValue.toString() : parseFloat(monitor.snmpControlValue);
|
||||||
|
|
||||||
switch (monitor.snmpCondition) {
|
switch (monitor.snmpCondition) {
|
||||||
case ">":
|
case ">":
|
||||||
heartbeat.status = numericValue > monitor.snmpControlValue ? UP : DOWN;
|
heartbeat.status = snmpValue > snmpControlValue ? UP : DOWN;
|
||||||
break;
|
break;
|
||||||
case ">=":
|
case ">=":
|
||||||
heartbeat.status = numericValue >= monitor.snmpControlValue ? UP : DOWN;
|
heartbeat.status = snmpValue >= snmpControlValue ? UP : DOWN;
|
||||||
break;
|
break;
|
||||||
case "<":
|
case "<":
|
||||||
heartbeat.status = numericValue < monitor.snmpControlValue ? UP : DOWN;
|
heartbeat.status = snmpValue < snmpControlValue ? UP : DOWN;
|
||||||
break;
|
break;
|
||||||
case "<=":
|
case "<=":
|
||||||
heartbeat.status = numericValue <= monitor.snmpControlValue ? UP : DOWN;
|
heartbeat.status = snmpValue <= snmpControlValue ? UP : DOWN;
|
||||||
break;
|
break;
|
||||||
case "==":
|
case "==":
|
||||||
if (!isNaN(value) && !isNaN(monitor.snmpControlValue)) {
|
heartbeat.status = snmpValue.toString() === snmpControlValue.toString() ? UP : DOWN;
|
||||||
// Both values are numeric, parse them as numbers
|
|
||||||
heartbeat.status = parseFloat(value) === parseFloat(monitor.snmpControlValue) ? UP : DOWN;
|
|
||||||
} else {
|
|
||||||
// At least one of the values is not numeric, compare them as strings
|
|
||||||
heartbeat.status = value.toString() === monitor.snmpControlValue.toString() ? UP : DOWN;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case "contains":
|
case "contains":
|
||||||
heartbeat.status = stringValue.includes(monitor.snmpControlValue) ? UP : DOWN;
|
heartbeat.status = snmpValue.toString().includes(snmpControlValue.toString()) ? UP : DOWN;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
heartbeat.status = DOWN;
|
heartbeat.status = DOWN;
|
||||||
heartbeat.msg = `Invalid condition: ${monitor.snmpCondition}`;
|
heartbeat.msg = `Invalid condition: ${monitor.snmpCondition}`;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
heartbeat.msg = "SNMP value " + (heartbeat.status ? "passes" : "does not pass") + ` comparison: ${value.toString("ascii")} ${monitor.snmpCondition} ${monitor.snmpControlValue}`;
|
heartbeat.msg = "SNMP value " + (heartbeat.status ? "passes" : "does not pass") + ` comparison: ${value.toString()} ${monitor.snmpCondition} ${snmpControlValue}`;
|
||||||
|
|
||||||
}
|
}
|
||||||
session.close();
|
session.close();
|
||||||
|
|
Loading…
Reference in a new issue