mirror of
https://github.com/node-red/node-red-nodes.git
synced 2023-10-10 13:36:58 +02:00
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 <bimalyn@us.ibm.com> * Replaced tabs with 4 spaces. Signed-off-by: Bryan Malyn <bimalyn@us.ibm.com>
This commit is contained in:
parent
cdd3b04492
commit
0f7e8385b8
@ -37,7 +37,7 @@
|
|||||||
category: 'network-input',
|
category: 'network-input',
|
||||||
color:"YellowGreen",
|
color:"YellowGreen",
|
||||||
defaults: {
|
defaults: {
|
||||||
host: {value:"",required:true},
|
host: {value:""},
|
||||||
community: {value:"public",required:true},
|
community: {value:"public",required:true},
|
||||||
version: {value:"1",required:true},
|
version: {value:"1",required:true},
|
||||||
oids: {value:""},
|
oids: {value:""},
|
||||||
@ -94,7 +94,7 @@
|
|||||||
category: 'network-input',
|
category: 'network-input',
|
||||||
color:"YellowGreen",
|
color:"YellowGreen",
|
||||||
defaults: {
|
defaults: {
|
||||||
host: {value:"",required:true},
|
host: {value:""},
|
||||||
community: {value:"public",required:true},
|
community: {value:"public",required:true},
|
||||||
version: {value:"1",required:true},
|
version: {value:"1",required:true},
|
||||||
oids: {value:""},
|
oids: {value:""},
|
||||||
@ -150,7 +150,7 @@
|
|||||||
category: 'network-input',
|
category: 'network-input',
|
||||||
color:"YellowGreen",
|
color:"YellowGreen",
|
||||||
defaults: {
|
defaults: {
|
||||||
host: {value:"",required:true},
|
host: {value:""},
|
||||||
community: {value:"public",required:true},
|
community: {value:"public",required:true},
|
||||||
version: {value:"1",required:true},
|
version: {value:"1",required:true},
|
||||||
oids: {value:""},
|
oids: {value:""},
|
||||||
@ -208,7 +208,7 @@
|
|||||||
category: 'network-input',
|
category: 'network-input',
|
||||||
color:"YellowGreen",
|
color:"YellowGreen",
|
||||||
defaults: {
|
defaults: {
|
||||||
host: {value:"",required:true},
|
host: {value:""},
|
||||||
community: {value:"public",required:true},
|
community: {value:"public",required:true},
|
||||||
version: {value:"1",required:true},
|
version: {value:"1",required:true},
|
||||||
oids: {value:""},
|
oids: {value:""},
|
||||||
|
140
io/snmp/snmp.js
140
io/snmp/snmp.js
@ -5,16 +5,21 @@ module.exports = function(RED) {
|
|||||||
|
|
||||||
function SnmpNode(n) {
|
function SnmpNode(n) {
|
||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
this.community = n.community || "public";
|
this.community = n.community;
|
||||||
this.host = n.host || "127.0.0.1";
|
this.host = n.host;
|
||||||
this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
|
this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
|
||||||
this.oids = n.oids.replace(/\s/g,"");
|
this.oids = n.oids.replace(/\s/g,"");
|
||||||
this.session = snmp.createSession(this.host, this.community, {version: this.version});
|
|
||||||
var node = this;
|
var node = this;
|
||||||
|
|
||||||
this.on("input",function(msg) {
|
this.on("input",function(msg) {
|
||||||
var oids = node.oids || msg.oid;
|
var oids = node.oids || msg.oid;
|
||||||
if (oids) {
|
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) {
|
node.session.get(oids.split(","), function(error, varbinds) {
|
||||||
if (error) {
|
if (error) {
|
||||||
node.error(error.toString(),msg);
|
node.error(error.toString(),msg);
|
||||||
@ -40,18 +45,22 @@ module.exports = function(RED) {
|
|||||||
node.warn("No oid(s) to search for");
|
node.warn("No oid(s) to search for");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.on("close", function(){
|
||||||
|
if (node.session){
|
||||||
|
node.session.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
RED.nodes.registerType("snmp",SnmpNode);
|
RED.nodes.registerType("snmp",SnmpNode);
|
||||||
|
|
||||||
function SnmpTNode(n) {
|
function SnmpTNode(n) {
|
||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
this.community = n.community || "public";
|
this.community = n.community;
|
||||||
this.host = n.host || "127.0.0.1";
|
this.host = n.host;
|
||||||
this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
|
this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
|
||||||
this.oids = n.oids.replace(/\s/g,"");
|
this.oids = n.oids.replace(/\s/g,"");
|
||||||
this.session = snmp.createSession(this.host, this.community, {version: this.version});
|
|
||||||
var node = this;
|
var node = this;
|
||||||
var msg;
|
|
||||||
var maxRepetitions = 20;
|
var maxRepetitions = 20;
|
||||||
|
|
||||||
function sortInt(a, b) {
|
function sortInt(a, b) {
|
||||||
@ -60,9 +69,19 @@ module.exports = function(RED) {
|
|||||||
else { return 0; }
|
else { return 0; }
|
||||||
}
|
}
|
||||||
|
|
||||||
function responseCb(error, table) {
|
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});
|
||||||
|
node.session.table(oids, maxRepetitions, function(error, table) {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error(error.toString());
|
node.error(error.toString());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var indexes = [];
|
var indexes = [];
|
||||||
@ -88,45 +107,31 @@ module.exports = function(RED) {
|
|||||||
msg.payload = table;
|
msg.payload = table;
|
||||||
node.send(msg);
|
node.send(msg);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
this.on("input",function(m) {
|
|
||||||
msg = m;
|
|
||||||
var oids = node.oids || msg.oid;
|
|
||||||
if (oids) {
|
|
||||||
msg.oid = oids;
|
|
||||||
node.session.table(oids, maxRepetitions, responseCb);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
node.warn("No oid to search for");
|
node.warn("No oid to search for");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.on("close", function(){
|
||||||
|
if (node.session){
|
||||||
|
node.session.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
RED.nodes.registerType("snmp table",SnmpTNode);
|
RED.nodes.registerType("snmp table",SnmpTNode);
|
||||||
|
|
||||||
function SnmpSubtreeNode(n) {
|
function SnmpSubtreeNode(n) {
|
||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
this.community = n.community || "public";
|
this.community = n.community;
|
||||||
this.host = n.host || "127.0.0.1";
|
this.host = n.host;
|
||||||
this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
|
this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
|
||||||
this.oids = n.oids.replace(/\s/g,"");
|
this.oids = n.oids.replace(/\s/g,"");
|
||||||
this.session = snmp.createSession(this.host, this.community, {version: this.version});
|
|
||||||
var node = this;
|
var node = this;
|
||||||
var maxRepetitions = 20;
|
var maxRepetitions = 20;
|
||||||
var response = [];
|
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) {
|
function feedCb(varbinds) {
|
||||||
for (var i = 0; i < varbinds.length; i++) {
|
for (var i = 0; i < varbinds.length; i++) {
|
||||||
if (snmp.isVarbindError(varbinds[i])) {
|
if (snmp.isVarbindError(varbinds[i])) {
|
||||||
@ -134,7 +139,7 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
//console.log(varbinds[i].oid + "|" + varbinds[i].value);
|
//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;
|
var oids = node.oids || msg.oid;
|
||||||
if (oids) {
|
if (oids) {
|
||||||
msg.oid = oids;
|
msg.oid = oids;
|
||||||
node.session.subtree(msg.oid, maxRepetitions, feedCb, doneCb);
|
if (msg.host && node.host || msg.community && node.community) {
|
||||||
//node.session.subtree(oids, maxRepetitions, responseCb);
|
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 {
|
else {
|
||||||
node.warn("No oid to search for");
|
node.warn("No oid to search for");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.on("close", function(){
|
||||||
|
if (node.session){
|
||||||
|
node.session.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
RED.nodes.registerType("snmp subtree",SnmpSubtreeNode);
|
RED.nodes.registerType("snmp subtree",SnmpSubtreeNode);
|
||||||
|
|
||||||
function SnmpWalkerNode(n) {
|
function SnmpWalkerNode(n) {
|
||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
this.community = n.community || "public";
|
this.community = n.community;
|
||||||
this.host = n.host || "127.0.0.1";
|
this.host = n.host;
|
||||||
this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
|
this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
|
||||||
this.oids = n.oids.replace(/\s/g,"");
|
this.oids = n.oids.replace(/\s/g,"");
|
||||||
this.session = snmp.createSession(this.host, this.community, {version: this.version});
|
|
||||||
var node = this;
|
var node = this;
|
||||||
var maxRepetitions = 20;
|
var maxRepetitions = 20;
|
||||||
var response = [];
|
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) {
|
function feedCb(varbinds) {
|
||||||
for (var i = 0; i < varbinds.length; i++) {
|
for (var i = 0; i < varbinds.length; i++) {
|
||||||
if (snmp.isVarbindError(varbinds[i])) {
|
if (snmp.isVarbindError(varbinds[i])) {
|
||||||
@ -183,21 +196,44 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
//console.log(varbinds[i].oid + "|" + varbinds[i].value);
|
//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) {
|
this.on("input",function(msg) {
|
||||||
|
node.msg = msg;
|
||||||
var oids = node.oids || msg.oid;
|
var oids = node.oids || msg.oid;
|
||||||
if (oids) {
|
if (oids) {
|
||||||
msg.oid = 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 {
|
else {
|
||||||
node.warn("No oid to search for");
|
node.warn("No oid to search for");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.on("close", function(){
|
||||||
|
if (node.session){
|
||||||
|
node.session.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
RED.nodes.registerType("snmp walker",SnmpWalkerNode);
|
RED.nodes.registerType("snmp walker",SnmpWalkerNode);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user