forked from markitondemand/node-perfmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perfmon.js
212 lines (172 loc) · 3.9 KB
/
perfmon.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
var os = require('os');
var TypePerf = require('./lib/TypePerf');
var PerfmonStream = require('./lib/PerfmonStream');
var hosts = {};
var hostname = os.hostname();
function subset(a, b) {
// is b a subset of a?
return b.every(function(value) {
return (a.indexOf(value) != -1);
});
}
function stringOrArrayToArray(value) {
if (Array.isArray(value)) {
return value;
}
if (typeof(value) == 'string') {
return [value];
}
return false;
}
function init(host, options, pstream) {
var typePerf;
if (!hosts[host]) {
hosts[host] = new TypePerf(host);
}
typePerf = hosts[host];
if (options.sampleInterval) {
typePerf.sampleInterval = options.sampleInterval;
}
if (options.sampleCount) {
typePerf.sampleCount = options.sampleCount;
}
pstream.attach(typePerf);
// this is wrong. if the current typeperf ALREADY contains the new
// counters, you should not do anything!
if (!subset(typePerf.counters(), options.counters)) {
typePerf.counters(typePerf.counters().concat(options.counters));
typePerf.spawn();
}
else if (!typePerf.cp) {
typePerf.spawn();
}
}
function each(host, cb) {
if (!host) {
// do em all
for (var h in hosts) {
cb(hosts[h]);
}
}
else {
if (!Array.isArray(host)) {
host = [host];
}
host.forEach(function(h) {
if (hosts[h]) {
cb(hosts[h]);
}
});
}
}
function parseInputs(args) {
// counters can be array or string
// options is object
// cb must be func
// host can be array or string
// if 0 is object, options == 0
// cb is last arg fn or nothing
// if 0 is string or array, counters = [0]
// if 1 is string or array, hosts = [1]
// cb is last arg fn or nothing
var counters = stringOrArrayToArray(args[0]);
var hosts = stringOrArrayToArray(args[1]);
var cb = args[args.length - 1];
var options = {};
if (counters) {
options = {
counters: counters
}
if (hosts) {
options.hosts = hosts;
}
}
else if (typeof(args[0]) == 'object') {
options = args[0];
options.counters = stringOrArrayToArray(options.counters || options.counter);
options.hosts = stringOrArrayToArray(options.hosts || options.host);
}
if (!options.counters) {
options.error = 'perfmon invalid inputs';
}
if (!options.hosts) {
options.hosts = [hostname];
}
return {
options: options,
cb: cb
}
}
/*
public interface
acceptable inputs
perfmon(counters)
perfmon(counters, cb)
perfmon(counters, host, cb)
perfmon(options)
perfmon(options, cb)
*/
function perfmon() {
var inputs = parseInputs(arguments);
var pstream = new PerfmonStream(inputs.options.counters);
if (typeof(inputs.cb) == 'function') {
pstream.on('data', function(data) {
inputs.cb(null, data);
});
pstream.on('error', inputs.cb);
}
if (inputs.options.error) {
process.nextTick(function() {
pstream.emit('error', inputs.options.error);
});
}
else {
inputs.options.hosts.forEach(function(h) {
init(h, inputs.options, pstream);
});
}
return pstream;
}
/*
acceptable inputs
perfmon.list(counterFamily, cb)
perfmon.list(counterFamily, hosts, cb)
*/
perfmon.list = function() {
var inputs = parseInputs(arguments);
// dont pass counters for list, no need to filter anything
var pstream = new PerfmonStream();
inputs.options.hosts.forEach(function(host) {
TypePerf.listCounters(inputs.options.counters, host, function(err, data) {
if (err) {
pstream.emit('error', err);
}
else {
pstream.write(data);
}
});
});
if (typeof(inputs.cb) == 'function') {
pstream.on('data', function(data) {
inputs.cb(null, data);
});
pstream.on('error', inputs.cb);
}
if (inputs.options.error) {
process.nextTick(function() {
pstream.emit('error', inputs.options.error);
});
}
return pstream;
};
perfmon.stop = function(host) {
each(host, function(typePerf) {
typePerf.kill();
});
};
perfmon.start = function(host) {
each(host, function(typePerf) {
typePerf.spawn();
});
};
module.exports = perfmon;