forked from immunda/monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.js
executable file
·71 lines (54 loc) · 2.45 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
66
67
68
69
70
71
var sys = require('sys'),
amqp = require('amqp'),
ws = require('websocket-server'),
express = require('express'),
config = require('./config');
// Express
var web = express.createServer();
web.set('view engine', 'ejs');
// Should wrap in a node development environment check
web.use('/static/', express.static(__dirname + '/static'));
web.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
web.get('/', function(req, res){
res.render('index.ejs', {layout: false, servers: config.servers, websocket_host: config.ws.host});
});
web.listen(config.web.port, config.web.host);
var socket_server = ws.createServer();
// Define AMQP (RabbitMQ) connection options
var connection = amqp.createConnection({
host: config.amqp.host,
});
// Wait for connection to become established.
connection.on('ready', function () {
// Create servers exchange if it doesn't exist
var servers_exch = connection.exchange('servers')
// Create queue and bind
var q = connection.queue('server-stats', function(queue) {
q.bind('servers', 'stats.#');
});
// Receive messages
q.subscribe(function (msg) {
// Marshal received string into JSON
data = msg.data.toString('utf8');
stats = JSON.parse(data);
// Maybe move this to the poller, trying to keep it as generic as possible at the moment
if (typeof stats.memory_total !== 'undefined' && typeof stats.memory_free !== 'undefined' && typeof stats.memory_buffered !== 'undefined' && typeof stats.memory_cached !== 'undefined') {
stats.memory = {};
stats.memory.low = parseInt((stats.memory_total/2)/1024);
stats.memory.high = parseInt((stats.memory_total*0.75)/1024);
stats.memory.total = parseInt(stats.memory_total/1024);
stats.memory.used = stats.memory.total - (parseInt(stats.memory_free/1024) + parseInt(stats.memory_buffered/1024) + parseInt(stats.memory_cached/1024));
}
delete stats.memory_free;
delete stats.memory_buffered;
delete stats.memory_cached;
delete stats.memory_total;
// Easier than combining sys, user and nice load
stats.cpu_use = 100 - parseInt(stats.cpu_idle);
delete stats.cpu_idle;
// Relay JSON via websocket server (as string)
//console.log(stats);
socket_server.broadcast(JSON.stringify(stats));
});
});
socket_server.listen(config.ws.port, config.ws.host);