-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmonitor.js
More file actions
116 lines (85 loc) · 2.67 KB
/
Copy pathmonitor.js
File metadata and controls
116 lines (85 loc) · 2.67 KB
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
var childProcess = require('child_process');
var utils = require('./utils.js');
var net = require('net');
var os = require('os');
var paths = [];
var programs;
for (var i = 2; i < process.argv.length; i++) {
paths.push(process.argv[i]);
}
programs = createProcessList(paths);
for (var i = 0; i < programs.length; i++) {
var aux = [];
var pid = runProgram(programs[i].process, programs[i].arguments);
console.log('A new process has been launched with PID: ' + pid);
setTimeout(function (i, pid) {
aux = utils.getchildProcesses(pid);
aux.push(pid);
programs[i].pids = aux;
console.log('PIDS: ' + programs[i].pids)
}.bind({}, i, pid), 1000);
}
var server = net.createServer(function (connection) {
var monitorInterval;
if (server.connections === 1) {
console.log('Client open the connection...');
//Monitoring an agent sending the client information about the usage of CPU and RAM
monitorInterval = setInterval(function () {
for (var i = 0; i < programs.length; i++) {
function iterate(i) {
var res = utils.monitor(programs[i].pids, function (res) {
console.log('PID: ' + programs[i].pids + ' - CPU: ' + res.cpu + ' - Memory: ' + res.memory);
connection.write(JSON.stringify({
host:os.hostname(), name:programs[i].name,
pid:programs[i].pids, cpu:{percentage:res.cpu},
memory:{value:res.memory}}) + '\n');
});
}
iterate(i);
}
}, 500);
connection.on('end', function () {
console.log('Client closed connection...');
clearInterval(monitorInterval);
connection.end();
});
} else {
connection.end();
}
}).listen(8091);
/**
* Runs a process
* @return The PID of the process
*/
function runProgram(prog, arguments) {
var child = childProcess.spawn(prog, arguments);
var pid = child.pid;
return pid;
};
function splitAndName(path) {
'use strict';
var elems = path.split('/');
var res = elems[elems.length - 1];
return res;
};
function createProcessList(paths) {
var elems = [];
var programsWithArgs = [];
var programs = [];
for (var i = 0; i < paths.length; i++) {
elems = paths[i].split(' ');
if (elems.length > 1) {
programsWithArgs.push({process:elems[0], pids:[], name:elems[0], arguments:elems.slice(1)});
}
if (elems.length === 1) {
programs.push({process:elems[0], pids:[], name:splitAndName(elems[0])});
}
}
programs = programs.concat(programsWithArgs);
return programs;
};
process.on('uncaughtException', function onUncaughtException(err) {
'use strict';
//logger.warning('onUncaughtException', err);
console.log(err.stack);
});