-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
97 lines (86 loc) · 1.76 KB
/
main.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
"use strict"
var async = require('async')
var plugins = [
require('./plugins/arc'),
require('./plugins/cpu'),
require('./plugins/ipmitool'),
require('./plugins/load'),
require('./plugins/mem'),
require('./plugins/network'),
require('./plugins/nvme'),
require('./plugins/processes'),
require('./plugins/swap'),
require('./plugins/zones_cpu'),
require('./plugins/zones_iops'),
require('./plugins/zones_memory'),
require('./plugins/zones_network'),
require('./plugins/zpool'),
]
function Mellorimon() {
this.stats = {}
this.last_update = 0
this.subscribers = []
}
var CACHE_TTL = 30 * 1000 /* 30 seconds */
Mellorimon.prototype.update = function(cb) {
var self = this
cb = cb || function() {}
var now = + new Date()
if(now - this.last_update < CACHE_TTL) {
cb(null)
return
}
self.subscribers.push(cb)
if(self.subscribers.length > 1) {
return
}
async.parallel(plugins, function(err, result) {
if(err) {
self.subscribers.forEach(function(s) {
process.nextTick(function() {
s.apply(err)
})
})
self.subscribers = []
return
}
self.last_update = now
self.stats = {}
result.forEach(function(v) {
for(var prop in v) {
self.stats[prop] = v[prop]
}
})
self.subscribers.forEach(function(s) {
process.nextTick(function() {
s.apply(null)
})
})
self.subscribers = []
})
}
Mellorimon.prototype.list = function list(cb) {
var self = this
this.update(function(err) {
if(err) {
cb(err)
return
}
cb(null, Object.keys(self.stats))
})
}
Mellorimon.prototype.config = function config(stat, cb) {
var self = this
this.update(function(err) {
if(err) {
cb(err)
return
}
if(!(stat in self.stats)) {
cb(null, [])
return
}
cb(null, self.stats[stat])
})
}
module.exports = Mellorimon