-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.js
More file actions
124 lines (99 loc) · 3.03 KB
/
monitor.js
File metadata and controls
124 lines (99 loc) · 3.03 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
117
118
119
120
121
122
123
124
var childProcess = require('child_process');
var utils = require('./utils.js');
var net = require('net');
var os = require('os');
var paths = [];
for (var i = 2; i < process.argv.length; i++) {
paths.push(process.argv[i]);
}
paths = paths.sort();
var names = [];
names = splitAndName(paths);
var paths = new Array();
for (var i = 2; i < programslength; i++) {
paths.push(programs.argv[i]);
}
paths = paths.sort();
var names = [];
var pid;
var pids = [];
names = splitAndName(paths);
for (var i = 0; i < paths.length; i++) {
var aux = new Array()
pid = createAgent(paths[i]);
console.log('A new program has been launched with PID: ' + pid);
setTimeout(function () {
aux = utils.getchildProcesses(pid);
aux.push(pid);
pids.push(aux);
}, 1000);
}
var server = net.createServer(function (connection, programs) {
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 < pids.length; i++) {
function iterate(i) {
var res = utils.monitor(pids[i], function (res) {
console.log('CPU: ' + res.cpu + ' - Memory: ' + res.memory);
console.log(names[0]);
connection.write(JSON.stringify({host: os.hostname(), name: names[i], cpu: {percentage: res.cpu}, memory: {value: res.memory}}) + '\n');
});
}
iterate(i);
}
}, 500);
/*connection.on('data', function(data){
config.tranRedisServer = JSON.parse(data);
});*/
connection.on('end', function () {
console.log('Client closed connection...');
clearInterval(monitorInterval);
connection.end();
});
} else {
connection.end();
}
}).listen(8091);
/**
* Creates an agent
* @return The PID of the agent
*/
function createAgent(path) {
var child = childProcess.fork(path);
var pid = child.pid;
return pid;
}
function splitAndName(programs) {
var listRes = [];
for (var i = 0; i < programs.length; i++) {
var elems = programs[i].split('/');
elems = elems[elems.length - 1].split('.');
var res = elems[0];
listRes.push(res);
}
listRes = listRes.sort();
var aux = listRes[1];
var cont = 1;
for (var i = 0; i < listRes.length; i++) {
if (aux === listRes[i]) {
listRes[i] = listRes[i] + cont.toString();
cont++;
}
else {
cont = 1;
aux = listRes[i];
listRes[i] = listRes[i] + cont.toString();
cont++;
}
}
console.log(listRes);
return listRes;
}
process.on('uncaughtException', function onUncaughtException(err) {
'use strict';
//logger.warning('onUncaughtException', err);
console.log(err.stack);
});