From 163345cf5c8f60c30e0d6d0ff03b5d5132572521 Mon Sep 17 00:00:00 2001 From: Mika Karaila Date: Tue, 12 Apr 2016 20:39:19 +0300 Subject: [PATCH] Added subtree and walker nodes. (#200) Thanks @mikakaraila - very useful. --- io/snmp/snmp.html | 114 ++++++++++++++++++++++++++++++++++++++++++++++ io/snmp/snmp.js | 96 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 210 insertions(+) diff --git a/io/snmp/snmp.html b/io/snmp/snmp.html index 3c7e2202..5c903692 100644 --- a/io/snmp/snmp.html +++ b/io/snmp/snmp.html @@ -126,3 +126,117 @@ } }); + + + + + + + + + + + + + + diff --git a/io/snmp/snmp.js b/io/snmp/snmp.js index 4ad178cf..e40e3147 100644 --- a/io/snmp/snmp.js +++ b/io/snmp/snmp.js @@ -115,4 +115,100 @@ module.exports = function(RED) { }); } 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 = new Array(); + + 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])) + console.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=new Array(); + + 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])) + console.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); }