Post Syndicated from Anonymous original http://deliantech.blogspot.com/2015/09/embeddedjs-async-reimplementation.html
Tag Archives: node.js
Node.JS module to access Cisco IOS XR XML interface
Post Syndicated from Anonymous original http://deliantech.blogspot.com/2015/03/nodejs-module-to-access-cisco-ios-xr.html
Module for Cisco XML API interface IOS XR
Installation
npm install node-ciscoxml
Usage
Load the module
var cxml = require('node-ciscoxml');
var c = cxml( { ...connect options.. });
Module init and connect options
var cxml = require('node-ciscoxml');
var c = cxml( {
host: '10.10.1.1',
port: 5000,
username: 'xmlapi',
password: 'xmlpass'
});
connect method
var cxml = require('node-ciscoxml');
var c = cxml();
c.connect( {
host: '10.10.1.1',
port: 5000,
username: 'xmlapi',
password: 'xmlpass'
});
var cxml = require('node-ciscoxml');
cxml().connect( {
host: '10.10.1.1',
port: 5000,
username: 'xmlapi',
password: 'xmlpass'
}, function(err) {
if (!err)
console.log('Successful connection');
});
var cxml = require('node-ciscoxml');
cxml({
host: '10.10.1.1',
port: 5000,
username: 'xmlapi',
password: 'xmlpass'
}).connect(function(err) {
if (!err)
console.log('Successful connection');
});
var cxml = require('node-ciscoxml');
var fs = require('fs');
cxml({
host: '10.10.1.1',
port: 38752,
username: 'xmlapi',
password: 'xmlpass',
ssl: {
// These are necessary only if using the client certificate authentication
key: fs.readFileSync('client-key.pem'),
cert: fs.readFileSync('client-cert.pem'),
// This is necessary only if the server uses the self-signed certificate
ca: [ fs.readFileSync('server-cert.pem') ]
}
}).connect(function(err) {
if (!err)
console.log('Successful connection');
});
disconnect method
sendRaw method
var cxml = require('node-ciscoxml');
var c = cxml({
host: '10.10.1.1',
port: 5000,
username: 'xmlapi',
password: 'xmlpass'
});
c.sendRaw('<Request><GetDataSpaceInfo/></Request>',function(err,data) {
console.log('Received',err,data);
});
sendRawObj method
var cxml = require('node-ciscoxml');
var c = cxml({
host: '10.10.1.1',
port: 5000,
username: 'xmlapi',
password: 'xmlpass'
});
c.sendRawObj({ GetDataSpaceInfo: '' },function(err,data) {
console.log('Received',err,data);
});
rootGetDataSpaceInfo method
getNext
var cxml = require('node-ciscoxml');
var c = cxml({
host: '10.10.1.1',
port: 5000,
username: 'xmlapi',
password: 'xmlpass'
});
c.sendRawObj({ Get: { Configuration: {} } },function(err,data) {
console.log('Received',err,data);
if ((!err) && data && data.Response.$.IteratorID) {
return c.getNext(data.Response.$.IteratorID,function(err,nextData) {
// .. code to merge data with nextData
});
}
// .. code
});
sendRequest method
var cxml = require('node-ciscoxml');
var c = cxml({
host: '10.10.1.1',
port: 5000,
username: 'xmlapi',
password: 'xmlpass'
});
c.sendRequest({ GetDataSpaceInfo: '' },function(err,data) {
console.log('Received',err,data);
});
requestPath method
var cxml = require('node-ciscoxml');
var c = cxml({
host: '10.10.1.1',
port: 5000,
username: 'xmlapi',
password: 'xmlpass'
});
c.requestPath('Get.Configuration.Hostname',function(err,data) {
console.log('Received',err,data);
});
reqPathPath method
var cxml = require('node-ciscoxml');
var c = cxml({
host: '10.10.1.1',
port: 5000,
username: 'xmlapi',
password: 'xmlpass'
});
c.reqPathPath('Get.Configuration.Hostname',/Hostname/,function(err,data) {
console.log('Received',data[0]);
// The output should be something like
// [ 'Response("MajorVersion"="1","MinorVersion"="0").Get.Configuration.Hostname("MajorVersion"="1","MinorVersion"="0")',
'asr9k-router' ]
});
getConfig method
c.getConfig(function(err,config) {
console.log(err,config);
});
cliConfig method
c.cliConfig('username testuser\ngroup operator\n',function(err,data) {
console.log(err,data);
c.commit();
});
cliExec method
c.cliExec('show interfaces',function(err,data) {
console.log(err,data?data.Response.CLI[0].Exec[0]);
});
commit method
c.commit(function(err,data) {
console.log(err,data);
});
lock method
c.lock(function(err,data) {
console.log(err,data);
});
unlock method
c.unlock(function(err,data) {
console.log(err,data);
});
Configure Cisco IOS XR for XML agent
xml agent
vrf default
ipv4 access-list SECUREACCESS
!
ipv6 enable
session timeout 10
iteration on size 100000
!
Debugging
require('debug').enable('ciscoxml');
node.js module implementing EventEmitter interface using MongoDB tailable cursors as backend
Post Syndicated from Anonymous original http://deliantech.blogspot.com/2015/03/nodejs-module-implementing-eventemitter.html
Module for creating event bus interface based on MongoDB tailable cursors
Installation
npm install node-mongotailableevents
Short
var ev = require('node-mongotailableevents');
var e = ev( { ...options ... }, callback );
e.on('event',callback);
e.emit('event',data);
Initialization and options
- mongoUrl (default mongodb://127.0.0.1/test) – the URL to the mongo database
- mongoOptions (default none) – Specific options to be used for the connection to the mongo database
- name (default tailedEvents) – the name of the capped collection that will be created if it does not exists
- size (default 1000000) – the maximum size of the capped collection (when reached, the oldest records will be automatically removed)
- max (default 1000) – the maximum size in amount of records for the capped collection
var ev = require('node-mongotailableevents');
var e = ev();
var ev = require('node-mongotailableevents');
var e = ev({
mongoUrl: 'mongodb://127.0.0.1/mydb',
name: 'myEventCollection'
});
var ev = require('node-mongotailableevents');
ev({
mongoUrl: 'mongodb://127.0.0.1/mydb',
name: 'myEventCollection'
}, function(err, e) {
console.log('EventEmitter',e);
});
ev(function(err, e) {
console.log('EventEmitter',e);
});
Usage
ev(function(err, e) {
if (err) throw err;
e.on('myevent',function(data) {
console.log('We have received',data);
});
e.emit('myevent','my data');
});
var ev = require('node-mongotailableevents');
ev(function(err, e) {
if (err) throw err;
e.on('myevent',function(data) {
console.log('We have received',data);
});
setInterval(function() {
e.emit('myevent','my data'+parseInt(Math.random()*1000000));
},5000);
});
Example how to use node-netflowv9 and define your own netflow type decoders
Post Syndicated from Anonymous original http://deliantech.blogspot.com/2015/03/example-how-to-use-node-netflowv9-and.html
var Collector = require('node-netflowv9');var colObj = Collector(function (flow) { console.log(flow) });
colObj.listen(5000); var aclDecodeRule = { 12: 'o["$name"] = {aclId: buf.readUInt32BE($pos), aclLineId: buf.readUInt32BE($pos+4), aclCnfId: buf.readUInt32BE($pos+8) };'
}; colObj.nfTypes[33000] = { name: 'nf_f_ingress_acl_id', compileRule: aclDecodeRule }; colObj.nfTypes[33001] = { name: 'nf_f_egress_acl_id', compileRule: aclDecodeRule }; colObj.nfTypes[33002] = { name: 'nf_f_fw_ext_event', compileRule: { 2: 'o['$name']=buf.readUInt16BE($pos);' } }; colObj.nfTypes[40000] = { name: 'nf_f_username', compileRule: { 0: 'o["$name"] = buf.toString("utf8",$pos,$pos+$len);' } };
node-netflowv9 node.js module for processing of netflowv9 has been updated to 0.2.5
Post Syndicated from Anonymous original http://deliantech.blogspot.com/2015/03/node-netflowv9-nodejs-module-for.html
My node-netflowv9 library has been updated to version 0.2.5
There are few new things –
- Almost all of the IETF netflow types are decoded now. Which means practically that we support IPFIX
- Unknown NetFlow v9 type does not throw an error. It is decoded into property with name ‘unknown_type_XXX’ where XXX is the ID of the type
- Unknown NetFlow v9 Option Template scope does not throw an error. It is decoded in ‘unknown_scope_XXX’ where XXX is the ID of the scope
- The user can overwrite how different types of NetFlow are decoded and the user can define its own decoding for new types. The same for scopes. And this can happen “on fly” – at any time.
- The library supports well multiple netflow collectors running at the same time
- A lot of new options and models for using of the library has been introduced
Usage
var Collector = require('node-netflowv9');
Collector(function(flow) {
console.log(flow);
}).listen(3000);
Collector({port: 3000}).on('data',function(flow) {
console.log(flow);
});
{ header:
{ version: 9,
count: 25,
uptime: 2452864139,
seconds: 1401951592,
sequence: 254138992,
sourceId: 2081 },
rinfo:
{ address: '15.21.21.13',
family: 'IPv4',
port: 29471,
size: 1452 },
packet: Buffer <00 00 00 00 ....>
flow: [
{ in_pkts: 3,
in_bytes: 144,
ipv4_src_addr: '15.23.23.37',
ipv4_dst_addr: '16.16.19.165',
input_snmp: 27,
output_snmp: 16,
last_switched: 2452753808,
first_switched: 2452744429,
l4_src_port: 61538,
l4_dst_port: 62348,
out_as: 0,
in_as: 0,
bgp_ipv4_next_hop: '16.16.1.1',
src_mask: 32,
dst_mask: 24,
protocol: 17,
tcp_flags: 0,
src_tos: 0,
direction: 1,
fw_status: 64,
flow_sampler_id: 2 } } ]
var netflowPktDecoder = require('node-netflowv9').nfPktDecode;
....
console.log(netflowPktDecoder(buffer))
Options
Collector({ port: 5000, cb: function (flow) { console.log(flow) } })
Collector(function (flow) { console.log(flow) }).listen(port)
Collector({ cb: function (flow) { console.log(flow) } }).listen(5000)
Collector({ ipv4num: true, cb: function (flow) { console.log(flow) } }).listen(5000)
Collector({ socketType: 'udp6', cb: function (flow) { console.log(flow) } }).listen(5000)
Define your own decoders for NetFlow v9+ types
- When a new flowset template is received from the NetFlow Agent, this netflow module generates and compile (with new Function()) a decoding function
- When a netflow is received for a known flowset template (we have a compiled function for it) – the function is simply executed
{ name: 'property-name', compileRule: compileRuleObject }
{
length: 'javascript code as a string that decode this value',
...
}
{
4: 'code used to decode this netflow type with length of 4',
8: 'code used to decode this netflow type with length of 8',
0: 'code used to decode ANY OTHER length'
}
decoding code
{
0: 'o["$name"] = buf.toString("utf8",$pos,$pos+$len)'
}
Example
Collector({
port: 5000,
nfTypes: {
4444: { // 4444 is the NetFlow Type ID which decoding we want to replace
name: 'my_vendor_type4444', // This will be the property name, that will contain the decoded value, it will be also the value of the $name
compileRule: {
1: "o['$name']=buf.readUInt8($pos);", // This is how we decode type of length 1 to a number
2: "o['$name']=buf.readUInt16BE($pos);", // This is how we decode type of length 2 to a number
3: "o['$name']=buf.readUInt8($pos)*65536+buf.readUInt16BE($pos+1);", // This is how we decode type of length 3 to a number
4: "o['$name']=buf.readUInt32BE($pos);", // This is how we decode type of length 4 to a number
5: "o['$name']=buf.readUInt8($pos)*4294967296+buf.readUInt32BE($pos+1);", // This is how we decode type of length 5 to a number
6: "o['$name']=buf.readUInt16BE($pos)*4294967296+buf.readUInt32BE($pos+2);", // This is how we decode type of length 6 to a number
8: "o['$name']=buf.readUInt32BE($pos)*4294967296+buf.readUInt32BE($pos+4);", // This is how we decode type of length 8 to a number
0: "o['$name']='Unsupported Length of $len'"
}
}
},
cb: function (flow) {
console.log(flow)
}
});
var colObj = Collector(function (flow) {
console.log(flow)
});
colObj.listen(5000);
colObj.nfTypes[6789] = {
name: 'vendor_string',
compileRule: {
0: 'o["$name"] = buf.toString("utf8",$pos,$pos+$len)'
}
}
var colObj = Collector(function (flow) {
console.log(flow)
});
colObj.listen(5000);
colObj.nfTypes[14].name = 'outputInterface';
colObj.nfTypes[10].name = 'inputInterface';
Logging / Debugging the module
require('debug').enable('NetFlowV9');
var Collector = require('node-netflowv9');
Collector(function(flow) {
console.log(flow);
}).listen(5555);
Multiple collectors
var Collector = require('node-netflowv9');
Collector(function(flow) { // Collector 1 listening on port 5555
console.log(flow);
}).listen(5555);
Collector(function(flow) { // Collector 2 listening on port 6666
console.log(flow);
}).listen(6666);
NetFlowV9 Options Template
node-netflowv9 is updated to support netflow v1, v5, v7 and v9
Post Syndicated from Anonymous original http://deliantech.blogspot.com/2014/11/node-netflowv9-is-updated-to-support.html
Collector({port: 3000}).on('data',function(flow) {
console.log(flow);
});
Sencha ExtJS grid update in real time from the back-end
Post Syndicated from Anonymous original http://deliantech.blogspot.com/2014/10/sencha-extjs-grid-update-in-real-time.html
New improved version of the node-netflowv9 module for Node.JS
Post Syndicated from Anonymous original http://deliantech.blogspot.com/2014/06/new-improved-version-of-node-netflowv9.html
- bug fixes (including avoidance of an issue that happens with ASR9k and IOS XR 4.3)
- now you can start the collector (with a second parameter of true) in a mode where you want to receive only one call back per packet instead of one callback per flow (the default mode). That could be useful if you want to count the lost packets (otherwise the relation netflow packet – callback is lost)
- decrease of the code size
- now the module compiles the templates dynamically into a function (using new Function). I like this approach very much, as it creates really fast functions (in contrast to eval, Function is always JIT processed) and it allows me to spare loops, function calls and memory copy. I like to do things like that with every data structure that allows it. Anyway, as an effect of this, the new module is about 3 times faster with all the live tests I was able to perform
Simple example for Node.JS sflow collector
Post Syndicated from Anonymous original http://deliantech.blogspot.com/2014/06/simple-example-for-nodejs-sflow.html
AMF0/3 encoding/decoding in Node.JS
Post Syndicated from Anonymous original http://deliantech.blogspot.com/2014/06/amf03-encodingdecoding-in-nodejs.html
- It can encode/decode all the objects defined in both AMF0 and AMF3 (the other AMF modules in the npm repository supports partial AMF0 or partial AMF3)
- It uses Node.JS buffers (it is not necessary to do string to buffer to string conversion, as you have to do with the other modules)
It is easy to use this module. You just have to do something like this:
var amfUtils = require('node-amfutils');
var buffer = amfUtils.amf0Encode([{ a: "xxx"},b: null]);
SFlow version 5 module for Node.JS
Post Syndicated from Anonymous original http://deliantech.blogspot.com/2014/06/sflow-version-5-module-for-nodejs.html
var Collector = require('node-sflow');
Collector(function(flow) {
console.log(flow);
}).listen(3000);
However, the SFlow support in the agents – the networking equipment is usually extremely simplified – far from the richness and complexity the SFlow protocol may provide. Most of the vendors just do packet sampling and send them over SFlow as raw packet/frame header with an associated unclear counter.
In case of you having the issue specified above, this module cannot help much. You will just get the raw packet header (usually Ethernet + IP header) as a Node.JS buffer and then you have to decode it on your own. I want to keep the node-sflow module simple and I don’t plan to decode raw packet headers there as this feature is not a feature of the SFlow itself.
If you need to decode the raw packet header I can suggest one easy solution for you. You can use the pcap module from the npm repository and decode the raw header with it:
var Collector = require('node-sflow');
var pcap = require('pcap');
Collector(function(flow) {
if (flow && flow.flow.records && flow.flow.records.length>0) {
flow.flow.records.forEach(function(n) {
if (n.type == 'raw') {
if (n.protocolText == 'ethernet') {
try {
var pkt = pcap.decode.ethernet(n.header, 0);
if (pkt.ethertype!=2048) return;
console.log('VLAN',pkt.vlan?pkt.vlan.id:'none','Packet',pkt.ip.protocol_name,pkt.ip.saddr,':',pkt.ip.tcp?pkt.ip.tcp.sport:pkt.ip.udp.sport,'->',pkt.ip.daddr,':',pkt.ip.tcp?pkt.ip.tcp.dport:pkt.ip.udp.dport)
} catch(e) { console.log(e); }
}
}
});
}
}).listen(3000);