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

@ -3,7 +3,7 @@ exports.up = function (knex) {
.alterTable("monitor", function (table) {
table.string("snmp_community_string", 255).defaultTo("public"); // Add snmp_community_string column
table.string("snmp_oid").defaultTo(null); // Add oid column
table.enum("snmp_version", ["1", "2c", "3"]).defaultTo("2c"); // Add snmp_version column with enum values
table.enum("snmp_version", [ "1", "2c", "3" ]).defaultTo("2c"); // Add snmp_version column with enum values
table.float("snmp_control_value").defaultTo(null); // Add control_value column as float
table.string("snmp_condition").defaultTo(null); // Add oid column
});
@ -11,4 +11,4 @@ exports.up = function (knex) {
exports.down = function (knex) {
// Nothing to do here
};
};

View File

@ -11,12 +11,18 @@ class SNMPMonitorType extends MonitorType {
async check(monitor, heartbeat, _server) {
const options = {
port: monitor.port || '161',
port: monitor.port || "161",
retries: monitor.maxretries,
timeout: 1000,
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) {
return Object.keys(obj).find(key => obj[key] === value) || null;
}
@ -25,14 +31,14 @@ class SNMPMonitorType extends MonitorType {
const session = snmp.createSession(monitor.hostname, monitor.snmpCommunityString, options);
// Handle errors during session creation
session.on('error', (error) => {
session.on("error", (error) => {
heartbeat.status = DOWN;
heartbeat.msg = `SNMP: Error creating SNMP session: ${error.message}`;
log.debug("monitor", heartbeat.msg);
});
const varbinds = await new Promise((resolve, reject) => {
session.get([monitor.snmpOid], (error, varbinds) => {
session.get([ monitor.snmpOid ], (error, varbinds) => {
if (error) {
reject(error);
} else {
@ -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})`);
} else {
const value = varbinds[0].value;
const numericValue = parseInt(value);
const stringValue = value.toString('ascii');
const stringValue = value.toString("ascii");
switch (monitor.snmpCondition) {
case '>':
case ">":
heartbeat.status = numericValue > monitor.snmpControlValue ? UP : DOWN;
break;
case '>=':
case ">=":
heartbeat.status = numericValue >= monitor.snmpControlValue ? UP : DOWN;
break;
case '<':
case "<":
heartbeat.status = numericValue < monitor.snmpControlValue ? UP : DOWN;
break;
case '<=':
case "<=":
heartbeat.status = numericValue <= monitor.snmpControlValue ? UP : DOWN;
break;
case '==':
case "==":
if (!isNaN(value) && !isNaN(monitor.snmpControlValue)) {
// Both values are numeric, parse them as numbers
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;
}
break;
case 'contains':
case "contains":
heartbeat.status = stringValue.includes(monitor.snmpControlValue) ? UP : DOWN;
break;
default:
@ -79,7 +85,7 @@ class SNMPMonitorType extends MonitorType {
heartbeat.msg = `Invalid condition: ${monitor.snmpCondition}`;
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();