-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·89 lines (79 loc) · 2.1 KB
/
index.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
82
83
84
85
86
87
88
89
"use strict";
var sequence = require('sequence'),
when = require('when'),
fs = require('fs'),
os = require('os'),
nconf = require('nconf'),
getConfig = require('junto'),
aws = require('plata');
var availablePlugins = {},
juntoConfig,
timer,
config;
fs.readdirSync(__dirname + '/plugins').forEach(function(pluginFilename){
availablePlugins[pluginFilename.split('.')[0]] = require(__dirname + '/plugins/' + pluginFilename);
});
getConfig('development').then(function(c){
juntoConfig = c;
aws.setLogLevel('err');
aws.connect(juntoConfig.aws);
});
module.exports.config = function(c){
config = c;
};
module.exports.start = function(ivl){
console.log('starting weatherman...');
console.log(ivl);
generateMetrics();
timer = setInterval(function(){
generateMetrics();
}, ivl*1000);
};
module.exports.stop = function(){
console.log('stopping weatherman...');
clearInterval(timer);
};
function generateMetrics(){
var allMetrics = {},
timestamp = new Date().toISOString(),
pluginOpts;
when.all(Object.keys(config.plugins).map(function(plugin){
var p = when.defer();
pluginOpts = config.plugins[plugin];
availablePlugins[plugin].addMetrics(timestamp, pluginOpts).then(function(data){
if (pluginOpts !== undefined && pluginOpts.namespace !== undefined) {
allMetrics[pluginOpts.namespace] = data;
return p.resolve();
}
else {
allMetrics[config.namespace] = data;
return p.resolve();
}
});
return p.promise;
})).then(function(){
sendMetricsToCloudWatch(allMetrics).then(function(resp){
console.log('metrics submitted');
});
});
}
function sendMetricsToCloudWatch(metrics){
var d = when.defer();
sequence().then(function(next){
aws.onConnected(next);
}).then(function(next){
when.all(Object.keys(metrics).map(function(namespace){
var p = when.defer();
aws.cloudWatch.putMetricData(namespace, metrics[namespace]).then(function(){
return p.resolve();
});
// console.log('ns:' + config.namespace);
// console.log(metrics[namespace]);
// p.resolve();
return p.promise;
})).then(next);
}).then(function(next){
d.resolve();
});
return d.promise;
}