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

Added subtree and walker nodes. (#200)

Thanks @mikakaraila  - very useful.
This commit is contained in:
Mika Karaila 2016-04-12 20:39:19 +03:00 committed by Dave Conway-Jones
parent cc9a6d887b
commit 163345cf5c
2 changed files with 210 additions and 0 deletions

View File

@ -126,3 +126,117 @@
} }
}); });
</script> </script>
<script type="text/x-red" data-template-name="snmp subtree">
<div class="form-row">
<label for="node-input-host"><i class="fa fa-globe"></i> Host</label>
<input type="text" id="node-input-host" placeholder="localhost">
</div>
<div class="form-row">
<label for="node-input-community"><i class="fa fa-user"></i> Community</label>
<input type="text" id="node-input-community" placeholder="public">
</div>
<div class="form-row">
<label for="node-input-version"><i class="fa fa-bookmark"></i> Version</label>
<select type="text" id="node-input-version" style="width: 150px;">
<option value="1">v1</option>
<option value="2c">v2c</option>
</select>
</div>
<div class="form-row">
<label for="node-input-oids"><i class="fa fa-tags"></i> OID</label>
<input type="text" id="node-input-oids" placeholder="e.g. 1.3.6.1.2.1.1.5.0">
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-tips">Tip: ONLY accepts a single OID (node).</div>
</script>
<script type="text/x-red" data-help-name="snmp subtree">
<p>Simple SNMP oid subtree fetcher. Triggered by any input.</p>
<p><code>msg.oid</code> may contain the oid of a table to request.</p>
<p>The node will output <code>msg.payload</code> and <code>msg.oid</code>.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('snmp subtree',{
category: 'network-input',
color:"YellowGreen",
defaults: {
host: {value:"",required:true},
community: {value:"public",required:true},
version: {value:"1",required:true},
oids: {value:""},
name: {value:""}
},
inputs:1,
outputs:1,
icon: "snmp.png",
label: function() {
return this.name||"snmp subtree "+this.host;
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});
</script>
<script type="text/x-red" data-template-name="snmp walker">
<div class="form-row">
<label for="node-input-host"><i class="fa fa-globe"></i> Host</label>
<input type="text" id="node-input-host" placeholder="localhost">
</div>
<div class="form-row">
<label for="node-input-community"><i class="fa fa-user"></i> Community</label>
<input type="text" id="node-input-community" placeholder="public">
</div>
<div class="form-row">
<label for="node-input-version"><i class="fa fa-bookmark"></i> Version</label>
<select type="text" id="node-input-version" style="width: 150px;">
<option value="1">v1</option>
<option value="2c">v2c</option>
</select>
</div>
<div class="form-row">
<label for="node-input-oids"><i class="fa fa-tags"></i> OID</label>
<input type="text" id="node-input-oids" placeholder="e.g. 1.3.6.1.2.1.1.5.0">
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-tips">Tip: ONLY accepts a single OID (node).</div>
</script>
<script type="text/x-red" data-help-name="snmp walker">
<p>Simple SNMP oid walker fetcher. Triggered by any input.</p>
<p><code>msg.oid</code> may contain the oid of a table to request.</p>
<p>The node will output <code>msg.payload</code> and <code>msg.oid</code>.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('snmp walker',{
category: 'network-input',
color:"YellowGreen",
defaults: {
host: {value:"",required:true},
community: {value:"public",required:true},
version: {value:"1",required:true},
oids: {value:""},
name: {value:""}
},
inputs:1,
outputs:1,
icon: "snmp.png",
label: function() {
return this.name||"snmp walker "+this.host;
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});
</script>

View File

@ -115,4 +115,100 @@ module.exports = function(RED) {
}); });
} }
RED.nodes.registerType("snmp table",SnmpTNode); 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);
} }