Variable changes

-Reuse expected_value for snmpControlValue
-Create jsonPathOperator for snmpCondition
This commit is contained in:
Matt Visnovsky 2024-06-05 15:37:47 -06:00
parent d25ee8f128
commit 7eee5db4d2
5 changed files with 17 additions and 21 deletions

View file

@ -4,8 +4,7 @@ exports.up = function (knex) {
table.string("snmp_community_string", 255).defaultTo("public");
table.string("snmp_oid").defaultTo(null);
table.enum("snmp_version", [ "1", "2c", "3" ]).defaultTo("2c");
table.float("snmp_control_value").defaultTo(null);
table.string("snmp_condition").defaultTo(null);
table.string("json_path_operator").defaultTo(null);
});
};
@ -14,7 +13,6 @@ exports.down = function (knex) {
table.dropColumn("snmp_community_string");
table.dropColumn("snmp_oid");
table.dropColumn("snmp_version");
table.dropColumn("snmp_control_value");
table.dropColumn("snmp_condition");
table.dropColumn("json_path_operator");
});
};

View file

@ -162,8 +162,7 @@ class Monitor extends BeanModel {
screenshot,
remote_browser: this.remote_browser,
snmpOid: this.snmpOid,
snmpCondition: this.snmpCondition,
snmpControlValue: this.snmpControlValue,
jsonPathOperator: this.jsonPathOperator,
snmpVersion: this.snmpVersion,
};

View file

@ -50,16 +50,16 @@ class SNMPMonitorType extends MonitorType {
const value = varbinds[0].value;
// 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);
const expectedValue = isNaN(monitor.expectedValue) ? monitor.expectedValue.toString() : parseFloat(monitor.expectedValue);
let snmpResponse = isNaN(value) ? value.toString() : parseFloat(value);
let jsonQueryExpression;
switch (monitor.snmpCondition) {
switch (monitor.jsonPathOperator) {
case ">":
case ">=":
case "<":
case "<=":
jsonQueryExpression = `$.value ${monitor.snmpCondition} $.control`;
jsonQueryExpression = `$.value ${monitor.jsonPathOperator} $.control`;
break;
case "==":
jsonQueryExpression = "$string($.value) = $string($.control)";
@ -68,13 +68,13 @@ class SNMPMonitorType extends MonitorType {
jsonQueryExpression = "$contains($string($.value), $string($.control))";
break;
default:
throw new Error(`Invalid condition ${monitor.snmpCondition}`);
throw new Error(`Invalid condition ${monitor.jsonPathOperator}`);
}
const expression = jsonata(jsonQueryExpression);
const result = await expression.evaluate({
value: snmpValue,
control: monitor.snmpControlValue
const evaluation = await expression.evaluate({
value: snmpResponse,
control: expectedValue
});
heartbeat.status = result ? UP : DOWN;
heartbeat.msg = `SNMP value ${result ? "passes" : "does not pass"} comparison: ${snmpValue} ${monitor.snmpCondition} ${snmpControlValue}`;

View file

@ -832,8 +832,7 @@ let needSetup = false;
bean.remote_browser = monitor.remote_browser;
bean.snmpVersion = monitor.snmpVersion;
bean.snmpOid = monitor.snmpOid;
bean.snmpCondition = monitor.snmpCondition;
bean.snmpControlValue = monitor.snmpControlValue;
bean.jsonPathOperator = monitor.jsonPathOperator;
bean.timeout = monitor.timeout;
bean.validate();

View file

@ -280,8 +280,8 @@
<div v-if="monitor.type === 'snmp'" class="my-3">
<div class="d-flex align-items-start">
<div class="me-2">
<label for="snmp_condition" class="form-label">{{ $t("Condition") }}</label>
<select id="snmp_condition" v-model="monitor.snmpCondition" class="form-select me-3" required>
<label for="json_path_operator" class="form-label">{{ $t("Condition") }}</label>
<select id="json_path_operator" v-model="monitor.jsonPathOperator" class="form-select me-3" required>
<option value=">">&gt;</option>
<option value=">=">&gt;=</option>
<option value="<">&lt;</option>
@ -291,9 +291,9 @@
</select>
</div>
<div class="flex-grow-1">
<label for="snmp_control_value" class="form-label">{{ $t("Control Value") }}</label>
<input v-if="monitor.snmpCondition !== 'contains' && monitor.snmpCondition !== '=='" id="snmp_control_value" v-model="monitor.snmpControlValue" type="number" class="form-control" required step=".01">
<input v-else id="snmp_control_value" v-model="monitor.snmpControlValue" type="text" class="form-control" required>
<label for="expectedValue" class="form-label">{{ $t("Expected Value (Control)") }}</label>
<input v-if="monitor.jsonPathOperator !== 'contains' && monitor.jsonPathOperator !== '==' && monitor.jsonPathOperator !== 'custom'" id="expectedValue" v-model="monitor.expectedValue" type="number" class="form-control" required step=".01">
<input v-else id="expectedValue" v-model="monitor.expectedValue" type="text" class="form-control" required>
</div>
</div>
</div>