From 4a882be6bac6884a84acf930645ae2f93c18ec7f Mon Sep 17 00:00:00 2001 From: Matt Visnovsky Date: Mon, 29 Apr 2024 15:59:59 -0600 Subject: [PATCH] Further SNMP monitor development Further testing of SNMP feat, however I'm running into the issue `Error in SNMP check: RequestTimedOutError: Request timed out` when the check function is called. I am unsure as to why since my local SNMP script works great with very similar code. --- server/monitor-types/snmp.js | 65 +++++++++++++++++++++++++----------- src/pages/EditMonitor.vue | 10 ++++-- 2 files changed, 52 insertions(+), 23 deletions(-) diff --git a/server/monitor-types/snmp.js b/server/monitor-types/snmp.js index 433fc527f..92c0f4875 100644 --- a/server/monitor-types/snmp.js +++ b/server/monitor-types/snmp.js @@ -12,51 +12,76 @@ class SNMPMonitorType extends MonitorType { * @param {object} _server Unused server object. */ async check(monitor, heartbeat, _server) { + + console.log("IP Address:", monitor._hostname); + console.log("SNMP Community String:", monitor._snmpCommunityString); + console.log("SNMP OID:", monitor._snmpOid); + console.log("SNMP Version:", monitor._snmpVersion); + console.log("SNMP Condition:", monitor._snmpCondition); + console.log("SNMP Control Value:", monitor._snmpControlValue); + + const options = { + port: monitor._port || 161, + retries: 1, + timeout: 1000, + version: getKey(snmp.Version, monitor._snmpVersion) || snmp.Version2c, + }; + + function getKey(obj, value) { + return Object.keys(obj).find(key => obj[key] === value) || null; + } + try { - const session = new snmp.Session({ host: monitor.ipAddress, community: monitor.snmpCommunityString, version: monitor.snmpVersion }); + const session = snmp.createSession(monitor._ipAddress, monitor._snmpCommunityString, options); - session.get({ oid: monitor.snmpOid }, (err, varbinds) => { - if (err) { - heartbeat.status = DOWN; - heartbeat.msg = `Error: ${err.message}`; - return; - } + const varbinds = await new Promise((resolve, reject) => { + session.get([monitor._snmpOid], (error, varbinds) => { + if (error) { + reject(error); + } else { + resolve(varbinds); + } + }); + }); - // Assuming only one varbind is returned + console.log("Received varbinds:", varbinds); // Log the received varbinds for debugging + + if (varbinds && varbinds.length > 0) { const value = varbinds[0].value; - - // Convert value to appropriate type based on SNMP type (assuming it's integer or string for simplicity) const numericValue = parseInt(value); const stringValue = value.toString(); - // Check against condition and control value switch (monitor.snmpCondition) { case '>': - heartbeat.status = numericValue > monitor.snmpControlValue ? UP : DOWN; + heartbeat.status = numericValue > monitor._snmpControlValue ? UP : DOWN; break; case '>=': - heartbeat.status = numericValue >= monitor.snmpControlValue ? UP : DOWN; + heartbeat.status = numericValue >= monitor._snmpControlValue ? UP : DOWN; break; case '<': - heartbeat.status = numericValue < monitor.snmpControlValue ? UP : DOWN; + heartbeat.status = numericValue < monitor._snmpControlValue ? UP : DOWN; break; case '<=': - heartbeat.status = numericValue <= monitor.snmpControlValue ? UP : DOWN; + heartbeat.status = numericValue <= monitor._snmpControlValue ? UP : DOWN; break; case '==': - heartbeat.status = value === monitor.snmpControlValue ? UP : DOWN; + heartbeat.status = value === monitor._snmpControlValue ? UP : DOWN; break; case 'contains': - heartbeat.status = stringValue.includes(monitor.snmpControlValue) ? UP : DOWN; + heartbeat.status = stringValue.includes(monitor._snmpControlValue) ? UP : DOWN; break; default: heartbeat.status = DOWN; - heartbeat.msg = `Invalid condition: ${monitor.snmpCondition}`; + heartbeat.msg = `Invalid condition: ${monitor._snmpCondition}`; } + } else { + heartbeat.status = DOWN; + heartbeat.msg = 'No varbinds returned from SNMP session'; + } - session.close(); - }); + session.close(); // Close the session after use } catch (err) { + console.error("Error in SNMP check:", err); // Log any errors heartbeat.status = DOWN; heartbeat.msg = `Error: ${err.message}`; } diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index d082d3e2f..c21c4a14b 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -924,9 +924,6 @@ const monitorDefaults = { kafkaProducerAllowAutoTopicCreation: false, gamedigGivenPortOnly: true, remote_browser: null, - port: 161, - communityString: 'public', - oid: '1.3.6.1.2.1.1.1.0', }; export default { @@ -1243,11 +1240,18 @@ message HealthCheckResponse { this.monitor.port = "53"; } else if (this.monitor.type === "radius") { this.monitor.port = "1812"; + } else if (this.monitor.type === "snmp") { + this.monitor.port = "161"; } else { this.monitor.port = undefined; } } + // Set default SNMP version + if (!this.monitor.snmpVersion) { + this.monitor.snmpVersion = "2c"; + } + // Get the game list from server if (this.monitor.type === "gamedig") { this.$root.getSocket().emit("getGameList", (res) => {