-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (90 loc) · 3.16 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
'use strict'
const singleMetric = require('@telemetry-js/metric').single
const match = require('@telemetry-js/match-metric-names')
const EventEmitter = require('events').EventEmitter
const drivelist = require('drivelist')
const diskusage = require('diskusage')
const after = require('after')
const ALL_METRICS = [
'telemetry.disk.free.bytes',
'telemetry.disk.free.percent',
'telemetry.disk.available.bytes',
'telemetry.disk.available.percent',
'telemetry.disk.total.bytes'
]
module.exports = function (options) {
return new DiskCollector(options)
}
class DiskCollector extends EventEmitter {
constructor (options) {
if (!options) options = {}
super()
this._devices = new Set()
this._metrics = new Set(match(ALL_METRICS, options.metrics))
this._percentOptions = { unit: 'percent' }
this._byteOptions = { unit: 'bytes' }
}
start (callback) {
this._devices.clear()
drivelist.list((err, drives) => {
if (err) return callback(err)
for (const drive of drives) {
if (drive.mountpoints == null || drive.mountpoints.length === 0) {
continue
}
const path = drive.mountpoints[0].path
if (process.platform === 'win32') {
if (/^[a-z]:\\$/i.test(path)) {
this._devices.add(path.slice(0, 2)) // E.g. "C:"
}
} else if (path) {
this._devices.add(path) // E.g. "/"
}
}
callback()
})
}
// TODO: reuse metric objects between pings
ping (callback) {
const next = after(this._devices.size, callback)
for (const device of this._devices) {
diskusage.check(device, (err, info) => {
if (err) {
console.error(err) // TODO: tbd
return next()
}
if (this._metrics.has('telemetry.disk.free.bytes')) {
const metric = singleMetric('telemetry.disk.free.bytes', this._byteOptions)
metric.tags.device = device
metric.record(info.free)
this.emit('metric', metric)
}
if (this._metrics.has('telemetry.disk.free.percent')) {
const metric = singleMetric('telemetry.disk.free.percent', this._percentOptions)
metric.tags.device = device
metric.record(info.free / info.total * 100)
this.emit('metric', metric)
}
if (this._metrics.has('telemetry.disk.available.bytes')) {
const metric = singleMetric('telemetry.disk.available.bytes', this._byteOptions)
metric.tags.device = device
metric.record(info.available)
this.emit('metric', metric)
}
if (this._metrics.has('telemetry.disk.available.percent')) {
const metric = singleMetric('telemetry.disk.available.percent', this._percentOptions)
metric.tags.device = device
metric.record(info.available / info.total * 100)
this.emit('metric', metric)
}
if (this._metrics.has('telemetry.disk.total.bytes')) {
const metric = singleMetric('telemetry.disk.total.bytes', this._byteOptions)
metric.tags.device = device
metric.record(info.total)
this.emit('metric', metric)
}
next()
})
}
}
}