-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwebhook.js
64 lines (57 loc) · 1.74 KB
/
webhook.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
/*
* This script is maintained at https://github.com/puzzle/prometheus-rocket-chat/blob/master/webhook.js
* See https://rocket.chat/docs/administrator-guides/integrations/ for a how-to.
*/
class Script {
process_incoming_request({ request }) {
console.log(request.content);
// Return a rocket.chat message object.
// If channel is undefined, the default channel from the webhook configuration is used
return {
content: {
username: "Prometheus Alert",
attachments: this.getAlerts(request.content),
channel: request.content.alerts[0].labels.rocketchat_channel
}
};
}
getAlerts(content) {
let alertColor = this.getAlertColor(content.status);
let attachments = [];
for (i = 0; i < content.alerts.length; i++) {
let alert = content.alerts[i];
attachments.push({
color: alertColor,
title_link: content.externalURL,
title: this.getAlertTitle(alert, content.status),
text: alert.annotations.description
});
}
return attachments;
}
getAlertColor(status) {
if (status === "resolved") {
return "good";
} else if (status === "firing") {
return "danger";
} else {
return "warning";
}
}
getAlertTitle(alert, status) {
let title = "[" + this.getAlertStatus(alert, status).toUpperCase() + "] ";
if (!!alert.annotations.summary) {
title += alert.annotations.summary;
} else if (!!alert.labels.alertname) {
title += alert.labels.alertname + ": " + alert.labels.instance;
}
return title;
}
getAlertStatus(alert, status) {
if (status === "firing" && !!alert.annotations.severity) {
return alert.annotations.severity;
} else {
return String(status);
}
}
}