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.
This commit is contained in:
Matt Visnovsky 2024-04-29 15:59:59 -06:00
parent ff5890a11f
commit 4a882be6ba
2 changed files with 52 additions and 23 deletions

View file

@ -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}`;
}

View file

@ -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) => {