-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
181 lines (149 loc) · 4.66 KB
/
Copy pathindex.js
File metadata and controls
181 lines (149 loc) · 4.66 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*jshint multistr: true */
// log the error as soon as possible
process.on('uncaughtException', function(err) {
console.error('msg %s, name %s, stack->\n%s', err.message, err.name, err.stack || 'NONE');
process.exit(-1);
});
var _os = require('os');
var _param = require('./param.json');
var _child = require('child_process');
var CONNTRACK_NOT_INSTALLED = '\
It does not look like conntrack is installed, please install it and re-run the relay\n \
- Debian users run: sudo apt-get install conntrack\n \
- Redhat users run: https://gist.github.com/codemoran/8309269\n';
var DEFAULT_TOTAL_CONNECTIONS = (_os.totalmem()/1024) * 64;
var STATES = ['ESTABLISHED', 'FIN_WAIT', 'TIME_WAIT', 'SYN_SENT', 'UDP'];
var _maxConnections; // the maximum number of connection available
var _mode; // the level of detail to report
var _pollInterval; // the interval to poll the metrics
var _source; // the source of the metrics
// ==========
// VALIDATION
// ==========
// which mode should we report 'basic' or 'advanced'
var _mode = _param.mode || 'basic';
// how often should we poll
var _pollInterval = (_param.pollSeconds && parseFloat(_param.pollSeconds) * 1000) ||
(_param.pollInterval) ||
5000;
// if we do not have a source, then set it
var _source = (_param.source && _param.source.trim() !== '') ? _param.source : _os.hostname();
// to call conntrack we need root access
var _isRoot = process.env.USER === 'root';
if (!_isRoot)
{
console.error('This plugin requires root/sudo access');
//process.exit(1);
}
// get the max value from the system so we can view the ratio of used connections
function getMaximumConnections(cb)
{
function handleOutput(err, stderr, stdout)
{
if (err)
return cb(err);
if (stderr || !stdout)
return cb(CONNTRACK_NOT_INSTALLED);
var value = parseInt((stdout.split('=')[1] || '').trim());
if (isNaN(value))
return cb(null, DEFAULT_TOTAL_CONNECTIONS);
else
return cb(null, value);
}
_child.exec('sysctl net.netfilter.nf_conntrack_max', function(err, stdout, stderr)
{
// try the other conntrack key
if (!err && stderr && stderr.indexOf('is an unknown key') !== -1)
_child.exec('sysctl net.ipv4.netfilter.ip_conntrack_max', handleOutput);
else
handleOutput(err, stderr, stdout);
});
}
// get the conntrack connection values
function getConnTrackValues(cb)
{
var stderr = '';
var assured = 0;
var total = 0;
var unreplied = 0;
var state = {};
STATES.forEach(function(s) { state[s] = 0; });
var conntrack = _child.spawn('conntrack', ['-L']);
conntrack.stderr.on('data', function (data) { stderr += data.toString(); });
conntrack.stdout.on('data', function (data)
{
// parse the output
var lines = data.toString().split('\n');
lines.forEach(function(line)
{
var match = line.match(/^(tcp|udp)\s+(\d+)\s+(\d+)\s+(\w+)/);
if (!match)
return;
if (match[1] === 'tcp' && STATES.indexOf(match[3]) !== -1)
state[match[3]]++;
if (match[1] === 'udp')
state.UDP++;
if (line.match(/ASSURED/))
assured++;
if (line.match(/UNREPLIED/))
unreplied++;
total++;
});
});
conntrack.on('error', function(err)
{
return cb(CONNTRACK_NOT_INSTALLED);
});
conntrack.on('close', function (code)
{
if (stderr && !stderr.match(/flow entries have been shown/))
return cb(stderr);
if (code !== 0)
return cb('conntrack exited with code ' + code);
return cb && cb(null, {
assured: assured,
unreplied: unreplied,
total: total,
state: state
});
});
}
function poll(cb)
{
getConnTrackValues(function(err, ct)
{
if (err)
return console.error(err);
ct = ct || {};
ct.state = ct.state || {};
var ratio = (ct.total || 0) / _maxConnections;
if (_mode === 'basic' || _mode === 'advanced')
{
console.log('CONNTRACK_CONNECTIONS %d %s', ct.total, _source);
console.log('CONNTRACK_LIMIT %d %s', ratio, _source); // precentage
}
if (_mode === 'advanced')
{
console.log('CONNTRACK_ESTABLISHED %d %s', ct.state.ESTABLISHED || 0, _source);
console.log('CONNTRACK_FINWAIT %d %s', ct.state.FIN_WAIT || 0, _source);
console.log('CONNTRACK_TIMEWAIT %d %s', ct.state.TIME_WAIT || 0, _source);
console.log('CONNTRACK_SYNSENT %d %s', ct.state.SYN_SENT || 0, _source);
console.log('CONNTRACK_UDP %d %s', ct.state.UDP || 0, _source);
console.log('CONNTRACK_ASSURED %d %s', ct.assured || 0, _source);
console.log('CONNTRACK_UNREPLIED %d %s', ct.unreplied || 0, _source);
}
setTimeout(poll, _pollInterval);
});
}
// get the max number of connections the system
// allows so we can calculate the ratio
getMaximumConnections(function(err, maxConnections)
{
if (err)
{
console.error(err);
process.exit(1);
}
_maxConnections = maxConnections;
poll();
});