Skip to content

Commit

Permalink
SNMP Initial Commits
Browse files Browse the repository at this point in the history
This commit introduces a new SNMP monitor feature to the application, allowing users to monitor devices using SNMP (Simple Network Management Protocol).
  • Loading branch information
mattv8 committed Apr 27, 2024
1 parent bab427f commit d92003e
Show file tree
Hide file tree
Showing 7 changed files with 631 additions and 564 deletions.
10 changes: 10 additions & 0 deletions db/knex_migrations/2024-04-26-0000-snmp-monitor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
exports.up = function (knex) {
return knex.schema
.alterTable("monitor", function (table) {
table.string("snmp_community_string", 255).defaultTo("public"); // Add community_string column
table.string("snmp_oid").notNullable(); // Add oid column
table.enum("snmp_version", ["1", "2c", "3"]).defaultTo("2c"); // Add snmp_version column with enum values
table.float("snmp_control_value").notNullable(); // Add control_value column as float
table.string("snmp_condition").notNullable(); // Add oid column
});
};
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
"redbean-node": "~0.3.0",
"redis": "~4.5.1",
"semver": "~7.5.4",
"snmp-native": "^1.2.0",
"socket.io": "~4.6.1",
"socket.io-client": "~4.6.1",
"socks-proxy-agent": "6.1.1",
Expand Down
69 changes: 69 additions & 0 deletions server/monitor-types/snmp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const { MonitorType } = require("./monitor-type");
const { UP, DOWN } = require("../../src/util");
const snmp = require("snmp-native");

class SNMPMonitorType extends MonitorType {
name = "snmp";

/**
* Checks the SNMP value against the condition and control value.
* @param {object} monitor The monitor object associated with the check.
* @param {object} heartbeat The heartbeat object to update.
* @param {object} _server Unused server object.
*/
async check(monitor, heartbeat, _server) {
try {
const session = new snmp.Session({ host: monitor.ipAddress, community: monitor.snmpCommunityString, version: monitor.snmpVersion });

session.get({ oid: monitor.snmpOid }, (err, varbinds) => {
if (err) {
heartbeat.status = DOWN;
heartbeat.msg = `Error: ${err.message}`;
return;
}

// Assuming only one varbind is returned
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;
break;
case '>=':
heartbeat.status = numericValue >= monitor.snmpControlValue ? UP : DOWN;
break;
case '<':
heartbeat.status = numericValue < monitor.snmpControlValue ? UP : DOWN;
break;
case '<=':
heartbeat.status = numericValue <= monitor.snmpControlValue ? UP : DOWN;
break;
case '==':
heartbeat.status = value === monitor.snmpControlValue ? UP : DOWN;
break;
case 'contains':
heartbeat.status = stringValue.includes(monitor.snmpControlValue) ? UP : DOWN;
break;
default:
heartbeat.status = DOWN;
heartbeat.msg = `Invalid condition: ${monitor.snmpCondition}`;
}

session.close();
});
} catch (err) {
heartbeat.status = DOWN;
heartbeat.msg = `Error: ${err.message}`;
}
}

}

module.exports = {
SNMPMonitorType,
};
2 changes: 2 additions & 0 deletions server/uptime-kuma-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class UptimeKumaServer {
UptimeKumaServer.monitorTypeList["tailscale-ping"] = new TailscalePing();
UptimeKumaServer.monitorTypeList["dns"] = new DnsMonitorType();
UptimeKumaServer.monitorTypeList["mqtt"] = new MqttMonitorType();
UptimeKumaServer.monitorTypeList["snmp"] = new SNMPMonitorType();

// Allow all CORS origins (polling) in development
let cors = undefined;
Expand Down Expand Up @@ -516,3 +517,4 @@ const { RealBrowserMonitorType } = require("./monitor-types/real-browser-monitor
const { TailscalePing } = require("./monitor-types/tailscale-ping");
const { DnsMonitorType } = require("./monitor-types/dns");
const { MqttMonitorType } = require("./monitor-types/mqtt");
const { SNMPMonitorType } = require("./monitor-types/snmp");
Binary file added src/pages/.EditMonitor.vue.swp
Binary file not shown.

0 comments on commit d92003e

Please sign in to comment.