From c8a1bc0df36558b3ec8af8058554c669d5e99ef9 Mon Sep 17 00:00:00 2001 From: Bryan Malyn Date: Thu, 6 Apr 2017 14:49:13 -0500 Subject: [PATCH 1/9] 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 --- io/snmp/snmp.html | 8 +- io/snmp/snmp.js | 188 +++++++++++++++++++++++++++------------------- 2 files changed, 116 insertions(+), 80 deletions(-) diff --git a/io/snmp/snmp.html b/io/snmp/snmp.html index 5eaefd617..c0c455748 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 c243496cb..518cb1530 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}); } } } @@ -142,40 +147,48 @@ module.exports = function(RED) { 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); + msg.oid = 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.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); }; From 8ee1da5bc47f5a044288b58db24d11ad5c854a9b Mon Sep 17 00:00:00 2001 From: Bryan Malyn Date: Thu, 6 Apr 2017 14:56:06 -0500 Subject: [PATCH 2/9] Replaced tabs with 4 spaces. Signed-off-by: Bryan Malyn --- io/snmp/snmp.js | 178 ++++++++++++++++++++++++------------------------ 1 file changed, 89 insertions(+), 89 deletions(-) diff --git a/io/snmp/snmp.js b/io/snmp/snmp.js index 518cb1530..fbda5f477 100644 --- a/io/snmp/snmp.js +++ b/io/snmp/snmp.js @@ -14,12 +14,12 @@ module.exports = function(RED) { 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}); + 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); @@ -47,10 +47,10 @@ module.exports = function(RED) { }); this.on("close", function(){ - if (node.session){ - node.session.close(); - } - }); + if (node.session){ + node.session.close(); + } + }); } RED.nodes.registerType("snmp",SnmpNode); @@ -74,40 +74,40 @@ module.exports = function(RED) { if (oids) { msg.oid = 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.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); - } - }); + 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"); @@ -115,10 +115,10 @@ module.exports = function(RED) { }); this.on("close", function(){ - if (node.session){ - node.session.close(); - } - }); + if (node.session){ + node.session.close(); + } + }); } RED.nodes.registerType("snmp table",SnmpTNode); @@ -147,24 +147,24 @@ module.exports = function(RED) { this.on("input",function(msg) { var oids = node.oids || msg.oid; if (oids) { - msg.oid = 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}); + msg.oid = 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.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; - } - }); + 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"); @@ -172,10 +172,10 @@ module.exports = function(RED) { }); this.on("close", function(){ - if (node.session){ - node.session.close(); - } - }); + if (node.session){ + node.session.close(); + } + }); } RED.nodes.registerType("snmp subtree",SnmpSubtreeNode); @@ -202,38 +202,38 @@ module.exports = function(RED) { } this.on("input",function(msg) { - node.msg = msg; + node.msg = msg; var oids = node.oids || msg.oid; if (oids) { msg.oid = 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.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; - } - }); + 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(); - } - }); + this.on("close", function(){ + if (node.session){ + node.session.close(); + } + }); } RED.nodes.registerType("snmp walker",SnmpWalkerNode); }; From a6abe922008ca3e4262df1b8048eb6ad6ca3f24c Mon Sep 17 00:00:00 2001 From: Bryan Malyn Date: Mon, 17 Apr 2017 11:19:24 -0500 Subject: [PATCH 3/9] fix extra spacing Signed-off-by: Bryan Malyn --- io/snmp/snmp.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/io/snmp/snmp.js b/io/snmp/snmp.js index 75022334d..c3e90bf69 100644 --- a/io/snmp/snmp.js +++ b/io/snmp/snmp.js @@ -45,7 +45,7 @@ module.exports = function(RED) { node.warn("No oid(s) to search for"); } }); - + this.on("close", function() { if (node.session) { node.session.close(); From 3e8aff83895de5298b6cbd305e6bd55d053dc93c Mon Sep 17 00:00:00 2001 From: Bryan Malyn Date: Mon, 17 Apr 2017 16:27:33 -0500 Subject: [PATCH 4/9] Standardise node.error to include msg so that errors can caught Signed-off-by: Bryan Malyn --- io/snmp/snmp.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/io/snmp/snmp.js b/io/snmp/snmp.js index c3e90bf69..0aea75559 100644 --- a/io/snmp/snmp.js +++ b/io/snmp/snmp.js @@ -81,7 +81,7 @@ module.exports = function(RED) { node.session = snmp.createSession(host, community, {version: node.version}); node.session.table(oids, maxRepetitions, function(error, table) { if (error) { - node.error(error.toString()); + node.error(error.toString(), msg); } else { var indexes = []; @@ -135,7 +135,7 @@ module.exports = function(RED) { function feedCb(varbinds) { for (var i = 0; i < varbinds.length; i++) { if (snmp.isVarbindError(varbinds[i])) { - node.error(snmp.varbindError(varbinds[i])); + node.error(snmp.varbindError(varbinds[i]), msg); } else { //console.log(varbinds[i].oid + "|" + varbinds[i].value); @@ -156,7 +156,7 @@ module.exports = function(RED) { node.session = snmp.createSession(host, community, {version: node.version}); node.session.subtree(msg.oid, maxRepetitions, feedCb, function(error) { if (error) { - node.error(error.toString()); + node.error(error.toString(), msg); } else { msg.payload = response; @@ -192,7 +192,7 @@ module.exports = function(RED) { function feedCb(varbinds) { for (var i = 0; i < varbinds.length; i++) { if (snmp.isVarbindError(varbinds[i])) { - node.error(snmp.varbindError(varbinds[i])); + node.error(snmp.varbindError(varbinds[i]), msg); } else { //console.log(varbinds[i].oid + "|" + varbinds[i].value); @@ -214,7 +214,7 @@ module.exports = function(RED) { node.session = snmp.createSession(host, community, {version: node.version}); node.session.walk(msg.oid, maxRepetitions, feedCb, function(error) { if (error) { - node.error(error.toString()); + node.error(error.toString(), msg); } else { msg.payload = response; From cae6f1474c00a77d4d875235936e3b6271fb2206 Mon Sep 17 00:00:00 2001 From: Bryan Malyn Date: Fri, 21 Apr 2017 15:32:21 -0500 Subject: [PATCH 5/9] Update documentation for snmp nodes fixed documentation as noted, removed conflict and iff from documention remove node.warns if host or community are set in both the node config and msg Signed-off-by: Bryan Malyn --- io/snmp/README.md | 38 +++++++++++++++++++------------------- io/snmp/snmp.html | 16 ++++++++-------- io/snmp/snmp.js | 28 ++++++++-------------------- 3 files changed, 35 insertions(+), 47 deletions(-) diff --git a/io/snmp/README.md b/io/snmp/README.md index 473db6159..b228c81db 100644 --- a/io/snmp/README.md +++ b/io/snmp/README.md @@ -18,15 +18,15 @@ Usage SNMP oids fetcher. Can fetch a single or comma separated list of oids. Triggered by any input. -`msg.host` may contain the host, iff host not set in the node configuration. +`msg.host` may contain the host. -`msg.community` may contain the community, iff community not set in the node configuration. +`msg.community` may contain the community. `msg.oid` may contain a comma separated list of oids to search for. (no spaces) -The host configured in the edit config will conflict with `msg.host`. Leave blank if you want to use `msg.host` to provide input. +The host configured in the edit config will override `msg.host`. Leave blank if you want to use `msg.host` to provide input. -The community configured in the edit config will conflict with `msg.community`. Leave blank if you want to use `msg.community` to provide input. +The community configured in the edit config will override `msg.community`. Leave blank if you want to use `msg.community` to provide input. The oids configured in the edit config will override `msg.oid`. Leave blank if you want to use `msg.oid` to provide input. @@ -38,17 +38,17 @@ Values depends on the oids being requested. Simple SNMP table oid fetcher. Triggered by any input. -`msg.host` may contain the host, iff host not set in the node configuration. +`msg.host` may contain the host. -`msg.community` may contain the community, iff community not set in the node configuration. +`msg.community` may contain the community. `msg.oid` may contain the oid of a single table to search for. -The host configured in the edit config will conflict with `msg.host`. Leave blank if you want to use `msg.host` to provide input. +The host configured in the edit config will override `msg.host`. Leave blank if you want to use `msg.host` to provide input. -The community configured in the edit config will conflict with `msg.community`. Leave blank if you want to use `msg.community` to provide input. +The community configured in the edit config will override `msg.community`. Leave blank if you want to use `msg.community` to provide input. -The oid confgured in the edit config will override `msg.oid`. Leave blank if you +The oid configured in the edit config will override `msg.oid`. Leave blank if you want to use `msg.oid` to provide input. Outputs `msg.payload` containing the table of objects, and the requested `msg.oid`. @@ -58,17 +58,17 @@ Values depends on the oids being requested. Simple SNMP oid subtree fetcher. Triggered by any input. -`msg.host` may contain the host, iff host not set in the node configuration. +`msg.host` may contain the host. -`msg.community` may contain the community, iff community not set in the node configuration. +`msg.community` may contain the community. `msg.oid` may contain the oid of a single table to search for. -The host configured in the edit config will conflict with `msg.host`. Leave blank if you want to use `msg.host` to provide input. +The host configured in the edit config will override `msg.host`. Leave blank if you want to use `msg.host` to provide input. -The community configured in the edit config will conflict with `msg.community`. Leave blank if you want to use `msg.community` to provide input. +The community configured in the edit config will override `msg.community`. Leave blank if you want to use `msg.community` to provide input. -The oid confgured in the edit config will override `msg.oid`. Leave blank if you +The oid configured in the edit config will override `msg.oid`. Leave blank if you want to use `msg.oid` to provide input. Outputs `msg.payload` containing the table of objects, and the requested `msg.oid`. @@ -78,17 +78,17 @@ Values depends on the oids being requested. Simple SNMP oid walker fetcher. Triggered by any input. -`msg.host` may contain the host, iff host not set in the node configuration. +`msg.host` may contain the host. -`msg.community` may contain the community, iff community not set in the node configuration. +`msg.community` may contain the community. `msg.oid` may contain the oid of a single table to search for. -The host configured in the edit config will conflict with `msg.host`. Leave blank if you want to use `msg.host` to provide input. +The host configured in the edit config will override `msg.host`. Leave blank if you want to use `msg.host` to provide input. -The community configured in the edit config will conflict with `msg.community`. Leave blank if you want to use `msg.community` to provide input. +The community configured in the edit config will override `msg.community`. Leave blank if you want to use `msg.community` to provide input. -The oid confgured in the edit config will override `msg.oid`. Leave blank if you +The oid configured in the edit config will override `msg.oid`. Leave blank if you want to use `msg.oid` to provide input. Outputs `msg.payload` containing the table of objects, and the requested `msg.oid`. diff --git a/io/snmp/snmp.html b/io/snmp/snmp.html index 58dcc4996..1621e7241 100644 --- a/io/snmp/snmp.html +++ b/io/snmp/snmp.html @@ -28,8 +28,8 @@ @@ -87,8 +87,8 @@ @@ -145,8 +145,8 @@ @@ -205,8 +205,8 @@ diff --git a/io/snmp/snmp.js b/io/snmp/snmp.js index 0aea75559..a1159ac08 100644 --- a/io/snmp/snmp.js +++ b/io/snmp/snmp.js @@ -12,13 +12,10 @@ module.exports = function(RED) { var node = this; this.on("input",function(msg) { + var host = node.host || msg.host; + var community = node.community || msg.community; 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) { @@ -70,14 +67,11 @@ module.exports = function(RED) { } this.on("input",function(msg) { + var host = node.host || msg.host; + var community = node.community || msg.community; var oids = node.oids || msg.oid; if (oids) { msg.oid = 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.table(oids, maxRepetitions, function(error, table) { if (error) { @@ -145,14 +139,11 @@ module.exports = function(RED) { } this.on("input",function(msg) { + var host = node.host || msg.host; + var community = node.community || msg.community; var oids = node.oids || msg.oid; if (oids) { msg.oid = 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.subtree(msg.oid, maxRepetitions, feedCb, function(error) { if (error) { @@ -204,13 +195,10 @@ module.exports = function(RED) { this.on("input",function(msg) { node.msg = msg; var oids = node.oids || msg.oid; + var host = node.host || msg.host; + var community = node.community || msg.community; if (oids) { msg.oid = 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.walk(msg.oid, maxRepetitions, feedCb, function(error) { if (error) { From b3801cb5c5a5f5d5f2c7ebc19d724aef940197b5 Mon Sep 17 00:00:00 2001 From: Bryan Malyn Date: Thu, 18 May 2017 13:53:18 -0500 Subject: [PATCH 6/9] FIX: Close net-snmp sessions Signed-off-by: Bryan Malyn --- io/snmp/snmp.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/io/snmp/snmp.js b/io/snmp/snmp.js index a1159ac08..7525c6d26 100644 --- a/io/snmp/snmp.js +++ b/io/snmp/snmp.js @@ -36,6 +36,10 @@ module.exports = function(RED) { msg.payload = varbinds; node.send(msg); } + if (node.session) { + node.session.close(); + delete node.session; + } }); } else { @@ -46,6 +50,7 @@ module.exports = function(RED) { this.on("close", function() { if (node.session) { node.session.close(); + delete node.session; } }); } @@ -101,6 +106,10 @@ module.exports = function(RED) { msg.payload = table; node.send(msg); } + if (node.session) { + node.session.close(); + delete node.session; + } }); } else { @@ -111,6 +120,7 @@ module.exports = function(RED) { this.on("close", function() { if (node.session) { node.session.close(); + delete node.session; } }); } @@ -155,6 +165,10 @@ module.exports = function(RED) { //Clears response response.length = 0; } + if (node.session) { + node.session.close(); + delete node.session; + } }); } else { @@ -165,6 +179,7 @@ module.exports = function(RED) { this.on("close", function() { if (node.session) { node.session.close(); + delete node.session; } }); } @@ -210,6 +225,10 @@ module.exports = function(RED) { //Clears response response.length = 0; } + if (node.session) { + node.session.close(); + delete node.session; + } }); } else { @@ -220,6 +239,7 @@ module.exports = function(RED) { this.on("close", function() { if (node.session) { node.session.close(); + delete node.session; } }); } From b49b5fb0507616b2884ca0eb798fe40ae01b02a5 Mon Sep 17 00:00:00 2001 From: Bryan Malyn Date: Thu, 18 May 2017 17:37:16 -0500 Subject: [PATCH 7/9] Fix 2: The previous push did not cleanly address the problem. This commit uses a singleton aproach to create socket. It still needs to be tested to see if there is any issue to the never-close, but reuse socket model. My only concern is if a socket dies, do we need to do something to reestablish it? Signed-off-by: Bryan Malyn --- io/snmp/snmp.js | 66 +++++++++++-------------------------------------- 1 file changed, 14 insertions(+), 52 deletions(-) diff --git a/io/snmp/snmp.js b/io/snmp/snmp.js index 7525c6d26..3bfc25fa8 100644 --- a/io/snmp/snmp.js +++ b/io/snmp/snmp.js @@ -3,6 +3,16 @@ module.exports = function(RED) { "use strict"; var snmp = require("net-snmp"); + 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]; + } + function SnmpNode(n) { RED.nodes.createNode(this,n); this.community = n.community; @@ -16,8 +26,7 @@ module.exports = function(RED) { var community = node.community || msg.community; var oids = node.oids || msg.oid; if (oids) { - node.session = snmp.createSession(host, community, {version: node.version}); - node.session.get(oids.split(","), function(error, varbinds) { + getSession(host, community, node.version).get(oids.split(","), function(error, varbinds) { if (error) { node.error(error.toString(),msg); } @@ -36,23 +45,12 @@ module.exports = function(RED) { msg.payload = varbinds; node.send(msg); } - if (node.session) { - node.session.close(); - delete node.session; - } }); } else { node.warn("No oid(s) to search for"); } }); - - this.on("close", function() { - if (node.session) { - node.session.close(); - delete node.session; - } - }); } RED.nodes.registerType("snmp",SnmpNode); @@ -77,8 +75,7 @@ module.exports = function(RED) { var oids = node.oids || msg.oid; if (oids) { msg.oid = oids; - node.session = snmp.createSession(host, community, {version: node.version}); - node.session.table(oids, maxRepetitions, function(error, table) { + getSession(host, community, node.version).table(oids, maxRepetitions, function(error, table) { if (error) { node.error(error.toString(), msg); } @@ -106,23 +103,12 @@ module.exports = function(RED) { msg.payload = table; node.send(msg); } - if (node.session) { - node.session.close(); - delete node.session; - } }); } else { node.warn("No oid to search for"); } }); - - this.on("close", function() { - if (node.session) { - node.session.close(); - delete node.session; - } - }); } RED.nodes.registerType("snmp table",SnmpTNode); @@ -154,8 +140,7 @@ module.exports = function(RED) { var oids = node.oids || msg.oid; if (oids) { msg.oid = oids; - node.session = snmp.createSession(host, community, {version: node.version}); - node.session.subtree(msg.oid, maxRepetitions, feedCb, function(error) { + getSession(host, community, node.version).subtree(msg.oid, maxRepetitions, feedCb, function(error) { if (error) { node.error(error.toString(), msg); } @@ -165,23 +150,12 @@ module.exports = function(RED) { //Clears response response.length = 0; } - if (node.session) { - node.session.close(); - delete node.session; - } }); } else { node.warn("No oid to search for"); } }); - - this.on("close", function() { - if (node.session) { - node.session.close(); - delete node.session; - } - }); } RED.nodes.registerType("snmp subtree",SnmpSubtreeNode); @@ -214,8 +188,7 @@ module.exports = function(RED) { var community = node.community || msg.community; if (oids) { msg.oid = oids; - node.session = snmp.createSession(host, community, {version: node.version}); - node.session.walk(msg.oid, maxRepetitions, feedCb, function(error) { + getSession(host, community, node.version).walk(msg.oid, maxRepetitions, feedCb, function(error) { if (error) { node.error(error.toString(), msg); } @@ -225,23 +198,12 @@ module.exports = function(RED) { //Clears response response.length = 0; } - if (node.session) { - node.session.close(); - delete node.session; - } }); } else { node.warn("No oid to search for"); } }); - - this.on("close", function() { - if (node.session) { - node.session.close(); - delete node.session; - } - }); } RED.nodes.registerType("snmp walker",SnmpWalkerNode); }; From b1da7fcd7b3387e02aa451d6c67d30800fd3e139 Mon Sep 17 00:00:00 2001 From: Bryan Malyn Date: Wed, 31 May 2017 10:30:27 -0500 Subject: [PATCH 8/9] Remove default value and required from the community attribute in the snmp node. This was preventing a user from being able to set the community value within the msg. Signed-off-by: Bryan Malyn --- io/snmp/snmp.html | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/io/snmp/snmp.html b/io/snmp/snmp.html index 1621e7241..e8bd25e85 100644 --- a/io/snmp/snmp.html +++ b/io/snmp/snmp.html @@ -40,7 +40,7 @@ color:"YellowGreen", defaults: { host: {value:""}, - community: {value:"public",required:true}, + community: {value:""}, version: {value:"1",required:true}, oids: {value:""}, name: {value:""} @@ -99,7 +99,7 @@ color:"YellowGreen", defaults: { host: {value:""}, - community: {value:"public",required:true}, + community: {value:""}, version: {value:"1",required:true}, oids: {value:""}, name: {value:""} @@ -157,7 +157,7 @@ color:"YellowGreen", defaults: { host: {value:""}, - community: {value:"public",required:true}, + community: {value:""}, version: {value:"1",required:true}, oids: {value:""}, name: {value:""} @@ -217,7 +217,7 @@ color:"YellowGreen", defaults: { host: {value:""}, - community: {value:"public",required:true}, + community: {value:""}, version: {value:"1",required:true}, oids: {value:""}, name: {value:""} From ea9949a77b9cc05330cdcfca5d01a49df8992102 Mon Sep 17 00:00:00 2001 From: Bryan Malyn Date: Wed, 31 May 2017 14:02:24 -0500 Subject: [PATCH 9/9] Set default value for host and community in nodes, but don't require them to be set so that they can be set my msg.host and msg.community respectivly. Signed-off-by: Bryan Malyn --- io/snmp/snmp.html | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/io/snmp/snmp.html b/io/snmp/snmp.html index e8bd25e85..7f3bce47c 100644 --- a/io/snmp/snmp.html +++ b/io/snmp/snmp.html @@ -39,8 +39,8 @@ category: 'network-input', color:"YellowGreen", defaults: { - host: {value:""}, - community: {value:""}, + host: {value:"127.0.0.1"}, + community: {value:"public"}, version: {value:"1",required:true}, oids: {value:""}, name: {value:""} @@ -98,8 +98,8 @@ category: 'network-input', color:"YellowGreen", defaults: { - host: {value:""}, - community: {value:""}, + host: {value:"127.0.0.1"}, + community: {value:"public"}, version: {value:"1",required:true}, oids: {value:""}, name: {value:""} @@ -156,8 +156,8 @@ category: 'network-input', color:"YellowGreen", defaults: { - host: {value:""}, - community: {value:""}, + host: {value:"127.0.0.1"}, + community: {value:"public"}, version: {value:"1",required:true}, oids: {value:""}, name: {value:""} @@ -216,8 +216,8 @@ category: 'network-input', color:"YellowGreen", defaults: { - host: {value:""}, - community: {value:""}, + host: {value:"127.0.0.1"}, + community: {value:"public"}, version: {value:"1",required:true}, oids: {value:""}, name: {value:""}