1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00
node-red-nodes/io/snmp/snmp.js

202 lines
7.1 KiB
JavaScript
Raw Normal View History

2015-03-04 20:55:53 +01:00
module.exports = function(RED) {
"use strict";
var snmp = require("net-snmp");
2015-03-04 20:55:53 +01:00
function SnmpNode(n) {
RED.nodes.createNode(this,n);
this.community = n.community || "public";
this.host = n.host || "127.0.0.1";
this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
this.oids = n.oids.replace(/\s/g,"");
this.session = snmp.createSession(this.host, this.community, {version: this.version});
2015-03-04 20:55:53 +01:00
var node = this;
this.on("input",function(msg) {
var oids = node.oids || msg.oid;
if (oids) {
node.session.get(oids.split(","), function(error, varbinds) {
if (error) {
node.error(error.toString(),msg);
2015-03-04 20:55:53 +01:00
} else {
for (var i = 0; i < varbinds.length; i++) {
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);
this.community = n.community || "public";
this.host = n.host || "127.0.0.1";
this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
this.oids = n.oids.replace(/\s/g,"");
this.session = snmp.createSession(this.host, this.community, {version: this.version});
2015-03-04 20:55:53 +01:00
var node = this;
var msg;
2015-03-04 20:55:53 +01:00
var maxRepetitions = 20;
function sortInt(a, b) {
if (a > b) { return 1; }
else if (b > a) { return -1; }
else { return 0; }
2015-03-04 20:55:53 +01:00
}
function responseCb(error, table) {
2015-03-04 20:55:53 +01:00
if (error) {
le.error(error.toString());
2015-03-04 20:55:53 +01:00
} else {
var indexes = [];
for (var index in table) {
if (table.hasOwnProperty(index)) {
indexes.push(parseInt(index));
}
}
indexes.sort(sortInt);
2015-03-04 20:55:53 +01:00
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));
}
}
2015-03-04 20:55:53 +01:00
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]]);
// }
2015-03-04 20:55:53 +01:00
}
msg.payload = table;
node.send(msg);
}
}
this.on("input",function(m) {
msg = m;
2015-03-04 20:55:53 +01:00
var oids = node.oids || msg.oid;
if (oids) {
msg.oid = oids;
2015-03-04 20:55:53 +01:00
node.session.table(oids, maxRepetitions, responseCb);
}
else {
node.warn("No oid to search for");
}
});
}
RED.nodes.registerType("snmp table",SnmpTNode);
function SnmpSubtreeNode(n) {
RED.nodes.createNode(this,n);
this.community = n.community || "public";
this.host = n.host || "127.0.0.1";
this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
this.oids = n.oids.replace(/\s/g,"");
this.session = snmp.createSession(this.host, this.community, {version: this.version});
var node = this;
var maxRepetitions = 20;
var response = [];
function doneCb(error) {
if (error) {
console.error(error.toString());
}
else {
var msg = {};
msg.payload = response;
node.send(msg);
response.clear();
}
}
function feedCb(varbinds) {
for (var i = 0; i < varbinds.length; i++) {
if (snmp.isVarbindError(varbinds[i])) {
node.error(snmp.varbindError(varbinds[i]));
}
else {
//console.log(varbinds[i].oid + "|" + varbinds[i].value);
response.add({oid: varbinds[i].oid, value: varbinds[i].value});
}
}
}
this.on("input",function(msg) {
var oids = node.oids || msg.oid;
if (oids) {
msg.oid = oids;
node.session.subtree(msg.oid, maxRepetitions, feedCb, doneCb);
//node.session.subtree(oids, maxRepetitions, responseCb);
}
else {
node.warn("No oid to search for");
}
});
}
RED.nodes.registerType("snmp subtree",SnmpSubtreeNode);
function SnmpWalkerNode(n) {
RED.nodes.createNode(this,n);
this.community = n.community || "public";
this.host = n.host || "127.0.0.1";
this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
this.oids = n.oids.replace(/\s/g,"");
this.session = snmp.createSession(this.host, this.community, {version: this.version});
var node = this;
var maxRepetitions = 20;
var response = [];
function doneCb(error) {
if (error) {
node.error(error.toString());
}
else {
var msg = {};
msg.payload = response;
node.send(msg);
response.clear();
}
}
function feedCb(varbinds) {
for (var i = 0; i < varbinds.length; i++) {
if (snmp.isVarbindError(varbinds[i])) {
node.error(snmp.varbindError(varbinds[i]));
}
else {
//console.log(varbinds[i].oid + "|" + varbinds[i].value);
response.add({oid: varbinds[i].oid, value: varbinds[i].value});
}
}
}
this.on("input",function(msg) {
var oids = node.oids || msg.oid;
if (oids) {
msg.oid = oids;
node.session.walk(msg.oid, maxRepetitions, feedCb, doneCb);
}
else {
node.warn("No oid to search for");
}
});
}
RED.nodes.registerType("snmp walker",SnmpWalkerNode);
};