-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
135 lines (115 loc) · 3.83 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict';
const Cap = require('cap').Cap;
const EventEmitter = require('events');
const decoders = require('cap').decoders;
const humanize = require('humanize');
const myLocalIp = require('my-local-ip');
const util = require('util');
const PROTOCOL = decoders.PROTOCOL;
const bufSize = 10 * 1024 * 1024;
const buffer = new Buffer(bufSize);
function BandwidthUsage(options) {
if (!options) options = {};
EventEmitter.call(this, options);
this.ip = myLocalIp;
let devices = Cap.deviceList();
if (options.interfaces) {
devices = devices.filter((d) => options.interfaces.indexOf(d.name) !== -1);
} else {
devices = devices.filter((d) => d.addresses.length);
}
this.monitors = {};
this.devices = devices;
devices.forEach((device) => {
this.monitors[device.name] = new DeviceMonitor(device, this.ip);
});
this.totalRx = -1;
this.totalTx = -1;
this.rxPerSec = -1;
this.txPerSec = -1;
this.startSampler();
}
util.inherits(BandwidthUsage, EventEmitter);
BandwidthUsage.prototype.startSampler = function startLogger() {
const run = () => {
this.totalRx = this.devices.reduce((m, device) => {
const monitor = this.monitors[device.name];
return monitor.totalRx + m;
}, 0);
this.totalTx = this.devices.reduce((m, device) => {
const monitor = this.monitors[device.name];
return monitor.totalTx + m;
}, 0);
this.rxPerSec = this.devices.reduce((m, device) => {
const monitor = this.monitors[device.name];
return monitor.rxPerSec + m;
}, 0);
this.txPerSec = this.devices.reduce((m, device) => {
const monitor = this.monitors[device.name];
return monitor.txPerSec + m;
}, 0);
};
setInterval(run, 500);
run();
};
BandwidthUsage.prototype.startTotalsLogger = function startTotalsLogger() {
setInterval(() => {
console.log('received rate', humanize.filesize(this.rxPerSec) + '/s');
console.log('send rate', humanize.filesize(this.txPerSec) + '/s');
console.log('total received', humanize.filesize(this.totalRx));
console.log('total sent', humanize.filesize(this.totalTx));
console.log('---------');
}, 2000);
};
BandwidthUsage.prototype.startLoggers = function startLoggers() {
this.devices.forEach((device) => {
this.startLogger(this.monitors[device.name], device.name);
});
};
BandwidthUsage.prototype.startLogger = function startLogger(monitor, name) {
setInterval(() => {
console.log(name, 'received rate', humanize.filesize(monitor.rxPerSec) + '/s');
console.log(name, 'send rate', humanize.filesize(monitor.txPerSec) + '/s');
console.log(name, 'total received', humanize.filesize(monitor.totalRx));
console.log(name, 'total sent', humanize.filesize(monitor.totalTx));
console.log('---------');
}, 2000);
};
exports = module.exports = BandwidthUsage;
function DeviceMonitor(device, ip) {
const c = new Cap();
const link = c.open(device.name, '', bufSize, buffer);
this.totalRx = 0;
this.totalTx = 0;
this.rxPerSec = 0;
this.txPerSec = 0;
c.on('packet', (size) => {
if (link === 'ETHERNET') {
let ret = decoders.Ethernet(buffer);
if (ret.info.type === PROTOCOL.ETHERNET.IPV4) {
ret = decoders.IPV4(buffer, ret.offset);
if (ret.info.srcaddr !== ip) {
this.totalRx += size;
} else {
this.totalTx += size;
}
}
}
});
let lastTotalRx = this.totalRx || 0;
let lastTotalTx = this.totalTx || 0;
setInterval(() => {
this.rxPerSec = Math.abs((this.totalRx - lastTotalRx));
lastTotalRx = this.totalRx;
this.txPerSec = Math.abs((this.totalTx - lastTotalTx));
lastTotalTx = this.totalTx;
}, 1000);
}
// const b = new BandwidthUsage();
// setTimeout(() => console.log(b.monitors.en0.totalRx), 1000);
//
// const b = new BandwidthUsage();
// b.startLoggers();
//
// const b = new BandwidthUsage();
// b.startTotalsLogger();