1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

slight edits to snmp to pass jslint-ing

This commit is contained in:
Dave Conway-Jones 2016-04-12 19:18:48 +01:00
parent 163345cf5c
commit d087281a11
3 changed files with 84 additions and 75 deletions

View File

@ -36,3 +36,7 @@ want to use `msg.oid` to provide input.
Outputs `msg.payload` containing the table of objects, and the requested `msg.oid`. Outputs `msg.payload` containing the table of objects, and the requested `msg.oid`.
Values depends on the oids being requested. Values depends on the oids being requested.
### snmp-walker
### snmp-subtree

View File

@ -1,6 +1,6 @@
{ {
"name" : "node-red-node-snmp", "name" : "node-red-node-snmp",
"version" : "0.0.6", "version" : "0.0.7",
"description" : "A Node-RED node that looks for SNMP oids.", "description" : "A Node-RED node that looks for SNMP oids.",
"dependencies" : { "dependencies" : {
"net-snmp" : "1.1.13" "net-snmp" : "1.1.13"
@ -20,5 +20,8 @@
"name": "Dave Conway-Jones", "name": "Dave Conway-Jones",
"email": "ceejay@vnet.ibm.com", "email": "ceejay@vnet.ibm.com",
"url": "http://nodered.org" "url": "http://nodered.org"
} },
"contributors": [
{ "name": "Mika Karalia" }
]
} }

View File

@ -1,5 +1,5 @@
/** /**
* Copyright 2014 IBM Corp. * Copyright 2014,2016 IBM Corp.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,7 +16,7 @@
module.exports = function(RED) { module.exports = function(RED) {
"use strict"; "use strict";
var snmp = require ("net-snmp"); var snmp = require("net-snmp");
function SnmpNode(n) { function SnmpNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
@ -24,7 +24,7 @@ module.exports = function(RED) {
this.host = n.host || "127.0.0.1"; this.host = n.host || "127.0.0.1";
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}); 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) {
@ -32,11 +32,11 @@ module.exports = function(RED) {
if (oids) { if (oids) {
node.session.get(oids.split(","), function(error, varbinds) { node.session.get(oids.split(","), function(error, varbinds) {
if (error) { if (error) {
node.error(error.toString()); node.error(error.toString(),msg);
} else { } else {
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])) {
node.error(snmp.varbindError (varbinds[i])); node.error(snmp.varbindError(varbinds[i]),msg);
} }
else { else {
if (varbinds[i].type == 4) { varbinds[i].value = varbinds[i].value.toString(); } if (varbinds[i].type == 4) { varbinds[i].value = varbinds[i].value.toString(); }
@ -63,45 +63,46 @@ module.exports = function(RED) {
this.host = n.host || "127.0.0.1"; this.host = n.host || "127.0.0.1";
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}); this.session = snmp.createSession(this.host, this.community, {version: this.version});
var node = this; var node = this;
var msg; var msg;
var maxRepetitions = 20; var maxRepetitions = 20;
function sortInt (a, b) { function sortInt(a, b) {
if (a > b) { return 1; } if (a > b) { return 1; }
else if (b > a) { return -1; } else if (b > a) { return -1; }
else { return 0; } else { return 0; }
} }
function responseCb (error, table) { function responseCb(error, table) {
if (error) { if (error) {
console.error (error.toString ()); console.error(error.toString());
} else { } else {
var indexes = []; var indexes = [];
for (var index in table) { for (var index in table) {
if (table.hasOwnProperty(index)) { if (table.hasOwnProperty(index)) {
indexes.push (parseInt (index)); indexes.push(parseInt(index));
} }
} }
indexes.sort (sortInt); indexes.sort(sortInt);
for (var i = 0; i < indexes.length; i++) { for (var i = 0; i < indexes.length; i++) {
var columns = []; var columns = [];
for (var column in table[indexes[i]]) { for (var column in table[indexes[i]]) {
if (table[indexes[i]].hasOwnProperty(column)) { if (table[indexes[i]].hasOwnProperty(column)) {
columns.push(parseInt (column)); columns.push(parseInt(column));
} }
} }
columns.sort(sortInt); columns.sort(sortInt);
console.log ("row index = " + indexes[i]); console.log("row index = " + indexes[i]);
for (var j = 0; j < columns.length; j++) { for (var j = 0; j < columns.length; j++) {
console.log (" column " + columns[j] + " = " + table[indexes[i]][columns[j]]); console.log(" column " + columns[j] + " = " + table[indexes[i]][columns[j]]);
} }
} }
msg.payload = table; msg.payload = table;
node.send(msg); node.send(msg);
} }
} }
this.on("input",function(m) { this.on("input",function(m) {
msg = m; msg = m;
var oids = node.oids || msg.oid; var oids = node.oids || msg.oid;
@ -116,45 +117,46 @@ module.exports = function(RED) {
} }
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 || "public";
this.host = n.host || "127.0.0.1"; this.host = n.host || "127.0.0.1";
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}); 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 = new Array(); var response = [];
function doneCb (error) { function doneCb(error) {
if (error) { if (error) {
console.error (error.toString ()); console.error(error.toString());
} }
else { else {
var msg = {}; var msg = {};
msg.payload=response; msg.payload = response;
node.send(msg); node.send(msg);
response.clear(); 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])) {
console.error (snmp.varbindError (varbinds[i])); node.error(snmp.varbindError(varbinds[i]));
else { }
console.log (varbinds[i].oid + "|" + varbinds[i].value); else {
response.add({oid: varbinds[i].oid, value: varbinds[i].value}); console.log(varbinds[i].oid + "|" + varbinds[i].value);
} response.add({oid: varbinds[i].oid, value: varbinds[i].value});
} }
} }
}
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) {
msg.oid = oids; msg.oid = oids;
node.session.subtree(msg.oid, maxRepetitions, feedCb, doneCb); node.session.subtree(msg.oid, maxRepetitions, feedCb, doneCb);
//node.session.subtree(oids, maxRepetitions, responseCb); //node.session.subtree(oids, maxRepetitions, responseCb);
} }
else { else {
@ -162,48 +164,48 @@ module.exports = function(RED) {
} }
}); });
} }
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 || "public";
this.host = n.host || "127.0.0.1"; this.host = n.host || "127.0.0.1";
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}); 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=new Array(); var response = [];
function doneCb (error) { function doneCb(error) {
if (error) { if (error) {
console.error (error.toString ()); node.error(error.toString());
} }
else { else {
var msg = {}; var msg = {};
msg.payload=response; msg.payload = response;
node.send(msg); node.send(msg);
response.clear(); 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])) {
console.error (snmp.varbindError (varbinds[i])); node.error(snmp.varbindError(varbinds[i]));
else { }
console.log (varbinds[i].oid + "|" + varbinds[i].value); else {
response.add({oid: varbinds[i].oid, value: varbinds[i].value}); console.log(varbinds[i].oid + "|" + varbinds[i].value);
} response.add({oid: varbinds[i].oid, value: varbinds[i].value});
} }
} }
}
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) {
msg.oid = oids; msg.oid = oids;
node.session.walk(msg.oid, maxRepetitions, feedCb, doneCb); node.session.walk(msg.oid, maxRepetitions, feedCb, doneCb);
} }
else { else {
node.warn("No oid to search for"); node.warn("No oid to search for");
@ -211,4 +213,4 @@ module.exports = function(RED) {
}); });
} }
RED.nodes.registerType("snmp walker",SnmpWalkerNode); RED.nodes.registerType("snmp walker",SnmpWalkerNode);
} };