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:
Matt Visnovsky 2024-05-02 15:11:03 -06:00
parent f059d54349
commit 8e56a81ef1

View file

@ -52,40 +52,36 @@ class SNMPMonitorType extends MonitorType {
throw new Error(`No varbinds returned from SNMP session (OID: ${monitor.snmpOid})`);
} else {
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) {
case ">":
heartbeat.status = numericValue > monitor.snmpControlValue ? UP : DOWN;
heartbeat.status = snmpValue > snmpControlValue ? UP : DOWN;
break;
case ">=":
heartbeat.status = numericValue >= monitor.snmpControlValue ? UP : DOWN;
heartbeat.status = snmpValue >= snmpControlValue ? UP : DOWN;
break;
case "<":
heartbeat.status = numericValue < monitor.snmpControlValue ? UP : DOWN;
heartbeat.status = snmpValue < snmpControlValue ? UP : DOWN;
break;
case "<=":
heartbeat.status = numericValue <= monitor.snmpControlValue ? UP : DOWN;
heartbeat.status = snmpValue <= snmpControlValue ? UP : DOWN;
break;
case "==":
if (!isNaN(value) && !isNaN(monitor.snmpControlValue)) {
// 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;
}
heartbeat.status = snmpValue.toString() === snmpControlValue.toString() ? UP : DOWN;
break;
case "contains":
heartbeat.status = stringValue.includes(monitor.snmpControlValue) ? UP : DOWN;
heartbeat.status = snmpValue.toString().includes(snmpControlValue.toString()) ? UP : DOWN;
break;
default:
heartbeat.status = DOWN;
heartbeat.msg = `Invalid condition: ${monitor.snmpCondition}`;
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();