diff --git a/io/snmp/README.md b/io/snmp/README.md index b228c81d..e71c82f8 100644 --- a/io/snmp/README.md +++ b/io/snmp/README.md @@ -34,6 +34,57 @@ want to use `msg.oid` to provide input. Outputs `msg.payload` containing a table of objects, and the requested `msg.oid`. Values depends on the oids being requested. +### snmp-set + +SNMP sets the value of one or more OIDs. + +`msg.host` may contain the host. + +`msg.community` may contain the community. + +`msg.varbinds` may contain an array of varbind JSON objects e.g.: +``` + msg.varbinds = [ + { + "oid": "1.3.6.1.2.1.1.5.0", + "type": "OctetString", + "value": "host1" + }, { + "oid": "1.3.6.1.2.1.1.6.0", + "type": "OctetString", + "value": "somewhere" + } + ]; +``` +Types can be: + + * `Boolean` + * `Integer` + * `OctetString` + * `Null` + * `OID` + * `IpAddress` + * `Counter` + * `Gauge` + * `TimeTicks` + * `Opaque` + * `Integer32` + * `Counter32` + * `Gauge32` + * `Unsigned32` + * `Counter64` + * `NoSuchObject` + * `NoSuchInstance` + * `EndOfMibView` + +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 override `msg.community`. Leave blank if you want to use `msg.community` to provide input. + +The varbinds configured in the edit config will override `msg.varbinds`. Leave blank if you want to use `msg.varbinds` to provide input. + + + ### snmp-table Simple SNMP table oid fetcher. Triggered by any input. diff --git a/io/snmp/snmp.html b/io/snmp/snmp.html index 7f3bce47..36664565 100644 --- a/io/snmp/snmp.html +++ b/io/snmp/snmp.html @@ -1,8 +1,7 @@ - + + + + + + + \ No newline at end of file diff --git a/io/snmp/snmp.js b/io/snmp/snmp.js index 3bfc25fa..008d5995 100644 --- a/io/snmp/snmp.js +++ b/io/snmp/snmp.js @@ -1,39 +1,39 @@ -module.exports = function(RED) { +module.exports = function (RED) { "use strict"; var snmp = require("net-snmp"); var sessions = {}; - function getSession(host, community, version){ + function getSession(host, community, version) { var sessionKey = host + ":" + community + ":" + version; - if (!(sessionKey in sessions)){ - sessions[sessionKey] = snmp.createSession(host, community, {version: version}); + if (!(sessionKey in sessions)) { + sessions[sessionKey] = snmp.createSession(host, community, { version: version }); } return sessions[sessionKey]; } function SnmpNode(n) { - RED.nodes.createNode(this,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.oids = n.oids.replace(/\s/g, ""); var node = this; - this.on("input",function(msg) { + 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) { - getSession(host, community, node.version).get(oids.split(","), function(error, varbinds) { + getSession(host, community, node.version).get(oids.split(","), function (error, varbinds) { if (error) { - node.error(error.toString(),msg); + node.error(error.toString(), msg); } else { for (var i = 0; i < varbinds.length; i++) { if (snmp.isVarbindError(varbinds[i])) { - node.error(snmp.varbindError(varbinds[i]),msg); + node.error(snmp.varbindError(varbinds[i]), msg); } else { if (varbinds[i].type == 4) { varbinds[i].value = varbinds[i].value.toString(); } @@ -52,14 +52,55 @@ module.exports = function(RED) { } }); } - RED.nodes.registerType("snmp",SnmpNode); + RED.nodes.registerType("snmp", SnmpNode); - function SnmpTNode(n) { - RED.nodes.createNode(this,n); + + 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.oids = n.oids.replace(/\s/g,""); + this.varbinds = n.varbinds; + var node = this; + this.on("input", function (msg) { + var host = node.host || msg.host; + var community = node.community || msg.community; + var varbinds = (node.varbinds) ? JSON.parse(node.varbinds) : msg.varbinds; + if (varbinds) { + 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) { + if (error) { + node.error(error.toString(), msg); + } + else { + for (var i = 0; i < varbinds.length; i++) { + // for version 2c we must check each OID for an error condition + if (snmp.isVarbindError(varbinds[i])) { + node.error(snmp.varbindError(varbinds[i]), msg); + } + } + } + }); + } + else { + node.warn("No varbinds to set"); + } + }); + + } + 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, ""); var node = this; var maxRepetitions = 20; @@ -69,13 +110,13 @@ module.exports = function(RED) { else { return 0; } } - this.on("input",function(msg) { + 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; - getSession(host, community, node.version).table(oids, maxRepetitions, function(error, table) { + getSession(host, community, node.version).table(oids, maxRepetitions, function (error, table) { if (error) { node.error(error.toString(), msg); } @@ -110,14 +151,14 @@ module.exports = function(RED) { } }); } - RED.nodes.registerType("snmp table",SnmpTNode); + RED.nodes.registerType("snmp table", SnmpTNode); function SnmpSubtreeNode(n) { - RED.nodes.createNode(this,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.oids = n.oids.replace(/\s/g, ""); var node = this; var maxRepetitions = 20; var response = []; @@ -129,18 +170,18 @@ module.exports = function(RED) { } else { //console.log(varbinds[i].oid + "|" + varbinds[i].value); - response.push({oid: varbinds[i].oid, value: varbinds[i].value}); + response.push({ oid: varbinds[i].oid, value: varbinds[i].value }); } } } - this.on("input",function(msg) { + 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; - getSession(host, community, node.version).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); } @@ -157,14 +198,14 @@ module.exports = function(RED) { } }); } - RED.nodes.registerType("snmp subtree",SnmpSubtreeNode); + RED.nodes.registerType("snmp subtree", SnmpSubtreeNode); function SnmpWalkerNode(n) { - RED.nodes.createNode(this,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.oids = n.oids.replace(/\s/g, ""); var node = this; var maxRepetitions = 20; var response = []; @@ -176,19 +217,19 @@ module.exports = function(RED) { } else { //console.log(varbinds[i].oid + "|" + varbinds[i].value); - response.push({oid: varbinds[i].oid, value: varbinds[i].value}); + response.push({ oid: varbinds[i].oid, value: varbinds[i].value }); } } } - this.on("input",function(msg) { + 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; - getSession(host, community, node.version).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); } @@ -205,5 +246,5 @@ module.exports = function(RED) { } }); } - RED.nodes.registerType("snmp walker",SnmpWalkerNode); + RED.nodes.registerType("snmp walker", SnmpWalkerNode); };