-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprocessMonitor.js
95 lines (86 loc) · 2.3 KB
/
processMonitor.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
/**
*Module dependencies
*/
var exec = require('child_process').exec
, spawn = require('child_process').spawn
, util = require('../utils/util');
/**
* Expose 'getPsInfo' constructor
*/
module.exports.getPsInfo = getPsInfo;
/**
* get the process information by command 'ps auxw | grep serverId | grep pid'
*
* @param {Object} param
* @param {Function} callback
* @api public
*/
function getPsInfo(param, callback) {
if (process.platform === 'windows') return;
var pid = param.pid;
var cmd = "ps auxw | grep " + pid + " | grep -v 'grep'";
//var cmd = "ps auxw | grep -E '.+?\\s+" + pid + "\\s+'" ;
exec(cmd, function(err, output) {
if (!!err) {
if (err.code === 1) {
console.log('the content is null!');
} else {
console.error('getPsInfo failed! ' + err.stack);
}
callback(err, null);
return;
}
format(param, output, callback);
});
};
/**
* convert serverInfo to required format, and the callback will handle the serverInfo
*
* @param {Object} param, contains serverId etc
* @param {String} data, the output if the command 'ps'
* @param {Function} cb
* @api private
*/
function format(param, data, cb) {
var time = util.formatTime(new Date());
var outArray = data.toString().replace(/^\s+|\s+$/g,"").split(/\s+/);
var outValueArray = [];
for (var i = 0; i < outArray.length; i++) {
if ((!isNaN(outArray[i]))) {
outValueArray.push(outArray[i]);
}
}
var ps = {};
ps.time = time;
ps.serverId = param.serverId;
ps.serverType = ps.serverId.split('-')[0];
var pid = ps.pid = param.pid;
ps.cpuAvg = outValueArray[1];
ps.memAvg = outValueArray[2];
ps.vsz = outValueArray[3];
ps.rss = outValueArray[4];
outValueArray = [];
if (process.platform === 'darwin') {
ps.usr = 0;
ps.sys = 0;
ps.gue = 0;
cb(null, ps);
return;
}
exec('pidstat -p ' + pid, function(err, output) {
if (!!err) {
console.error('the command pidstat failed! ', err.stack);
return;
}
var outArray = output.toString().replace(/^\s+|\s+$/g,"").split(/\s+/);
for (var i = 0; i < outArray.length; i++) {
if ((!isNaN(outArray[i]))) {
outValueArray.push(outArray[i]);
}
}
ps.usr = outValueArray[1];
ps.sys = outValueArray[2];
ps.gue = outValueArray[3];
cb(null, ps);
});
};