From b5a73e5ad7f54c3bc8dbe7312072fa238212279f Mon Sep 17 00:00:00 2001 From: Matt Visnovsky Date: Wed, 12 Jun 2024 14:25:25 -0600 Subject: [PATCH] Apply suggestions from code review Co-authored-by: Frank Elsinga --- server/model/monitor.js | 9 ++++++--- server/monitor-types/snmp.js | 29 ++++++++++++++--------------- src/pages/EditMonitor.vue | 2 +- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index c76e303ad..281f56214 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -602,9 +602,12 @@ class Monitor extends BeanModel { const { status, response } = await evaluateJsonQuery(data, this.jsonPath, this.jsonPathOperator, this.expectedValue); - bean.status = status ? UP : DOWN; - bean.msg = `JSON query ${status ? "passes" : "does not pass"} `; - bean.msg += `comparison: ${response} ${this.jsonPathOperator} ${this.expectedValue}.`; + if (status) { + bean.status = UP; + bean.msg = `JSON query passes (comparing ${response} ${this.jsonPathOperator} ${this.expectedValue})` + } else { + throw new Error(`JSON query does not pass (comparing ${response} ${this.jsonPathOperator} ${this.expectedValue})`); + } } diff --git a/server/monitor-types/snmp.js b/server/monitor-types/snmp.js index 5168cc9ad..07398853b 100644 --- a/server/monitor-types/snmp.js +++ b/server/monitor-types/snmp.js @@ -9,17 +9,15 @@ class SNMPMonitorType extends MonitorType { * @inheritdoc */ async check(monitor, heartbeat, _server) { - - const options = { - port: monitor.port || "161", - retries: monitor.maxretries, - timeout: monitor.timeout * 1000, - version: snmp.Version[monitor.snmpVersion], - }; - let session; try { - session = snmp.createSession(monitor.hostname, monitor.radiusPassword, options); + const sessionOptions = { + port: monitor.port || "161", + retries: monitor.maxretries, + timeout: monitor.timeout * 1000, + version: snmp.Version[monitor.snmpVersion], + }; + session = snmp.createSession(monitor.hostname, monitor.radiusPassword, sessionOptions); // Handle errors during session creation session.on("error", (error) => { @@ -46,13 +44,14 @@ class SNMPMonitorType extends MonitorType { const { status, response } = await evaluateJsonQuery(value, monitor.jsonPath, monitor.jsonPathOperator, monitor.expectedValue); - heartbeat.status = status ? UP : DOWN; - heartbeat.msg = `SNMP value ${status ? "passes" : "does not pass"} `; - heartbeat.msg += `comparison: ${response} ${monitor.jsonPathOperator} ${monitor.expectedValue}.`; - + if (status) { + bean.status = UP; + bean.msg = `JSON query passes (comparing ${response} ${this.jsonPathOperator} ${this.expectedValue})` + } else { + throw new Error(`JSON query does not pass (comparing ${response} ${this.jsonPathOperator} ${this.expectedValue})`); + } } catch (err) { - heartbeat.status = DOWN; - heartbeat.msg = `Error: ${err.message}`; + throw err; } finally { if (session) { session.close(); diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index 21f5154cc..7a5954b60 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -1354,7 +1354,7 @@ message HealthCheckResponse { // Set default SNMP version if (!this.monitor.snmpVersion) { - this.monitor.snmpVersion = "1"; + this.monitor.snmpVersion = "2c"; } // Set default jsonPath