-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (61 loc) · 2.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
var validateip = require('validate-ip'),
c = require('chalk'),
condenseWhitespace = require('condense-whitespace'),
_ = require('underscore'),
spawn = require('child_process').spawn,
fs = require('fs'),
config = require('./config'),
pmacctdProcess = null,
pcapFilter = '',
pmacctdProcesses = {};
var processTopTalkers = function(direction, topTalkers) {
topTalkers = condenseWhitespace(topTalkers).split('\n').filter(function(i) {
return i.split(' ').length == 3 && validateip(i.split(' ')[0]);
}).map(function(i) {
return {
ip: i.split(' ')[0],
packets: +i.split(' ')[1],
bytes: +i.split(' ')[2],
};
});
topTalkers = {
packets: _.sortBy(topTalkers, 'packets').reverse().slice(0, config.listLimit),
bytes: _.sortBy(topTalkers, 'bytes').reverse().slice(0, config.listLimit),
};
console.log('Top Talkers ' +direction+':\n', topTalkers);
};
process.on('exit', function() {
_.each([pmacctdProcesses.in, pmacctdProcesses.out], function(pmacctdProcess) {
console.log(c.yellow('\nKilling pmacctd child process with pid ' + c.red.bgWhite(pmacctdProcess.pid)));
pmacctdProcess.kill();
});
});
_.each(config.localNetworks, function(net, index) {
if (index > 0)
pcapFilter += ' or ';
pcapFilter += 'dst net ' + net;
});
var pmArgs_in = '-i ' + config.interface + ' -P print -r ' + config.interval + ' -c ' + 'src' + '_host ' + pcapFilter;
pcapFilter = '';
_.each(config.localNetworks, function(net, index) {
if (index > 0)
pcapFilter += ' or ';
pcapFilter += 'src net ' + net;
});
var pmArgs_out = '-i ' + config.interface + ' -P print -r ' + config.interval + ' -c ' + 'src' + '_host ' + pcapFilter;
pmacctdProcesses.in = spawn(config.pmacctd, pmArgs_in.split(' '));
pmacctdProcesses.in.on('exit', function(code) {
console.log('pmacctd exited with code', code);
});
pmacctdProcesses.in.stdout.on('data', function(data) {
processTopTalkers('in', data.toString());
});
pmacctdProcesses.in.stderr.on('data', function(data) {});
pmacctdProcesses.out = spawn(config.pmacctd, pmArgs_out.split(' '));
pmacctdProcesses.out.on('exit', function(code) {
console.log('pmacctd exited with code', code);
});
pmacctdProcesses.out.stdout.on('data', function(data) {
processTopTalkers('out', data.toString());
});
pmacctdProcesses.out.stderr.on('data', function(data) {});