-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
131 lines (125 loc) · 4.47 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
/**
* Slack plugin
*
* Notifies all events (up, down, paused, restarted) by slack message
*
* 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 git@github.com:waltzofpearls/uptime-plugin-slack.git slack
*
* to the `plugins` key of the configuration:
*
* // in config/production.yaml
* plugins:
* - ./plugins/slack
*
* Usage
* -----
* This plugin sends an message to slack each time a check is started, goes down,
* or goes back up. When the check goes down, the message contains the error details:
*
* Object: [Down] Check "FooBar" just went down
* On Thursday, September 4th 1986 8:30 PM,
* a test on URL "http://foobar.com" failed with the following error:
*
* Error 500
*
* Uptime won't send anymore messages about this check until it goes back up.
*
* Configuration
* -------------
* Here is an example configuration:
*
* // in config/production.yaml
* slack:
* webhook: [slack webhook url]
* channel: '[slack channel name]' # default '#general'
* username: [slack username] # default 'Uptime Alert'
* icon_emoji: [emoji icon for [username]] # default ':turtle:'
* icon_url: [optional, icon_url for [username]] # default empty, icon_url overrides icon_emoji
* event:
* up: true
* down: true
* paused: false
* restarted: false
*/
var fs = require('fs');
var ejs = require('ejs');
var http = require('http');
var https = require('https');
var moment = require('moment');
var CheckEvent = require('../../models/checkEvent');
exports.initWebApp = function(options) {
var config = options.config.slack;
var templateDir = __dirname + '/views/';
var matches = config.webhook.match(/(http|https):\/\/([^\/]+)(\/.*)?/);
if (matches === null) {
return console.error('Problem parsing Slack Webhook URL: ' + config.webhook);
}
var httpOpts = {
host: matches[2],
path: matches[3] || '/',
method: 'POST'
};
CheckEvent.on('afterInsert', function(checkEvent) {
if (!config.event[checkEvent.message]) {
return;
}
checkEvent.findCheck(function(err, check) {
if (err) {
return console.error(err);
}
var filename = templateDir + checkEvent.message + '.ejs';
var renderOptions = {
check: check,
checkEvent: checkEvent,
url: options.config.url,
moment: moment,
filename: filename
};
var lines = ejs.render(fs.readFileSync(filename, 'utf8'), renderOptions).split('\n');
var postdata = {
channel: config.channel || '#general',
username: config.username || 'Uptime Alert',
icon_emoji: config.icon_emoji || ':turtle:',
text: lines.join('\n'),
link_names: '1'
}
if (config.icon_url) {
delete postdata.icon_emoji;
postdata.icon_url = config.icon_url;
}
var req = (
matches[1] == 'https' ? https.request : http.request
)(httpOpts, function(res) {
if (res.statusCode == 200) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log(moment().format() +
' - Slack plugin response data: ' + chunk);
console.log(moment().format() +
' - Notified event by slack: Check ' + check.name +
' ' + checkEvent.message);
});
} else {
console.error(moment().format() +
' - Slack plugin response code: ' + res.statusCode);
console.error(moment().format() +
' - Slack plugin response headers: ' +
JSON.stringify(res.headers));
}
});
req.on('error', function(_err) {
console.error(moment().format() +
' - Slack plugin response error: ' + _err.message);
});
req.write(JSON.stringify(postdata));
req.end();
});
});
console.log(moment().format() + ' - Enabled Slack notifications');
}