ES Lint Compliant

This commit is contained in:
Matt Visnovsky 2024-04-30 18:04:59 -06:00
parent ba47aca51f
commit 7459654e11
2 changed files with 20 additions and 14 deletions

View File

@ -11,12 +11,18 @@ class SNMPMonitorType extends MonitorType {
async check(monitor, heartbeat, _server) { async check(monitor, heartbeat, _server) {
const options = { const options = {
port: monitor.port || '161', port: monitor.port || "161",
retries: monitor.maxretries, retries: monitor.maxretries,
timeout: 1000, timeout: 1000,
version: getKey(snmp.Version, monitor.snmpVersion) || snmp.Version2c, version: getKey(snmp.Version, monitor.snmpVersion) || snmp.Version2c,
}; };
/**
* Retrieves the key from the provided object corresponding to the given value.
* @param {object} obj - The object to search.
* @param {*} value - The value to search for.
* @returns {string|null} - The key associated with the value, or null if not found.
*/
function getKey(obj, value) { function getKey(obj, value) {
return Object.keys(obj).find(key => obj[key] === value) || null; return Object.keys(obj).find(key => obj[key] === value) || null;
} }
@ -25,7 +31,7 @@ class SNMPMonitorType extends MonitorType {
const session = snmp.createSession(monitor.hostname, monitor.snmpCommunityString, options); const session = snmp.createSession(monitor.hostname, monitor.snmpCommunityString, options);
// Handle errors during session creation // Handle errors during session creation
session.on('error', (error) => { session.on("error", (error) => {
heartbeat.status = DOWN; heartbeat.status = DOWN;
heartbeat.msg = `SNMP: Error creating SNMP session: ${error.message}`; heartbeat.msg = `SNMP: Error creating SNMP session: ${error.message}`;
log.debug("monitor", heartbeat.msg); log.debug("monitor", heartbeat.msg);
@ -42,27 +48,27 @@ class SNMPMonitorType extends MonitorType {
}); });
}); });
if (varbinds.length === 0 || getKey(snmp.ObjectType, varbinds[0].type) === 'NoSuchInstance') { if (varbinds.length === 0 || getKey(snmp.ObjectType, varbinds[0].type) === "NoSuchInstance") {
throw new Error(`No varbinds returned from SNMP session (OID: ${monitor.snmpOid})`); throw new Error(`No varbinds returned from SNMP session (OID: ${monitor.snmpOid})`);
} else { } else {
const value = varbinds[0].value; const value = varbinds[0].value;
const numericValue = parseInt(value); const numericValue = parseInt(value);
const stringValue = value.toString('ascii'); const stringValue = value.toString("ascii");
switch (monitor.snmpCondition) { switch (monitor.snmpCondition) {
case '>': case ">":
heartbeat.status = numericValue > monitor.snmpControlValue ? UP : DOWN; heartbeat.status = numericValue > monitor.snmpControlValue ? UP : DOWN;
break; break;
case '>=': case ">=":
heartbeat.status = numericValue >= monitor.snmpControlValue ? UP : DOWN; heartbeat.status = numericValue >= monitor.snmpControlValue ? UP : DOWN;
break; break;
case '<': case "<":
heartbeat.status = numericValue < monitor.snmpControlValue ? UP : DOWN; heartbeat.status = numericValue < monitor.snmpControlValue ? UP : DOWN;
break; break;
case '<=': case "<=":
heartbeat.status = numericValue <= monitor.snmpControlValue ? UP : DOWN; heartbeat.status = numericValue <= monitor.snmpControlValue ? UP : DOWN;
break; break;
case '==': case "==":
if (!isNaN(value) && !isNaN(monitor.snmpControlValue)) { if (!isNaN(value) && !isNaN(monitor.snmpControlValue)) {
// Both values are numeric, parse them as numbers // Both values are numeric, parse them as numbers
heartbeat.status = parseFloat(value) === parseFloat(monitor.snmpControlValue) ? UP : DOWN; heartbeat.status = parseFloat(value) === parseFloat(monitor.snmpControlValue) ? UP : DOWN;
@ -71,7 +77,7 @@ class SNMPMonitorType extends MonitorType {
heartbeat.status = value.toString() === monitor.snmpControlValue.toString() ? UP : DOWN; heartbeat.status = value.toString() === monitor.snmpControlValue.toString() ? UP : DOWN;
} }
break; break;
case 'contains': case "contains":
heartbeat.status = stringValue.includes(monitor.snmpControlValue) ? UP : DOWN; heartbeat.status = stringValue.includes(monitor.snmpControlValue) ? UP : DOWN;
break; break;
default: default:
@ -79,7 +85,7 @@ class SNMPMonitorType extends MonitorType {
heartbeat.msg = `Invalid condition: ${monitor.snmpCondition}`; heartbeat.msg = `Invalid condition: ${monitor.snmpCondition}`;
break; break;
} }
heartbeat.msg = `SNMP value ` + (heartbeat.status ? `passes` : `does not pass`) + ` comparison: ${value.toString('ascii')} ${monitor.snmpCondition} ${monitor.snmpControlValue}`; heartbeat.msg = "SNMP value " + (heartbeat.status ? "passes" : "does not pass") + ` comparison: ${value.toString("ascii")} ${monitor.snmpCondition} ${monitor.snmpControlValue}`;
} }
session.close(); session.close();