2015-03-04 20:55:53 +01:00
|
|
|
|
|
|
|
module.exports = function(RED) {
|
2015-03-26 19:55:03 +01:00
|
|
|
"use strict";
|
2016-04-12 20:18:48 +02:00
|
|
|
var snmp = require("net-snmp");
|
2015-03-04 20:55:53 +01:00
|
|
|
|
2017-05-30 18:15:36 +02:00
|
|
|
var sessions = {};
|
|
|
|
|
|
|
|
function getSession(host, community, version){
|
|
|
|
var sessionKey = host + ":" + community + ":" + version;
|
|
|
|
if (!(sessionKey in sessions)){
|
|
|
|
sessions[sessionKey] = snmp.createSession(host, community, {version: version});
|
|
|
|
}
|
|
|
|
return sessions[sessionKey];
|
|
|
|
}
|
|
|
|
|
2015-03-04 20:55:53 +01:00
|
|
|
function SnmpNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
2017-04-15 13:04:47 +02:00
|
|
|
this.community = n.community;
|
|
|
|
this.host = n.host;
|
2015-03-04 20:55:53 +01:00
|
|
|
this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
|
|
|
|
this.oids = n.oids.replace(/\s/g,"");
|
|
|
|
var node = this;
|
|
|
|
|
|
|
|
this.on("input",function(msg) {
|
2017-04-22 16:53:04 +02:00
|
|
|
var host = node.host || msg.host;
|
|
|
|
var community = node.community || msg.community;
|
2015-03-04 20:55:53 +01:00
|
|
|
var oids = node.oids || msg.oid;
|
|
|
|
if (oids) {
|
2017-05-30 18:15:36 +02:00
|
|
|
getSession(host, community, node.version).get(oids.split(","), function(error, varbinds) {
|
2015-03-04 20:55:53 +01:00
|
|
|
if (error) {
|
2016-04-12 20:18:48 +02:00
|
|
|
node.error(error.toString(),msg);
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-03-04 20:55:53 +01:00
|
|
|
for (var i = 0; i < varbinds.length; i++) {
|
2016-04-12 20:18:48 +02:00
|
|
|
if (snmp.isVarbindError(varbinds[i])) {
|
|
|
|
node.error(snmp.varbindError(varbinds[i]),msg);
|
2015-03-04 20:55:53 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (varbinds[i].type == 4) { varbinds[i].value = varbinds[i].value.toString(); }
|
|
|
|
varbinds[i].tstr = snmp.ObjectType[varbinds[i].type];
|
|
|
|
//node.log(varbinds[i].oid + "|" + varbinds[i].tstr + "|" + varbinds[i].value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msg.oid = oids;
|
|
|
|
msg.payload = varbinds;
|
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.warn("No oid(s) to search for");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("snmp",SnmpNode);
|
|
|
|
|
|
|
|
function SnmpTNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
2017-04-15 13:04:47 +02:00
|
|
|
this.community = n.community;
|
|
|
|
this.host = n.host;
|
2015-03-04 20:55:53 +01:00
|
|
|
this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
|
|
|
|
this.oids = n.oids.replace(/\s/g,"");
|
|
|
|
var node = this;
|
|
|
|
var maxRepetitions = 20;
|
|
|
|
|
2016-04-12 20:18:48 +02:00
|
|
|
function sortInt(a, b) {
|
2015-03-26 19:55:03 +01:00
|
|
|
if (a > b) { return 1; }
|
|
|
|
else if (b > a) { return -1; }
|
|
|
|
else { return 0; }
|
2015-03-04 20:55:53 +01:00
|
|
|
}
|
|
|
|
|
2017-04-15 13:04:47 +02:00
|
|
|
this.on("input",function(msg) {
|
2017-04-22 16:53:04 +02:00
|
|
|
var host = node.host || msg.host;
|
|
|
|
var community = node.community || msg.community;
|
2015-03-04 20:55:53 +01:00
|
|
|
var oids = node.oids || msg.oid;
|
|
|
|
if (oids) {
|
2016-02-11 23:50:30 +01:00
|
|
|
msg.oid = oids;
|
2017-05-30 18:15:36 +02:00
|
|
|
getSession(host, community, node.version).table(oids, maxRepetitions, function(error, table) {
|
2017-04-15 13:04:47 +02:00
|
|
|
if (error) {
|
2017-04-22 16:53:04 +02:00
|
|
|
node.error(error.toString(), msg);
|
2017-04-15 13:04:47 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
var indexes = [];
|
|
|
|
for (var index in table) {
|
|
|
|
if (table.hasOwnProperty(index)) {
|
|
|
|
indexes.push(parseInt(index));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
indexes.sort(sortInt);
|
|
|
|
for (var i = 0; i < indexes.length; i++) {
|
|
|
|
var columns = [];
|
|
|
|
for (var column in table[indexes[i]]) {
|
|
|
|
if (table[indexes[i]].hasOwnProperty(column)) {
|
|
|
|
columns.push(parseInt(column));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
columns.sort(sortInt);
|
|
|
|
// console.log("row index = " + indexes[i]);
|
|
|
|
// for (var j = 0; j < columns.length; j++) {
|
|
|
|
// console.log(" column " + columns[j] + " = " + table[indexes[i]][columns[j]]);
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
msg.payload = table;
|
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
});
|
2015-03-04 20:55:53 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.warn("No oid to search for");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("snmp table",SnmpTNode);
|
2016-04-12 20:18:48 +02:00
|
|
|
|
|
|
|
function SnmpSubtreeNode(n) {
|
2016-04-12 19:39:19 +02:00
|
|
|
RED.nodes.createNode(this,n);
|
2017-04-15 13:04:47 +02:00
|
|
|
this.community = n.community;
|
|
|
|
this.host = n.host;
|
2016-04-12 19:39:19 +02:00
|
|
|
this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
|
|
|
|
this.oids = n.oids.replace(/\s/g,"");
|
|
|
|
var node = this;
|
|
|
|
var maxRepetitions = 20;
|
2016-04-12 20:18:48 +02:00
|
|
|
var response = [];
|
|
|
|
|
|
|
|
function feedCb(varbinds) {
|
|
|
|
for (var i = 0; i < varbinds.length; i++) {
|
|
|
|
if (snmp.isVarbindError(varbinds[i])) {
|
2017-04-22 16:53:04 +02:00
|
|
|
node.error(snmp.varbindError(varbinds[i]), msg);
|
2016-04-12 20:18:48 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-09-01 12:17:18 +02:00
|
|
|
//console.log(varbinds[i].oid + "|" + varbinds[i].value);
|
2017-04-15 13:04:47 +02:00
|
|
|
response.push({oid: varbinds[i].oid, value: varbinds[i].value});
|
2016-04-12 20:18:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-12 19:39:19 +02:00
|
|
|
|
|
|
|
this.on("input",function(msg) {
|
2017-04-22 16:53:04 +02:00
|
|
|
var host = node.host || msg.host;
|
|
|
|
var community = node.community || msg.community;
|
2016-04-12 19:39:19 +02:00
|
|
|
var oids = node.oids || msg.oid;
|
|
|
|
if (oids) {
|
|
|
|
msg.oid = oids;
|
2017-05-30 18:15:36 +02:00
|
|
|
getSession(host, community, node.version).subtree(msg.oid, maxRepetitions, feedCb, function(error) {
|
2017-04-15 13:04:47 +02:00
|
|
|
if (error) {
|
2017-04-22 16:53:04 +02:00
|
|
|
node.error(error.toString(), msg);
|
2017-04-15 13:04:47 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
msg.payload = response;
|
|
|
|
node.send(msg);
|
|
|
|
//Clears response
|
|
|
|
response.length = 0;
|
|
|
|
}
|
|
|
|
});
|
2016-04-12 19:39:19 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.warn("No oid to search for");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("snmp subtree",SnmpSubtreeNode);
|
2016-04-12 20:18:48 +02:00
|
|
|
|
|
|
|
function SnmpWalkerNode(n) {
|
2016-04-12 19:39:19 +02:00
|
|
|
RED.nodes.createNode(this,n);
|
2017-04-15 13:04:47 +02:00
|
|
|
this.community = n.community;
|
|
|
|
this.host = n.host;
|
2016-04-12 19:39:19 +02:00
|
|
|
this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
|
|
|
|
this.oids = n.oids.replace(/\s/g,"");
|
|
|
|
var node = this;
|
|
|
|
var maxRepetitions = 20;
|
2016-04-12 20:18:48 +02:00
|
|
|
var response = [];
|
|
|
|
|
|
|
|
function feedCb(varbinds) {
|
|
|
|
for (var i = 0; i < varbinds.length; i++) {
|
|
|
|
if (snmp.isVarbindError(varbinds[i])) {
|
2017-04-22 16:53:04 +02:00
|
|
|
node.error(snmp.varbindError(varbinds[i]), msg);
|
2016-04-12 20:18:48 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-09-01 12:17:18 +02:00
|
|
|
//console.log(varbinds[i].oid + "|" + varbinds[i].value);
|
2017-04-15 13:04:47 +02:00
|
|
|
response.push({oid: varbinds[i].oid, value: varbinds[i].value});
|
2016-04-12 20:18:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-12 19:39:19 +02:00
|
|
|
|
|
|
|
this.on("input",function(msg) {
|
2017-04-15 13:04:47 +02:00
|
|
|
node.msg = msg;
|
2016-04-12 19:39:19 +02:00
|
|
|
var oids = node.oids || msg.oid;
|
2017-04-22 16:53:04 +02:00
|
|
|
var host = node.host || msg.host;
|
|
|
|
var community = node.community || msg.community;
|
2016-04-12 19:39:19 +02:00
|
|
|
if (oids) {
|
|
|
|
msg.oid = oids;
|
2017-05-30 18:15:36 +02:00
|
|
|
getSession(host, community, node.version).walk(msg.oid, maxRepetitions, feedCb, function(error) {
|
2017-04-15 13:04:47 +02:00
|
|
|
if (error) {
|
2017-04-22 16:53:04 +02:00
|
|
|
node.error(error.toString(), msg);
|
2017-04-15 13:04:47 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
msg.payload = response;
|
|
|
|
node.send(msg);
|
|
|
|
//Clears response
|
|
|
|
response.length = 0;
|
|
|
|
}
|
|
|
|
});
|
2016-04-12 19:39:19 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.warn("No oid to search for");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("snmp walker",SnmpWalkerNode);
|
2016-04-12 20:18:48 +02:00
|
|
|
};
|