This repository was archived by the owner on Jun 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathperfmon.js
88 lines (66 loc) · 1.94 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
var util = require("util");
var events = require("events");
var spawn = require("child_process").spawn;
var boundary = require("./boundary.js");
var parse = require("csv-parse");
var _ = require("underscore");
function Perfmon(counters) {
var _this = this;
_this.counters = counters;
}
util.inherits(Perfmon, events.EventEmitter);
Perfmon.prototype.start = function() {
var _this = this;
var typeperf = spawn("typeperf", _this.counters);
var counters;
var missingCounterIndexes;
typeperf.stdout = boundary.delimited(typeperf.stdout);
typeperf.stdout.on("message", function(message) {
message = message.toString();
if (!message)
return;
if (message.indexOf("\"") == -1)
return;
parse(message, function(error, csv) {
var row = csv[0];
if (row[0] == "(PDH-CSV 4.0)") {
counters = _.rest(row, 1);
return;
}
var host = counters.length > 0 ? /\\\\?([^\\]+)\\/.exec(counters[0])[1] : "";
var time = new Date(row[0]);
var values = _.rest(row, 1);
if (!missingCounterIndexes) {
missingCounterIndexes = [];
values.forEach(function (value, index) {
if (value === "-1")
missingCounterIndexes.push(index);
});
}
for (var i = missingCounterIndexes.length - 1; i >= 0; i--)
values.splice(missingCounterIndexes[i], 1);
if (counters.length !== values.length) {
_this.emit("error");
return;
}
var result = {
host: host,
time: time,
counters: _.toArray(_.map(_.zip(counters, values), function(item) {
return {
name: item[0].replace("\\\\" + host, ""),
value: parseFloat(item[1])
};
}))
}
_this.emit("data", result);
});
});
_this.typeperf = typeperf;
};
Perfmon.prototype.stop = function() {
var _this = this;
if (_this.typeperf)
_this.typeperf.kill();
};
module.exports = Perfmon;