-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
stats.js
118 lines (101 loc) · 2.97 KB
/
stats.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
110
111
112
113
114
115
116
117
118
'use strict'
const os = require('os')
const startMeasure = cpuAverage()
function cpuAverage () {
let totalIdle = 0
let totalTick = 0
const cpus = os.cpus()
for (let i = 0, len = cpus.length; i < len; i++) {
const cpu = cpus[i]
for (const type in cpu.times) {
totalTick += cpu.times[type]
}
totalIdle += cpu.times.idle
}
return {
idle: totalIdle / cpus.length,
total: totalTick / cpus.length
}
}
function cpuCalculation () {
const endMeasure = cpuAverage()
const idleDifference = endMeasure.idle - startMeasure.idle
const totalDifference = endMeasure.total - startMeasure.total
const cpuPercentage = 100 - ~~(100 * idleDifference / totalDifference)
return cpuPercentage
}
function client () {
this.stats.connectedClients++
if (this.stats.connectedClients > this.stats.maxConnectedClients) {
this.stats.maxConnectedClients = this.stats.connectedClients
}
}
function clientDisconnect () {
this.stats.connectedClients--
}
function publish (packet) {
if (packet && packet.topic.indexOf('$SYS') < 0) {
this.stats.publishedMessages++
}
}
const aedesEvents = [
client,
clientDisconnect,
publish
]
function wire (aedesInstance, options) {
if (!aedesInstance) {
throw new Error('Need to pass an instance of aedes to enable stats')
}
options = options || {}
aedesInstance.stats = {
maxConnectedClients: 0,
connectedClients: 0,
publishedMessages: 0,
started: new Date(),
time: new Date(),
cpuUsage: 0
}
function doPub (topic, value) {
aedesInstance.publish({
topic: '$SYS/' + aedesInstance.id + '/' + topic,
payload: '' + value,
qos: 0,
retain: true
})
}
const timer = setInterval(iterate, options.interval || (1 * 1000))
const cpuUsageTimer = setInterval(function () { aedesInstance.stats.cpuUsage = cpuCalculation() }, 1000)
function iterate () {
const stats = aedesInstance.stats
stats.time = new Date()
const uptime = Math.round((stats.time - stats.started) / 1000)
const mem = process.memoryUsage()
const cpu = os.loadavg()
doPub('uptime', uptime)
doPub('time', aedesInstance.stats.time.toISOString())
doPub('clients/total', stats.connectedClients)
doPub('clients/maximum', stats.maxConnectedClients)
doPub('messages/publish/sent', stats.publishedMessages)
doPub('memory/heap/current', mem.heapUsed)
doPub('memory/heap/maximum', mem.heapTotal)
doPub('cpu/usage', stats.cpuUsage)
if (cpu && cpu.length >= 3) {
// ref: http://nodejs.org/api/os.html#os_os_loadavg
doPub('cpu/avg/last/1', cpu[0])
doPub('cpu/avg/last/5', cpu[1])
doPub('cpu/avg/last/15', cpu[2])
}
}
aedesEvents.forEach(function (event) {
aedesInstance.on(event.name, event)
})
aedesInstance.once('closed', function () {
clearInterval(timer)
clearInterval(cpuUsageTimer)
aedesEvents.forEach(function (event) {
aedesInstance.removeListener(event.name, event)
})
})
}
module.exports = wire