-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
126 lines (110 loc) · 4.09 KB
/
main.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
const got = require('got');
const twilio = require('twilio');
const config = require('./config.json');
const twilioClient = twilio(config.twilio.account_sid, config.twilio.auth_token);
const offlineData = {};
let interval = 2 * 60;
if (config.check_interval_in_seconds && config.check_interval_in_seconds > 0) {
interval = Number(config.check_interval_in_seconds);
}
/*
* TODO:
* Check if server that runs this tool has internet conneciton by itself
* If not, don't notify, or maybe notify with different notice (configurable)
*/
const runChecks = () => {
setTimeout(() => {
runChecks();
}, interval * 1000);
config.urls.forEach((url) => {
checkUrl(url);
});
};
const checkUrl = async (url) => {
let online = true;
if (offlineData[url] && offlineData[url].timestamp_last_retry) {
if (new Date().valueOf() < (offlineData[url].timestamp_last_retry + (config.retry_after_offline_interval_in_seconds * 1000))) {
// Skip this check because we know it's offline, and the retry_after_offline_interval_in_seconds hasn't passed yet
return;
}
}
if (offlineData[url] && offlineData[url].timestamp_last_retry) {
offlineData[url].timestamp_last_retry = new Date().valueOf();
}
try {
await got(url, {
timeout: config.url_timeout_ms || 500,
retry: 0
});
} catch (e) {
let error = e.code;
if (e.name === 'HTTPError') {
// If response statusCode does not start with a 5, we continue as valid response
if (String(e.response.statusCode).startsWith('5') === false) {
return;
}
error = e.response.statusCode;
}
online = false;
if (!offlineData[url]) {
offlineData[url] = {
counter: 0
};
}
if (offlineData[url] && offlineData[url].counter === 0) {
offlineData[url].offline_since = new Date();
}
offlineData[url].counter++;
if (offlineData[url].timestamp_last_retry) {
console.log(new Date(), 'site still OFFLINE:', url, 'Error:', error, 'detection #' + offlineData[url].counter, 'offline since:', offlineData[url].offline_since);
} else {
console.log(new Date(), 'site POSSIBLE OFFLINE:', url, 'Error:', error, 'detection #' + offlineData[url].counter);
}
if (offlineData[url].counter >= Number(config.notify_treshold || 5) && offlineData[url].notified !== true) {
offlineData[url].notified = true;
offlineData[url].timestamp_last_retry = new Date().valueOf();
notifySMS('SERVER OFFLINE!\nServer: ' + url + '\nError: ' + error + '\nSince: ' + offlineData[url].offline_since + '\nCurrent time:', new Date().toString());
if (config.call_notification === true) {
notifyCall();
}
console.log('\n\n\n/!\\ Site (' + url + ') reached the offline notify treshold. Current offline state: ' + offlineData[url].counter, '\n\n');
}
} finally {
if (online === true) {
if (offlineData[url] && offlineData[url].counter >= Number(config.notify_treshold || 5)) {
notifySMS('Server back online!\nServer: ' + url + '\nOffline since: ' + offlineData[url].offline_since + '\nCurrent time:', new Date().toString());
console.log(new Date(), 'site ONLINE after ' + offlineData[url].counter + ' offline detections. Site was offline since:', offlineData[url].offline_since);
}
if (offlineData[url]) {
offlineData[url].counter = 0;
offlineData[url].offline_since = 0;
offlineData[url].timestamp_last_retry = 0;
}
}
}
};
const notifySMS = async (message) => {
let response = null;
try {
response = await twilioClient.messages.create({
body: message,
from: config.twilio.from,
to: config.twilio.to
});
} catch (e) {
console.log('Error while sending text:', e);
}
};
const notifyCall = async () => {
let response = null;
try {
response = await twilioClient.calls.create({
twiml: '<Response><Say>Your server seems to be offline, please check your text message for more details</Say></Response>',
from: config.twilio.from,
to: config.twilio.to
});
} catch (e) {
console.log('Error while calling:', e);
}
};
runChecks();