From 7eee5db4d2986e6b5752557ff92c138960a3e97c Mon Sep 17 00:00:00 2001 From: Matt Visnovsky Date: Wed, 5 Jun 2024 15:37:47 -0600 Subject: [PATCH] Variable changes -Reuse expected_value for snmpControlValue -Create jsonPathOperator for snmpCondition --- .../2024-04-26-0000-snmp-monitor.js | 6 ++---- server/model/monitor.js | 3 +-- server/monitor-types/snmp.js | 16 ++++++++-------- server/server.js | 3 +-- src/pages/EditMonitor.vue | 10 +++++----- 5 files changed, 17 insertions(+), 21 deletions(-) diff --git a/db/knex_migrations/2024-04-26-0000-snmp-monitor.js b/db/knex_migrations/2024-04-26-0000-snmp-monitor.js index 988828c0..1ecf61ba 100644 --- a/db/knex_migrations/2024-04-26-0000-snmp-monitor.js +++ b/db/knex_migrations/2024-04-26-0000-snmp-monitor.js @@ -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"); }); }; diff --git a/server/model/monitor.js b/server/model/monitor.js index 3b267a60..55ccfd58 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -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, }; diff --git a/server/monitor-types/snmp.js b/server/monitor-types/snmp.js index 60c46187..0964acce 100644 --- a/server/monitor-types/snmp.js +++ b/server/monitor-types/snmp.js @@ -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}`; diff --git a/server/server.js b/server/server.js index c6127762..e66c3e39 100644 --- a/server/server.js +++ b/server/server.js @@ -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(); diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index bb8c84ed..fa49d857 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -280,8 +280,8 @@
- - @@ -291,9 +291,9 @@
- - - + + +