-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.js
73 lines (59 loc) · 1.75 KB
/
app.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
var RQ = require('./rq');
var _ = require('underscore');
var path = require('path');
var appPath = path.dirname(process.mainModule.filename);
var configPath = path.join(appPath, 'config.js');
var config = require(configPath);
var readers = require('./lib/readers');
var writer = require('./lib/writer')(config);
function fetchDataForServer(requestion, server, lastMetrics) {
RQ.sequence([
readers.connectToMongoDb,
readers.fetchServerStatus,
writer.toGraphiteMetricsArray(server, config.metrics.serverStatus, lastMetrics),
writer.sendToGraphite
])(requestion, server);
}
function fetchReplicaStatus(requestion, server) {
RQ.sequence([
readers.connectToMongoDb,
readers.replSetGetStatus,
writer.replicaSetToGraphiteMetrics,
writer.sendToGraphite
])(requestion, server);
}
var lastResultArray = [];
function intervalLoop() {
var workArray = config.servers.map(function(server, index) {
return function(requestion) {
fetchDataForServer(requestion, server, lastResultArray[index]);
};
});
RQ.parallel(workArray)(function(success, failure) {
if (failure) {
console.log('Error:', failure);
}
else {
lastResultArray = success;
console.log('ServerStatus fetched and set to graphite');
}
});
var clusters = _.values(_.groupBy(config.servers, 'cluster'));
var work = clusters.map(function(servers) {
var replicaWork = servers.map(function(server, index) {
return function(requestion) {
fetchReplicaStatus(requestion, server);
};
});
return RQ.fallback(replicaWork);
});
RQ.parallel(work)(function(success, failure) {
if (failure) {
console.log('Error:', failure);
}
else {
console.log('ReplicaSetStatus fetched and set to graphite');
}
});
}
setInterval(intervalLoop, config.intervalSeconds * 1000);