-
Notifications
You must be signed in to change notification settings - Fork 8
/
WhatStatus.js
317 lines (279 loc) · 9.9 KB
/
WhatStatus.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**
* WhatStatus.info is a simple status page for torrent site.
* @author dewey
* https://github.com/dewey/WhatStatus
*/
var express = require('express'),
http = require('http'),
net = require('net'),
path = require('path'),
net = require('net'),
redis = require('redis'),
cronJob = require('cron').CronJob,
date = require('date-format-lite'),
request = require('request');
var app = express();
var db = redis.createClient();
// Catch connection errors if redis-server isn't running
db.on("error", function(err) {
console.log(err.toString());
console.log(" Make sure redis-server is started and listening for connections.");
});
app.configure(function() {
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon('images/favicon.ico'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function() {
app.use(express.errorHandler());
app.locals.pretty = true;
app.use(express.logger('dev'));
});
// If there's an outtage reset uptime record counter.
function reset_uptime(component) {
db.set('uptime:' + component, 0);
}
// Render the index page
app.get('/', function(req, res) {
res.render('index', {
title: 'WhatStatus',
logo_url: 'images/logos/logo.png'
});
})
// Render the Stats page
app.get('/stats', function(req, res) {
res.render('stats', {
title: 'WhatStatus'
});
})
// Render the About page
app.get('/about', function(req, res) {
res.render('about', {
title: 'WhatStatus'
});
})
// Render the FAQ page
app.get('/faq', function(req, res) {
res.render('faq', {
title: 'WhatStatus'
});
})
// JSON Response for uptime values
app.get('/api/uptime', function(req, res) {
db.get('uptime:site', function(err, uptimeSite) {
db.get('uptime:irc', function(err, uptimeIrc) {
db.get('uptime:tracker', function(err, uptimeTracker) {
res.json({
site: uptimeSite,
irc: uptimeIrc,
tracker: uptimeTracker
})
});
});
});
})
// JSON Response for uptime records
app.get('/api/records', function(req, res) {
db.get('uptimerecord:site', function(err, uptimeRecordSite) {
db.get('uptimerecord:irc', function(err, uptimeRecordIrc) {
db.get('uptimerecord:tracker', function(err, uptimeRecordTracker) {
res.json({
site: uptimeRecordSite,
irc: uptimeRecordIrc,
tracker: uptimeRecordTracker
})
});
});
});
})
// JSON Response for current component status
app.get('/api/status', function(req, res) {
db.get('site-status', function(err, siteStatus) {
db.get('irc-status', function(err, ircStatus) {
db.get('tracker-status', function(err, trackerStatus) {
res.json({
site: siteStatus,
irc: ircStatus,
tracker: trackerStatus
})
});
});
});
})
// JSON Response for tracker uptime with time stamps
app.get('/api/uptime/tracker', function(req, res) {
db.lrange('trackeruptime', 0, -1, function(err, uptimesTrackerHistory) {
var jsonObj = {};
for (var i = 0; i < uptimesTrackerHistory.length; i++) {
var tokens = uptimesTrackerHistory[i].split(':')
jsonObj[tokens[0]] = tokens[1]
}
res.json(jsonObj)
})
})
// JSON Response for tracker uptime with time stamps [array]
app.get('/api/2/uptime/tracker', function(req, res) {
db.lrange('trackeruptime', 0, -1, function(err, uptimesTrackerHistory) {
var jsonArray = [];
for (var i = 0; i < uptimesTrackerHistory.length; i++) {
var tokens = uptimesTrackerHistory[i].split(':')
jsonArray.push({
timestamp: tokens[0],
status: tokens[1]
});
}
res.json(jsonArray)
})
})
// Check all components every minute
var site_status_counter = 0
var tracker_status_counter = 0
var irc_status_counter = 0
// Check Site Components (Cronjob running every minute)
new cronJob('*/1 * * * *', function() {
// Get Site Status
request('https://what.cd', function(error, response) {
if (!error && response.statusCode == 200) {
db.set('site-status', '1')
site_status_counter = 0;
} else {
site_status_counter++;
console.log("[Check-Site] Status counter: " + site_status_counter);
if (site_status_counter > 2) {
db.set('site-status', '0')
reset_uptime('site');
console.log("[Check-Site] Site down");
}
}
});
// Get Tracker Status
request('http://tracker.what.cd:34000', function(error, response, body) {
if(!error && body.length > 0 && body.indexOf('is down') == -1) {
db.set('tracker-status', '1')
tracker_status_counter = 0;
} else {
tracker_status_counter++;
console.log("[Check-Tracker] Status counter: " + tracker_status_counter);
if (tracker_status_counter > 2) {
db.set('tracker-status', '0')
reset_uptime('tracker');
console.log("[Check-Tracker] Tracker down");
}
}
});
// Get IRC Status
var client = net.connect(6667, 'irc.what-network.net', function() {
db.set('irc-status', '1')
irc_status_counter = 0;
});
// Socket connection closed
client.on('end', function() {
});
// Error on connecting to target host
client.on('error', function() {
irc_status_counter++;
console.log("[Check-IRC] Status counter: " + irc_status_counter);
if (irc_status_counter > 2) {
db.set('irc-status', '0')
reset_uptime('irc');
console.log("[Check-IRC] IRC down");
}
client.end();
});
client.on('timeout', function() {
console.log("[Check-IRC] Timeout");
client.end();
});
}, null, true, "Europe/Vienna");
/*
Statistics (hourly)
This cronjob is incrementing the uptime counters for the various monitored components
and updating the uptime records if the current uptime > the old record.
*/
// Initialize Redis Keys to prevent "null" values
function initializeRedis(component) {
db.exists(component, function(err, reply) {
if (reply != 1) {
db.set(component, 0);
}
});
}
initializeRedis('uptimerecord:site')
initializeRedis('uptimerecord:tracker')
initializeRedis('uptimerecord:irc')
initializeRedis('uptime:site')
initializeRedis('uptime:tracker')
initializeRedis('uptime:irc')
new cronJob('0 * * * *', function() {
console.log("[Stats] Cronjob started")
// Hourly Increment Uptime if Component is Up
db.get('site-status', function(err, siteStatus) {
if (siteStatus != 0) {
db.incr('uptime:site');
}
// Update Site Uptime Record
db.get('uptime:site', function(err, uptimeSite) {
db.get('uptimerecord:site', function(err, uptimerecordSite) {
if (parseInt(uptimeSite) > parseInt(uptimerecordSite)) {
console.log("[Stats-Site] Site Records updated [" + uptimerecordSite + " to " + uptimeSite + "]");
db.set('uptimerecord:site', uptimeSite);
}
});
});
});
// Hourly Increment Uptime if Component is Up
db.get('tracker-status', function(err, trackerStatus) {
if (trackerStatus != 0) {
db.incr('uptime:tracker');
}
// Update Tracker Uptime Record
db.get('uptime:tracker', function(err, uptimeTracker) {
db.get('uptimerecord:tracker', function(err, uptimerecordTracker) {
if (parseInt(uptimeTracker) > parseInt(uptimerecordTracker)) {
console.log("[Stats-Tracker] Tracker Records updated [" + uptimerecordTracker + " to " + uptimeTracker + "]");
db.set('uptimerecord:tracker', uptimeTracker);
}
});
});
/*
Building string for Google Charts used to graph tracker uptime.
String written to redis: DDMMYYYYhhmm|int where int is the current tracker uptime.
*/
db.get('uptime:tracker', function(err, uptimeTracker) {
// Initialize new timestamp
var now = new Date().format('DDMMYYYYhhmm');
db.llen('trackeruptime', function(err, uptimesTrackerCount) {
// Add new timestamp to redis, if there are more than 24 objects pop the oldest value.
if (uptimesTrackerCount <= 24) {
// Pushing Strings to Redis (I'm sorry!)
db.rpush('trackeruptime', now + ":" + uptimeTracker);
} else {
db.lpop('trackeruptime');
}
});
});
});
// Hourly Increment Uptime if Component is Up
db.get('irc-status', function(err, ircStatus) {
if (ircStatus != 0) {
db.incr('uptime:irc');
}
// Update IRC Uptime Record
db.get('uptime:irc', function(err, uptimeIrc) {
db.get('uptimerecord:irc', function(err, uptimerecordIrc) {
if (parseInt(uptimeIrc) > parseInt(uptimerecordIrc)) {
console.log("[Stats-Irc] Irc Records updated [" + uptimerecordIrc + " to " + uptimeIrc + "]")
db.set('uptimerecord:irc', uptimeIrc);
}
});
});
});
}, null, true, "Europe/Vienna");
http.createServer(app).listen(app.get('port'), function() {
console.log("WhatStatus server listening on port: " + app.get('port'));
});