Using JSON Query Expressions

Equivalent functionality as before, but we're now building json-query expressions for the user.
This commit is contained in:
Matt Visnovsky 2024-05-10 10:56:38 -06:00
parent c4759948ec
commit d25ee8f128

View file

@ -1,6 +1,7 @@
const { MonitorType } = require("./monitor-type"); const { MonitorType } = require("./monitor-type");
const { UP, DOWN, log } = require("../../src/util"); const { UP, DOWN, log } = require("../../src/util");
const snmp = require("net-snmp"); const snmp = require("net-snmp");
const jsonata = require("jsonata");
class SNMPMonitorType extends MonitorType { class SNMPMonitorType extends MonitorType {
name = "snmp"; name = "snmp";
@ -52,29 +53,31 @@ class SNMPMonitorType extends MonitorType {
let snmpValue = isNaN(value) ? value.toString() : parseFloat(value); let snmpValue = isNaN(value) ? value.toString() : parseFloat(value);
let snmpControlValue = isNaN(monitor.snmpControlValue) ? monitor.snmpControlValue.toString() : parseFloat(monitor.snmpControlValue); let snmpControlValue = isNaN(monitor.snmpControlValue) ? monitor.snmpControlValue.toString() : parseFloat(monitor.snmpControlValue);
let jsonQueryExpression;
switch (monitor.snmpCondition) { switch (monitor.snmpCondition) {
case ">": case ">":
heartbeat.status = snmpValue > snmpControlValue ? UP : DOWN;
break;
case ">=": case ">=":
heartbeat.status = snmpValue >= snmpControlValue ? UP : DOWN;
break;
case "<": case "<":
heartbeat.status = snmpValue < snmpControlValue ? UP : DOWN;
break;
case "<=": case "<=":
heartbeat.status = snmpValue <= snmpControlValue ? UP : DOWN; jsonQueryExpression = `$.value ${monitor.snmpCondition} $.control`;
break; break;
case "==": case "==":
heartbeat.status = snmpValue.toString() === snmpControlValue.toString() ? UP : DOWN; jsonQueryExpression = "$string($.value) = $string($.control)";
break; break;
case "contains": case "contains":
heartbeat.status = snmpValue.toString().includes(snmpControlValue.toString()) ? UP : DOWN; jsonQueryExpression = "$contains($string($.value), $string($.control))";
break; break;
default: default:
throw new Error(`Invalid condition ${monitor.snmpCondition}`); throw new Error(`Invalid condition ${monitor.snmpCondition}`);
} }
heartbeat.msg = "SNMP value " + (heartbeat.status ? "passes" : "does not pass") + ` comparison: ${value.toString()} ${monitor.snmpCondition} ${snmpControlValue}`;
const expression = jsonata(jsonQueryExpression);
const result = await expression.evaluate({
value: snmpValue,
control: monitor.snmpControlValue
});
heartbeat.status = result ? UP : DOWN;
heartbeat.msg = `SNMP value ${result ? "passes" : "does not pass"} comparison: ${snmpValue} ${monitor.snmpCondition} ${snmpControlValue}`;
} catch (err) { } catch (err) {
heartbeat.status = DOWN; heartbeat.status = DOWN;