forked from m0llysour/node-resource-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.js
65 lines (51 loc) · 1.57 KB
/
monitor.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
// modules
var fs = require('fs');
var redis = require('redis');
// config file
var config = require('./config');
// redis client connect
var client;
function redisConnect() {
client = redis.createClient(config.redis.port, config.redis.host);
client.on("error", function (err) {
console.log("Redis "+err);
});
}
redisConnect();
// profiling
var timeOut = 5;
setInterval(function() {
date = new Date();
data = fs.readFileSync('/proc/stat', 'utf8');
lines = data.split("\n");
line = lines[0];
line = line.replace(/ +/,' ').split(/ /);
// console.log(line);
totalProc = line[1] + line[2] + line[3] + line[4];
processor = 100 - (((totalProc - line[4]) * 100) / totalProc);
console.log(processor);
var memTotal, memFree;
data = fs.readFileSync('/proc/meminfo', 'utf8');
lines = data.split("\n");
memTotal = lines[0].replace(/[^0-9]/g, '');
memFree = lines[1].replace(/[^0-9]/g, '');
ts = Math.round(date.getTime() / 1000) + date.getTimezoneOffset() * 60;
mem = process.memoryUsage();
memoryNode = (mem.rss / 1024);
var profileData = {
ts: ts,
memRss: mem.rss,
memHeapTotal: mem.heapTotal,
memHeapUsed: mem.heapUsed,
processor: processor,
memory: 100 - ((memFree * 100) / memTotal),
memoryNode: (memoryNode * 100) / memTotal,
};
client.zadd('profileStats', String(ts), JSON.stringify(profileData));
// data purge
date = new Date();
ts = Math.round(date.getTime() / 1000) + date.getTimezoneOffset() * 60;
// removing older than 10 minutes (600 secs)
ts -= 600;
client.zremrangebyscore('profileStats', 0, ts);
}, timeOut*1000);