-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
198 lines (159 loc) · 4.93 KB
/
index.js
File metadata and controls
198 lines (159 loc) · 4.93 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
'use strict'
const single = require('@telemetry-js/metric').single
const Writable = require('readable-stream').Writable
const pipeline = require('readable-stream').pipeline
const split2 = require('split2')
const cp = require('child_process')
const EventEmitter = require('events').EventEmitter
class DmCollector extends EventEmitter {
constructor (options) {
super()
if (!options) options = {}
this._retryDelay = options.retryDelay || 30 * 60e3
this._fillInterval = options.fillInterval || 12 * 60 * 60e3
this._cmd = options.cmd || 'dmesg'
this._args = options.args || ['--color=never', '-xwf', 'kern', '--time-format', 'iso']
this._nowFn = options.now || Date.now
this._restartTimer = null
this._pendingStop = null
this._dmesg = null
this._pos = [0, 0]
this._watch = this._watch.bind(this)
this._counters = new Map([
['tcp', [
{ message: 'possible syn flooding', value: 0, lastSent: 0 },
{ message: 'too many orphaned sockets', value: 0, lastSent: 0 },
{ message: 'out of memory', value: 0, lastSent: 0 }
]],
['net_ratelimit', [
{ message: 'callbacks suppressed', value: 0, lastSent: 0 }
]]
])
}
start (callback) {
for (const items of this._counters.values()) {
for (const item of items) {
item.value = 0
item.lastSent = 0
}
}
this._setPosition(this._nowFn(), 0)
this._watch()
process.nextTick(callback)
}
ping (callback) {
let date
const now = this._nowFn()
for (const [subject, items] of this._counters) {
for (const item of items) {
const { message, value, lastSent } = item
// Report periodically and on start even if 0
if (value > 0 || (now - lastSent) >= this._fillInterval) {
if (date === undefined) date = new Date(now)
const tags = { subject, message }
const opts = { unit: 'count', statistic: 'sum', tags, value, date }
item.lastSent = now
this.emit('metric', single('telemetry.dmesg.delta', opts))
}
item.value = 0
}
}
// No need to dezalgo ping()
callback()
}
stop (callback) {
if (this._restartTimer !== null) {
clearTimeout(this._restartTimer)
this._restartTimer = null
process.nextTick(callback)
} else if (this._dmesg) {
this._pendingStop = callback
this._dmesg.kill()
} else {
process.nextTick(callback)
}
}
_setPosition (ms, us) {
this._pos[0] = ms
this._pos[1] = us
}
_parse (line) {
// Parse "facility :level : 2019-03-20T17:21:18,660780+0000 subject: msg"
const res = /^([a-z]+)\s*:\s*([a-z]+)\s*:\s*([^\s]+)\s+([a-z\d_]+)\s*:\s*(.+)$/i.exec(line)
if (res === null) return
const [, /* facility */, /* level */, ts, subject, msg] = res
const [date, time] = ts.split(',')
const ms = new Date(date + 'Z').getTime()
const us = parseInt(time.split('+')[0], 10)
if (Number.isNaN(ms) || Number.isNaN(us)) return
if (ms < this._pos[0] || (ms === this._pos[0] && us <= this._pos[1])) return
this._setPosition(ms, us)
const haystack = msg.toLowerCase()
const items = this._counters.get(subject.toLowerCase())
if (items !== undefined) {
for (const item of items) {
if (haystack.indexOf(item.message) >= 0) {
item.value++
break
}
}
}
}
_watch () {
this._restartTimer = null
this._dmesg = cp.spawn(this._cmd, this._args, {
stdio: ['ignore', 'pipe', 'ignore']
})
let finished = false
const finish = (err) => {
if (finished) return
finished = true
if (this._dmesg) {
this._dmesg.kill()
this._dmesg = null
}
if (this._pendingStop) {
const callback = this._pendingStop
this._pendingStop = null
return callback(err)
}
if (err) {
try {
err.message = `${err.name} watching dmesg: ${err.message}`
err.name = 'TelemetryWarning'
} catch (_) {}
process.emitWarning(err)
} else {
process.emitWarning('Stopped watching dmesg', 'TelemetryWarning')
}
this._restartTimer = setTimeout(this._watch, this._retryDelay)
}
const writable = new Writable({
objectMode: true,
write: (line, enc, next) => {
if (finished) return
try {
this._parse(line)
} catch (err) {
return next(err)
}
next()
}
})
pipeline(this._dmesg.stdout, split2(), writable, (err) => {
// If the stream ended normally, then wait for `close` event below
if (err) finish(err)
})
this._dmesg.on('close', (code) => {
this._dmesg = null
finish(code ? new Error(`Exited with code ${code}`) : null)
})
this._dmesg.on('error', (err) => {
this._dmesg = null
finish(err)
})
}
}
module.exports = function (options) {
return new DmCollector(options)
}