/
statsd_aggr.js
92 lines (82 loc) · 1.99 KB
/
statsd_aggr.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
/*
var cli = require('cli');
cli.parse({
'thresh' : ['', 'The mtu threshold', 'integer', 1400],
'timeout': ['', 'The interval to drain_buffer', 'integer', 1000],
'rhost': ['', 'The remote host', 'string', ''],
'rport': ['', 'The remote port', 'integer', 8125],
'lhost': ['', 'The local host', 'string', '127.0.0.1'],
'rport': ['', 'The local port', 'string', 8125],
});
var thresh = cli.options.thresh;
var timeout = cli.options.timeout;
var rhost = cli.options.rhost;
var rport = cli.options.rport;
var lhost = cli.options.lhost;
var lport = cli.options.lport;
*/
var thresh = 1432; // 1500 - 68
//var thresh = 8932; // 9000 - 68
var timeout = 1000;
var rhost = '';
var rport = 8125;
var lhost = '127.0.0.1';
var lport = 8125;
var dgram = require('dgram');
var buf = new Buffer(thresh);
var length = 0;
var timeoutId;
var client = dgram.createSocket('udp4');
function drain_buffer(event)
{
if (event != 'time')
{
clearTimeout(timeoutId);
}
if (length > 0)
{
client.send(buf, 0, length, rport, rhost);
length = 0;
}
timeoutId = setTimeout(drain_buffer, timeout, 'time');
}
function handle_packet(msg, rinfo)
{
if (msg.length >= thresh)
{
client.send(msg, 0, msg.length, rport, rhost);
return;
}
if (length > 0)
{
if (msg.length + length + 1 <= thresh)
{
buf.write('\n', length++);
}
else
{
if (msg.length < length)
{
drain_buffer('full');
}
else
{
client.send(msg, 0, msg.length, rport, rhost);
return;
}
}
}
msg.copy(buf, length);
length += msg.length;
}
function handle_exit()
{
server.close();
drain_buffer('exit');
}
var server = dgram.createSocket('udp4', handle_packet);
timeoutId = setTimeout(drain_buffer, timeout, 'time');
server.bind(lport, lhost);
process.on('exit', handle_exit);
process.on('SIGINT', process.exit);
process.on('SIGTERM', process.exit);