-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalert-events.js
190 lines (163 loc) · 8.4 KB
/
alert-events.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const axios = require('axios');
const config = require('./config.json');
const DISCORD_URL_WEBHOOK = config.discord.url;
const SLACK_URL_WEBHOOK = config.slack.url;
const GRAFANA_URL = config.grafana.url;
const ALERT_MANAGER_URL = config.alertmanager.url;
const NOTIFY_COLOR_MESSAGE = 12263456;
const api = axios.create()
function formatStringValue(string_value_text) {
if (string_value_text === '' | typeof string_value_text === 'undefined') {
throw new Error('Content of __value_string__ is empty!!!')
}
const array_key_values = string_value_text.split(" ");
array_key_values.splice(0, 1);
array_key_values.splice(array_key_values.length - 1, 1)
let array_sanitize = array_key_values.filter(value => {
const raw_text = value.replaceAll(",", "").replaceAll("{", "").replaceAll("}", "").replaceAll("'", "");
const splited_text = raw_text.split("=");
if (splited_text[0] === "value") {
return true
}
})
array_sanitize = array_sanitize.map(value => {
const raw_text = value.replaceAll(",", "").replaceAll("{", "").replaceAll("}", "").replaceAll("'", "");
const splited_text = raw_text.split("=");
return `"${splited_text[0]}":"${splited_text[1]}"`
})
const message_test = "{" + array_sanitize.join() + "}";
const parsed_test = JSON.parse(message_test);
return parsed_test;
}
function appendAdditionDescription(value_string, low_condition, high_condition) {
if (typeof value_string !== 'undefined' && typeof low_condition !== 'undefined' && typeof high_condition !== 'undefined') {
const parsed_test = formatStringValue(value_string);
let metric_value = parseFloat(parsed_test.value);
const high_limit = parseFloat(high_condition);
const low_limit = parseFloat(low_condition);
if (metric_value > high_limit) {
return "HIGH"
}
if (metric_value < low_limit) {
return "LOW"
}
return ""
} else {
return ""
}
}
async function sendDiscordMessage(message) {
try {
console.log("timestamp=" + new Date().toISOString(), "level=DEBUG", "labels=input-message,discord", "message=" + JSON.stringify(message))
const { alerts } = message;
if (typeof alerts !== 'undefined') {
alerts.map(alert => {
let embeds = [];
console.log("timestamp=" + new Date().toISOString(), "level=DEBUG", "labels=alert-content,slack", "message:", alert.status, alert.labels.alertname)
const { __value_string__ } = alert.annotations;
const { low_condition, high_condition } = alert.labels;
let aditional_text = appendAdditionDescription(__value_string__, low_condition, high_condition);
let embed;
if (typeof __value_string__ === "undefined") {
embed = {
"title": `(${alert.status.toUpperCase()}) ${alert.labels.alertname.toUpperCase()} ${aditional_text}`,
"description": `${typeof alert.annotations.summary === 'undefined' ? "" : alert.annotations.summary}\n[Silence Alert](${ALERT_MANAGER_URL}/#/alerts)`,
"url": `${GRAFANA_URL}/d/${alert.annotations.__dashboardUid__}?orgId=1&refresh=5s&viewPanel=${alert.annotations.__panelId__}`,
"color": (alert.status == 'firing' ? NOTIFY_COLOR_MESSAGE : 4109717)
}
} else {
let { value } = formatStringValue(__value_string__);
embed = {
"title": `(${alert.status.toUpperCase()}) ${alert.labels.alertname.toUpperCase()} ${aditional_text}`,
"description": `Value: ${value === 'undefined' ? "-" : parseFloat(value).toFixed(2)}\n${typeof alert.annotations.summary === 'undefined' ? "" : alert.annotations.summary}\n[Silence Alert](${ALERT_MANAGER_URL}/#/alerts)`,
"url": `${GRAFANA_URL}/d/${alert.annotations.__dashboardUid__}?orgId=1&refresh=5s&viewPanel=${alert.annotations.__panelId__}`,
"color": (alert.status == 'firing' ? NOTIFY_COLOR_MESSAGE : 4109717)
}
}
const json = JSON.stringify({
"username": "Grafana Alert",
"embeds": [embed]
})
console.log("Send notify change Info to Discord:", json)
try {
api.post(DISCORD_URL_WEBHOOK, json, {
headers: {
'Content-Type': 'application/json'
}
})
} catch (err) {
console.log("timestamp=" + new Date().toISOString(), "level=ERROR", "message=" + err.message);
}
});
}
} catch (err) {
console.log("timestamp=" + new Date().toISOString(), err.message, "error=" + err);
}
}
async function sendSlackMessage(message) {
try {
console.log("timestamp=" + new Date().toISOString(), "level=DEBUG", "labels=input-message,slack", "message=" + JSON.stringify(message))
const { alerts } = message;
let attachments = [];
if (typeof alerts !== 'undefined') {
alerts.map(alert => {
console.log("timestamp=" + new Date().toISOString(), "level=DEBUG", "labels=alert-content,slack", "message:", alert.status, alert.labels.alertname)
const { __value_string__ } = alert.annotations;
const { low_condition, high_condition } = alert.labels;
let aditional_text = appendAdditionDescription(__value_string__, low_condition, high_condition);
if (typeof __value_string__ === "undefined") {
attachments.push({
"title": `(${alert.status.toUpperCase()}) ${alert.labels.alertname.toUpperCase()} ${aditional_text}`,
"text": `${typeof alert.annotations.summary === 'undefined' ? "" : alert.annotations.summary}`,
"title_link": `${GRAFANA_URL}/d/${alert.annotations.__dashboardUid__}?orgId=1&refresh=5s&viewPanel=${alert.annotations.__panelId__}`,
"color": (alert.status == 'firing' ? '#FA2C23' : "#2DE64F")
})
}else{
let { value } = formatStringValue(__value_string__);
attachments.push({
"title": `(${alert.status.toUpperCase()}) ${alert.labels.alertname.toUpperCase()} ${aditional_text}`,
"text": `Value: ${value === 'undefined' ? "-" : parseFloat(value).toFixed(2)}\n${typeof alert.annotations.summary === 'undefined' ? "" : alert.annotations.summary}`,
"title_link": `${GRAFANA_URL}/d/${alert.annotations.__dashboardUid__}?orgId=1&refresh=5s&viewPanel=${alert.annotations.__panelId__}`,
"color": (alert.status == 'firing' ? '#FA2C23' : "#2DE64F")
})
}
})
const json = JSON.stringify({
"attachments": attachments,
"blocks": [
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Silence Alert",
"emoji": true
},
"value": "click_me_123",
"url": `${ALERT_MANAGER_URL}/#/alerts`
}
]
}
]
})
console.log("Send notify change Info to Slack:", json)
try {
api.post(SLACK_URL_WEBHOOK, json, {
headers: {
'Content-Type': 'application/json'
}
})
} catch (err) {
console.log("timestamp=" + new Date().toISOString(), "level=ERROR", "message=" + err.message);
}
}
} catch (err) {
console.log("timestamp=" + new Date().toISOString(), "level=ERROR", err.message);
}
}
module.exports = {
sendDiscordMessage,
sendSlackMessage
}