forked from statusdashboard/statusdashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook_plugin.js
More file actions
78 lines (65 loc) · 2.3 KB
/
Copy pathwebhook_plugin.js
File metadata and controls
78 lines (65 loc) · 2.3 KB
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
/**
* Webhook plugin : Post status remote HTTP service
* @author Christophe Hamerling - christophe.hamerling@ow2.org
*/
var logger = require('util');
exports.create = function(api, settings) {
if (settings.plugins && settings.plugins.webhook && settings.plugins.webhook.enable) {
var request = require('request');
console.log('Creating the plugin: ' + __filename);
// add route for test purposes
api.emit("postRouteContribution", { method: 'POST', path: '/api/webhook/test', binding: module.exports.myRouteContribution });
var lastCount = 0;
var lastService = {};
var post = function(body) {
request.post({
url: settings.plugins.webhook.url,
method: 'post',
body: body,
json: true
},
function(error, response, body) {
if (error) {
logger.log('Failed to send http message : ' + error);
}
});
};
api.on('up', function(service) {
checkChanges(service);
});
api.on('down', function(service) {
checkChanges(service);
});
api.on('unknown', function(service) {
checkChanges(service);
});
api.on('critical', function(service) {
checkChanges(service);
});
checkChanges = function(service) {
if (!lastService[service.name]) {
lastService[service.name] = {};
lastService[service.name].status = 'up';
}
if (lastService[service.name].status != service.status) {
if (service.status == 'up') {
post({ service : service.name, status : service.status.toUpperCase()});
} else {
post({ service : service.name, status : service.status.toUpperCase(), code : service.statusCode, message : service.message});
}
}
lastService[service.name].status = service.status;
};
api.on('refresh', function(status) {
var count = (status.summarize.critical + status.summarize.down + status.summarize.unknown).toString();
if (lastCount != count) {
post({ summary : { up : status.summarize.up, critical : status.summarize.critical, down : status.summarize.down, unknown : status.summarize.unknown}});
lastCount = count;
}
});
}
};
module.exports.myRouteContribution = function(req, res) {
console.log('Got a webhook call : ', req.body);
res.send(200);
};