-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
94 lines (85 loc) · 3.13 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
/**
* Created by vlad on 01.06.17.
*/
var moment = require('moment');
var TgFancy = require('tgfancy');
var TELEGRAM_TOKEN = process.env.WATCHMEN_TELEGRAM_TOKEN || null;
var CHAT_ID = process.env.WATCHMEN_CHAT_ID || null;
var ENABLE_LATENCY_WARNING = process.env.WATCHMEN_TELEGRAM_LATENCY_WARNING || true;
const bot = new TgFancy(TELEGRAM_TOKEN);
var eventHandlers = {
/**
* On a new outage
* @param {Object} service
* @param {Object} outage
* @param {Object} outage.error check error
* @param {number} outage.timestamp outage timestamp
*/
onNewOutage: function (service, outage) {
var errorMsg = service.name + ' down!. Error: ' + JSON.stringify(outage.error);
_telegramSend(errorMsg);
},
/**
* Failed ping on an existing outage
* @param {Object} service
* @param {Object} outage
* @param {Object} outage.error check error
* @param {number} outage.timestamp outage timestamp
*/
onCurrentOutage: function (service, outage) {
var errorMsg = service.name + ' is still down!. Error: ' + JSON.stringify(outage.error);
_telegramSend(errorMsg);
},
/**
* Failed check (it will be an outage or not according to service.failuresToBeOutage
* @param {Object} service
* @param {Object} data
* @param {Object} data.error check error
* @param {number} data.currentFailureCount number of consecutive check failures
*/
onFailedCheck: function (service, data) {
var errorMsg = service.name + ' check failed!. Error: ' + JSON.stringify(data.error);
_telegramSend(errorMsg);
},
/**
* Warning alert
* @param {Object} service
* @param {Object} data
* @param {number} data.elapsedTime (ms)
*/
onLatencyWarning: function (service, data) {
var msg = service.name + ' latency warning. Took: ' + (data.elapsedTime + ' ms.');
_telegramSend(msg);
},
/**
* Service is back online
* @param {Object} service
* @param {Object} lastOutage
* @param {Object} lastOutage.error
* @param {number} lastOutage.timestamp (ms)
*/
onServiceBack: function (service, lastOutage) {
var duration = moment.duration(moment().unix() - lastOutage.timestamp, 'seconds');
var msg = service.name + ' is back. Down for ' + duration.humanize();
_telegramSend(msg);
}
};
/**
* Send message using Telegram
* @param {string} msg Message to send
* @private
*/
function _telegramSend(msg) {
if (!TELEGRAM_TOKEN) return console.log("Please set the environment variable WATCHMEN_TELEGRAM_TOKEN");
if (!CHAT_ID) return console.log("Please set the environment variable WATCHMEN_CHAT_ID");
bot.sendMessage(CHAT_ID, msg);
}
function TelegramPlugin(watchmen) {
watchmen.on('new-outage', eventHandlers.onNewOutage);
watchmen.on('current-outage', eventHandlers.onCurrentOutage);
watchmen.on('service-error', eventHandlers.onFailedCheck);
if (ENABLE_LATENCY_WARNING)
watchmen.on('latency-warning', eventHandlers.onLatencyWarning);
watchmen.on('service-back', eventHandlers.onServiceBack);
}
exports = module.exports = TelegramPlugin;