-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (51 loc) · 1.68 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
'use strict'
const singleMetric = require('@telemetry-js/metric').single
const match = require('@telemetry-js/match-metric-names')
const EventEmitter = require('events').EventEmitter
const names = require('./lib/names')
const read = require('./lib/read')
const ALL_METRICS = Array.from(names.keys())
module.exports = function (options) {
return new NetCollector(options)
}
class NetCollector extends EventEmitter {
constructor (options) {
if (!options) options = {}
super()
this._byProtocol = {}
this._countOptions = { unit: 'count' }
this._snmpPath = options.snmpPath || '/proc/net/snmp'
for (const name of match(ALL_METRICS, options.metrics)) {
const metadata = names.get(name)
const protocol = metadata.protocol
const group = this._byProtocol[protocol] || (this._byProtocol[protocol] = {})
group[name] = metadata
}
}
// TODO: reuse metric objects between pings
ping (callback) {
read(this._snmpPath, (err, data) => {
if (err) {
if (err.code === 'ENOENT' && process.platform === 'win32') {
return callback()
} else {
return callback(err)
}
}
for (const protocol in this._byProtocol) {
const group = this._byProtocol[protocol]
const values = data[protocol]
if (values === undefined) continue
for (const name in group) {
const metadata = group[name]
const value = parseInt(values[metadata.column], 10)
if (Number.isNaN(value)) continue
const metric = singleMetric(name, this._countOptions)
metric.record(value)
this.emit('metric', metric)
}
}
callback()
})
}
}