From 0f7e8385b89eaccb7153573b76f1bcc20bf7bad9 Mon Sep 17 00:00:00 2001 From: bimalyn-IBM Date: Sat, 15 Apr 2017 06:04:47 -0500 Subject: [PATCH] Allow msg to set parameters for snmp nodes. (#299) * I've modified the snmp libraries so that the server and the community can be defined by the msg. Error will be thrown if you try to override what was defined in the node. Verified that the contents of msg is no longer clobbered. Signed-off-by: Bryan Malyn * Replaced tabs with 4 spaces. Signed-off-by: Bryan Malyn --- io/snmp/snmp.html | 8 +- io/snmp/snmp.js | 186 +++++++++++++++++++++++++++------------------- 2 files changed, 115 insertions(+), 79 deletions(-) diff --git a/io/snmp/snmp.html b/io/snmp/snmp.html index 5eaefd61..c0c45574 100644 --- a/io/snmp/snmp.html +++ b/io/snmp/snmp.html @@ -37,7 +37,7 @@ category: 'network-input', color:"YellowGreen", defaults: { - host: {value:"",required:true}, + host: {value:""}, community: {value:"public",required:true}, version: {value:"1",required:true}, oids: {value:""}, @@ -94,7 +94,7 @@ category: 'network-input', color:"YellowGreen", defaults: { - host: {value:"",required:true}, + host: {value:""}, community: {value:"public",required:true}, version: {value:"1",required:true}, oids: {value:""}, @@ -150,7 +150,7 @@ category: 'network-input', color:"YellowGreen", defaults: { - host: {value:"",required:true}, + host: {value:""}, community: {value:"public",required:true}, version: {value:"1",required:true}, oids: {value:""}, @@ -208,7 +208,7 @@ category: 'network-input', color:"YellowGreen", defaults: { - host: {value:"",required:true}, + host: {value:""}, community: {value:"public",required:true}, version: {value:"1",required:true}, oids: {value:""}, diff --git a/io/snmp/snmp.js b/io/snmp/snmp.js index c243496c..fbda5f47 100644 --- a/io/snmp/snmp.js +++ b/io/snmp/snmp.js @@ -5,16 +5,21 @@ module.exports = function(RED) { function SnmpNode(n) { RED.nodes.createNode(this,n); - this.community = n.community || "public"; - this.host = n.host || "127.0.0.1"; + this.community = n.community; + this.host = n.host; 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; this.on("input",function(msg) { var oids = node.oids || msg.oid; if (oids) { + if (msg.host && node.host || msg.community && node.community) { + node.warn(RED._("common.errors.nooverride")); + } + var host = node.host || msg.host; + var community = node.community || msg.community; + node.session = snmp.createSession(host, community, {version: node.version}); node.session.get(oids.split(","), function(error, varbinds) { if (error) { node.error(error.toString(),msg); @@ -40,18 +45,22 @@ module.exports = function(RED) { node.warn("No oid(s) to search for"); } }); + + this.on("close", function(){ + if (node.session){ + node.session.close(); + } + }); } 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.community = n.community; + this.host = n.host; 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 msg; var maxRepetitions = 20; function sortInt(a, b) { @@ -60,73 +69,69 @@ module.exports = function(RED) { else { return 0; } } - function responseCb(error, table) { - if (error) { - console.error(error.toString()); - } - 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); - } - } - - this.on("input",function(m) { - msg = m; + this.on("input",function(msg) { var oids = node.oids || msg.oid; if (oids) { msg.oid = oids; - node.session.table(oids, maxRepetitions, responseCb); + if (msg.host && node.host || msg.community && node.community) { + node.warn(RED._("common.errors.nooverride")); + } + var host = node.host || msg.host; + var community = node.community || msg.community; + node.session = snmp.createSession(host, community, {version: node.version}); + node.session.table(oids, maxRepetitions, function(error, table) { + if (error) { + node.error(error.toString()); + } + 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); + } + }); } else { node.warn("No oid to search for"); } }); + + this.on("close", function(){ + if (node.session){ + node.session.close(); + } + }); } 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.community = n.community; + this.host = n.host; 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])) { @@ -134,7 +139,7 @@ module.exports = function(RED) { } else { //console.log(varbinds[i].oid + "|" + varbinds[i].value); - response.add({oid: varbinds[i].oid, value: varbinds[i].value}); + response.push({oid: varbinds[i].oid, value: varbinds[i].value}); } } } @@ -143,39 +148,47 @@ module.exports = function(RED) { 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); + if (msg.host && node.host || msg.community && node.community) { + node.warn(RED._("common.errors.nooverride")); + } + var host = node.host || msg.host; + var community = node.community || msg.community; + node.session = snmp.createSession(host, community, {version: node.version}); + node.session.subtree(msg.oid, maxRepetitions, feedCb, function(error) { + if (error) { + node.error(error.toString()); + } + else { + msg.payload = response; + node.send(msg); + //Clears response + response.length = 0; + } + }); } else { node.warn("No oid to search for"); } }); + + this.on("close", function(){ + if (node.session){ + node.session.close(); + } + }); } 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.community = n.community; + this.host = n.host; 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])) { @@ -183,21 +196,44 @@ module.exports = function(RED) { } else { //console.log(varbinds[i].oid + "|" + varbinds[i].value); - response.add({oid: varbinds[i].oid, value: varbinds[i].value}); + response.push({oid: varbinds[i].oid, value: varbinds[i].value}); } } } this.on("input",function(msg) { + node.msg = msg; var oids = node.oids || msg.oid; if (oids) { msg.oid = oids; - node.session.walk(msg.oid, maxRepetitions, feedCb, doneCb); + if (msg.host && node.host || msg.community && node.community) { + node.warn(RED._("common.errors.nooverride")); + } + var host = node.host || msg.host; + var community = node.community || msg.community; + node.session = snmp.createSession(host, community, {version: node.version}); + node.session.walk(msg.oid, maxRepetitions, feedCb, function(error) { + if (error) { + node.error(error.toString()); + } + else { + msg.payload = response; + node.send(msg); + //Clears response + response.length = 0; + } + }); } else { node.warn("No oid to search for"); } }); + + this.on("close", function(){ + if (node.session){ + node.session.close(); + } + }); } RED.nodes.registerType("snmp walker",SnmpWalkerNode); };