From b3fd755527580fd16090188e1859df9faf13d36f Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Sun, 28 Jan 2018 17:50:41 +0000 Subject: [PATCH] Add timeout option to snap nodes to address #397 --- io/snmp/package.json | 2 +- io/snmp/snmp.html | 18 ++++++++++++++++-- io/snmp/snmp.js | 24 ++++++++++++++---------- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/io/snmp/package.json b/io/snmp/package.json index 047cff9b..d561c581 100644 --- a/io/snmp/package.json +++ b/io/snmp/package.json @@ -1,6 +1,6 @@ { "name" : "node-red-node-snmp", - "version" : "0.0.15", + "version" : "0.0.16", "description" : "A Node-RED node that looks for SNMP oids.", "dependencies" : { "net-snmp" : "^1.1.19" diff --git a/io/snmp/snmp.html b/io/snmp/snmp.html index 36664565..2ada8dd4 100644 --- a/io/snmp/snmp.html +++ b/io/snmp/snmp.html @@ -9,10 +9,12 @@
- + Timeout +  S
@@ -42,6 +44,7 @@ community: { value: "public" }, version: { value: "1", required: true }, oids: { value: "" }, + timeout: { value: 5 }, name: { value: "" } }, inputs: 1, @@ -72,6 +75,8 @@ + Timeout +  S
@@ -140,6 +145,8 @@ + Timeout +  S
@@ -169,6 +176,7 @@ community: { value: "public" }, version: { value: "1", required: true }, oids: { value: "" }, + timeout: { value: 5 }, name: { value: "" } }, inputs: 1, @@ -199,6 +207,8 @@ + Timeout +  S
@@ -228,6 +238,7 @@ community: { value: "public" }, version: { value: "1", required: true }, oids: { value: "" }, + timeout: { value: 5 }, name: { value: "" } }, inputs: 1, @@ -260,6 +271,8 @@ + Timeout +  S
@@ -289,6 +302,7 @@ community: { value: "public" }, version: { value: "1", required: true }, oids: { value: "" }, + timeout: { value: 5 }, name: { value: "" } }, inputs: 1, @@ -302,4 +316,4 @@ } }); - \ No newline at end of file + diff --git a/io/snmp/snmp.js b/io/snmp/snmp.js index 008d5995..9207217d 100644 --- a/io/snmp/snmp.js +++ b/io/snmp/snmp.js @@ -5,10 +5,10 @@ module.exports = function (RED) { var sessions = {}; - function getSession(host, community, version) { + function getSession(host, community, version, timeout) { var sessionKey = host + ":" + community + ":" + version; if (!(sessionKey in sessions)) { - sessions[sessionKey] = snmp.createSession(host, community, { version: version }); + sessions[sessionKey] = snmp.createSession(host, community, { version:version, timeout:(timeout || 5000) }); } return sessions[sessionKey]; } @@ -19,6 +19,7 @@ module.exports = function (RED) { this.host = n.host; this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1; this.oids = n.oids.replace(/\s/g, ""); + this.timeout = Number(n.timeout || 5) * 1000; var node = this; this.on("input", function (msg) { @@ -26,7 +27,7 @@ module.exports = function (RED) { var community = node.community || msg.community; var oids = node.oids || msg.oid; if (oids) { - getSession(host, community, node.version).get(oids.split(","), function (error, varbinds) { + getSession(host, community, node.version, node.timeout).get(oids.split(","), function (error, varbinds) { if (error) { node.error(error.toString(), msg); } @@ -54,13 +55,13 @@ module.exports = function (RED) { } RED.nodes.registerType("snmp", SnmpNode); - function SnmpSNode(n) { RED.nodes.createNode(this, n); this.community = n.community; this.host = n.host; this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1; this.varbinds = n.varbinds; + this.timeout = Number(n.timeout || 5) * 1000; var node = this; this.on("input", function (msg) { var host = node.host || msg.host; @@ -70,7 +71,7 @@ module.exports = function (RED) { for (var i = 0; i < varbinds.length; i++) { varbinds[i].type = snmp.ObjectType[varbinds[i].type]; } - getSession(host, community, node.version).set(varbinds, function (error, varbinds) { + getSession(host, community, node.version, node.timeout).set(varbinds, function (error, varbinds) { if (error) { node.error(error.toString(), msg); } @@ -93,14 +94,13 @@ module.exports = function (RED) { RED.nodes.registerType("snmp set", SnmpSNode); - - function SnmpTNode(n) { RED.nodes.createNode(this, n); 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.timeout = Number(n.timeout || 5) * 1000; var node = this; var maxRepetitions = 20; @@ -116,7 +116,7 @@ module.exports = function (RED) { var oids = node.oids || msg.oid; if (oids) { msg.oid = oids; - getSession(host, community, node.version).table(oids, maxRepetitions, function (error, table) { + getSession(host, community, node.version, node.timeout).table(oids, maxRepetitions, function (error, table) { if (error) { node.error(error.toString(), msg); } @@ -153,12 +153,14 @@ module.exports = function (RED) { } RED.nodes.registerType("snmp table", SnmpTNode); + function SnmpSubtreeNode(n) { RED.nodes.createNode(this, n); 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.timeout = Number(n.timeout || 5) * 1000; var node = this; var maxRepetitions = 20; var response = []; @@ -181,7 +183,7 @@ module.exports = function (RED) { var oids = node.oids || msg.oid; if (oids) { msg.oid = oids; - getSession(host, community, node.version).subtree(msg.oid, maxRepetitions, feedCb, function (error) { + getSession(host, community, node.version, node.timeout).subtree(msg.oid, maxRepetitions, feedCb, function (error) { if (error) { node.error(error.toString(), msg); } @@ -200,12 +202,14 @@ module.exports = function (RED) { } RED.nodes.registerType("snmp subtree", SnmpSubtreeNode); + function SnmpWalkerNode(n) { RED.nodes.createNode(this, n); 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.timeout = Number(n.timeout || 5) * 1000; var node = this; var maxRepetitions = 20; var response = []; @@ -229,7 +233,7 @@ module.exports = function (RED) { var community = node.community || msg.community; if (oids) { msg.oid = oids; - getSession(host, community, node.version).walk(msg.oid, maxRepetitions, feedCb, function (error) { + getSession(host, community, node.version, node.timeout).walk(msg.oid, maxRepetitions, feedCb, function (error) { if (error) { node.error(error.toString(), msg); }