-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
141 lines (126 loc) · 4.46 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Geckoboard plugin
*
* Send up/down status (monitoring widget) and response time (line chart and
* highcharts widget) to Geckoboard
*
* Installation
* ------------
* This plugin is uninstalled by default. To install and enable it, git clone
* the plugin repo and add its entry
* to the `plugins` folder under uptime
*
* $ git clone [email protected]:waltzofpearls/uptime-plugin-geckoboard.git geckoboard
*
* to the `plugins` key of the configuration:
*
* // in config/production.yaml
* plugins:
* - ./plugins/geckoboard
*
* Configuration
* -------------
* Here is an example configuration:
*
* // in config/production.yaml
* geckoboard:
* apiKey: [geckoboard api key]
* check:
* [Uptime check name]:
* - url: [geckoboard push url]
* key: [geckoboard widget key]
* widget: monitoring # monitoring, linechart or highcharts
* - url: [geckoboard push url]
* key: [geckoboard widget key]
* widget: linechart
* color: '#00ff00'
*/
var fs = require('fs');
var ejs = require('ejs');
var http = require('http');
var https = require('https');
var moment = require('moment');
var Ping = require('../../models/ping');
exports.initWebApp = function(options) {
var config = options.config.geckoboard;
var templateDir = __dirname + '/views/';
Ping.on('afterInsert', function(ping) {
ping.findCheck(function(err, check) {
if (err) {
return console.error(err);
}
// from: 60 mins ago
var begin = moment().subtract(60, 'minutes').valueOf();
// to: now
var end = moment().valueOf();
check.getStatsForPeriod('hour', begin, end, function(err, stats) {
if (err) {
return console.error(err);
}
var confCheck = [];
if (config.check[check.name]) {
confCheck = config.check[check.name];
} else if (config.check[check._id]) {
confCheck = config.check[check._id];
} else {
return;
}
confCheck.forEach(function(widget, index) {
var matches = widget.url.match(/(http|https):\/\/([^\/]+)(\/.*)?/);
if (matches === null) {
return;
}
var filename = templateDir + widget.widget + '.ejs';
var renderOptions = {
apiKey: config.apiKey,
widget: widget,
check: check,
ping: ping,
stats: stats,
numTick: stats.length,
moment: moment
};
var postData = ejs.render(fs.readFileSync(filename, 'utf8'), renderOptions);
var httpOptions = {
host: matches[2],
path: matches[3] || '/',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': postData.length
}
};
var protocol = matches[1];
sendHttpRequest(check.name, protocol, httpOptions, postData);
});
});
});
});
console.log(moment().format() + ' - Enabled Geckoboard data push');
}
function sendHttpRequest(name, protocol, options, data) {
var req = (
protocol == 'https' ? https.request : http.request
)(options, function(res) {
if (res.statusCode == 200) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log(moment().format() +
' - Geckoboard plugin response data [' + name + ']: ' +
chunk);
});
} else {
console.error(moment().format() +
' - Geckoboard plugin response code: ' + res.statusCode);
console.error(moment().format() +
' - Geckoboard plugin response headers: ' +
JSON.stringify(res.headers));
}
});
req.on('error', function(_err) {
console.error(moment().format() +
' - Geckoboard plugin response error: ' + _err.message);
});
req.write(data);
req.end();
}