-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnode-stat.js
136 lines (125 loc) · 3.27 KB
/
node-stat.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
/*
* nstat is statistical tools which works with node.js.
* nstat is insipred by dstat (https://github.com/dagwieers/dstat)
*/
// node modules
var fs = require('fs');
var events = require('events');
var os = require('os');
var path = require('path');
var async = require('async');
var spawn = require('child_process').spawn;
var _ = require('lodash');
function nstat() {
this._plugins = {};
this.plugin({
disk: require('./plugins/disk'),
load: require('./plugins/load'),
mem: require('./plugins/mem'),
net: require('./plugins/net'),
stat: require('./plugins/stat'),
});
}
nstat.prototype = new events.EventEmitter();
// read multiple files into single content
nstat.prototype.read = function() {
var files = Array.prototype.slice.apply(arguments);
var callback = files.pop();
var funcs = [];
var self = this;
async.each(files, function(file, done) {
fs.readFile(file, 'utf8', done);
}, function(err, results) {
if (err) {
callback(err);
} else {
callback(null, results.join(''));
}
});
};
// read lines
nstat.prototype.lines = function() {
var files = Array.prototype.slice.apply(arguments);
var endHandler = files.pop();
var lineHandler = files.pop();
var funcs = [];
var self = this;
async.each(
files,
function(file, callback) {
fs.readFile(file, 'utf8', function(err, content) {
if (err) {
callback(err);
} else {
var lines = content.split('\n');
for (var i = 0; i < lines.length; i++) {
lineHandler.call(self, lines[i]);
}
callback(null,lines);
}
});
},
endHandler
);
};
// get data from specific plugin
nstat.prototype.get = function get() {
var args = Array.prototype.slice.apply(arguments);
var callback = args.pop();
var self = this;
var funcs = {};
args.forEach(function(name) {
funcs[name] = function(callback) {
var plugin = self._plugins[name];
if (plugin) {
plugin.get.call(plugin, self, callback);
} else {
callback(new Error('plugin ' + name + ' does not found'));
}
};
});
async.series(funcs, callback);
};
// get data from child processes
nstat.prototype.exec = function exec(path, args, callback) {
var worker = spawn(path, args);
var text = '';
worker.stdin.end();
worker.stdout.setEncoding('utf8');
worker.stdout.on('data', function(data) {
text += data;
});
worker.on('error', function(err) {
callback(err);
});
worker.on('close', function(code) {
if (code === 0) {
callback(null, text);
} else {
callback(new Error(path + ' exist abnormally. code:' + code));
}
});
};
nstat.prototype.trim = function(string) {
if (!string) return string;
return string.replace(/^[\s\t]+/,'').replace(/[\s\t]+$/,'');
};
nstat.prototype.split = function(string) {
if (!string) return [];
return string.split(/[\s\t]+/);
};
nstat.prototype.plugin = function(name, plugin) {
var self = this;
if (arguments.length === 1) {
if (typeof name === 'object') {
_.each(name, function(value, name) {
self._plugins[name] = value;
});
} else if (typeof name === 'string') {
return self._plugins[name];
}
} else {
self._plugins[name] = plugin;
}
};
module.exports = new nstat();