-
Notifications
You must be signed in to change notification settings - Fork 1
/
varnish-live.js
81 lines (72 loc) · 2.56 KB
/
varnish-live.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
var sys = require("sys"),
request = require("request"),
http = require("http"),
url = require("url"),
io = require('socket.io'),
path = require("path"),
events = require('events'),
fs = require("fs");
var last_hits = 0;
var last_misses = 0;
var last_total = 0;
var server = require('http').createServer(function(req, response){
var uri = url.parse(req.url).pathname;
var filename = path.join(process.cwd(), uri);
path.exists(filename, function(exists) {
if(!exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
return;
}
fs.readFile(filename, "binary", function(err, file) {
if(err) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
return;
}
response.writeHead(200);
response.write(file, "binary");
response.end();
});
});
});
server.listen(5000);
var metrics_emitter = new events.EventEmitter();
var riak = require('riak-js').getClient();
function get_metrics() {
var current_hits = 0;
var current_misses = 0;
var metrics = {};
var second = String(new Date().getSeconds());
riak.walk('varnishstat-rb-list', second, [['_','varnishstat-second', '1']], function(err, data) {
data[1].forEach(function(metric) {
current_misses = current_misses + metric.cache_miss;
current_hits = current_hits + metric.cache_hit;
});
var current_total = current_hits + current_misses;
if(last_total > 0) {
var rate_hits = current_hits - last_hits;
var rate_misses = current_misses - last_misses;
var rate_total = current_total - last_total;
if(rate_total > 0) {
metrics['total'] = rate_total;
metrics['hits'] = rate_hits;
metrics['misses'] = rate_misses;
metrics_emitter.emit("metrics", metrics);
}
}
last_total = current_total;
last_hits = current_hits;
last_misses = current_misses;
})
}
setInterval(get_metrics, 1000);
var listener = metrics_emitter.addListener("metrics", function(metrics) {
send_metric(metrics);
});
var socket = io.listen(server);
function send_metric(metrics) {
socket.broadcast(metrics);
}