forked from prometheus-msteams/prometheus-msteams
-
Notifications
You must be signed in to change notification settings - Fork 0
/
card-grafana-inspired.tmpl
306 lines (276 loc) · 12.2 KB
/
card-grafana-inspired.tmpl
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
{{/*
# Template with Grouping
This card template tries to emulate a Grafana like card layout while also
improving it in various aspects. The main characteristic is that the values
of all alerts included in a single alert group are listed as facts.
To leverage this card layout multiple requirements must be fullfilled. In
addition a fallback is included that will be used if the annotation
`template` has not the value `group`.
`escape_underscores` in prometheus-msteams must be activated.
## "group" Card
Multiple requirements. "Common" annotations / labels have the same value for
the whole alert group. This also means you cannot use templating in them.
Grouping must be done at least by `alertname` label.
**Mandatory common annotations:**
* `template`: Must be `group`.
* `title`: Very short human readable title. For example "Memory: Memory Limit
Hits" or "Server Errors Occurred".
* `summary`: Formula-like explaination of the alert. For example "Ratio
(Working Set Memory / Reservation) [12h] > 90 %".
* `description`: Long description, description of impact, what to do, etc. For
example "Working set memory of listed servouices has been lower than 10 %
of ..."
**Optional common annotations:**
* `investigate_link`: Not necessary. If set a button will be visible. Set it
to a valid link. Something like a Grafana dashboard is good. You can use
templating, but remember that the annotation must stay common.
* `generator_url_override`: If set, the link behind the summary text will point
to the given link instead of the already given generator URL. Usually not
necessary.
**Mandatory specific annotations:**
* `key`: Identifier for a specific alert. For example container name. Use
templating to concatenate several labels just like in Grafana. For example
"{{ $labels.name }} | {{ $labels.namespace }}".
* `value`: Should map to `$value`. Use templating to format it. For example
"{{ $value | printf "%.2f" }} %".
**Common Labels:**
* `severity`
**Specific Labels:**
* You can have specific labels, but they won't be visible in the notififcation.
## Fallback Card
More or less the same as the default template included with prometheus-ms. The
`investigate_link` annotation can be specific to every alert. Fallback will be
used if common `template` annotation is not `group`.
*/}}
{{ define "teams.card" }}
{{/* Should 'http:' in the generator url be replaced with 'https:'? */}}
{{ $should_enforce_https := true }}
{{/* Should certain common labels be filtered? */}}
{{ $should_filter_labels := true }}
{{/* Should certain common annotations be filtered? */}}
{{ $should_filter_annotations := true }}
{{/* Should the potential actions field be included? */}}
{{ $should_have_potential_actions := true }}
{{/* For example 'https://www.google.com/alertkarma/?'. Ignored if empty. */}}
{{ $alertkarma_prefix := ""}}
{
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"themeColor": "
{{- if eq .Status "resolved" -}}
2DC72D
{{- else if eq .Status "firing" -}}
{{- if eq .CommonLabels.severity "critical" -}}
8C1A1A
{{- else if eq .CommonLabels.severity "error" -}}
8C1A1A
{{- else if eq .CommonLabels.severity "warning" -}}
FFA500
{{- else if eq .CommonLabels.severity "info" -}}
00BFFF
{{- else -}}
808080
{{- end -}}
{{- else -}}
808080
{{- end -}}",
{{/*====================================================================*/}}
{{ if eq .CommonAnnotations.template "group" }}
"title": "{{ .CommonAnnotations.title }} [{{ .Status | title }}, {{ .CommonLabels.severity | title }}] x {{ len .Alerts }}",
"summary": "{{ .CommonAnnotations.title }} [{{ .Status | title }}, {{ .CommonLabels.severity | title }}] x {{ len .Alerts }}",
"sections": [
{{/* FIRST SECTION: Common annotations and labels */}}
{
"markdown": true,
{{ if gt (len .CommonAnnotations.generator_url_override) 0}}
"activityTitle": "[{{ .CommonAnnotations.summary }}]({{ .CommonAnnotations.enerator_url_override }})",
{{ else if $should_enforce_https }}
"activityTitle": "[{{ .CommonAnnotations.summary }}]({{ (index .Alerts 0).GeneratorURL | replace "http:" "https:" }})",
{{ else }}
"activityTitle": "[{{ .CommonAnnotations.summary }}]({{ (index .Alerts 0).GeneratorURL }})",
{{ end }}
"text": "{{ .CommonAnnotations.description }}",
"facts": [
{{/* Common Annotations (filtered) */}}
{{ $foundannotation := false }}
{{ $first := true }}
{{ range $key, $value := .CommonAnnotations }}
{{ if and $should_filter_annotations (ne $key "title") (ne $key "summary") (ne $key "description") (ne $key "key") (ne $key "value") (ne $key "generator_url_override") (ne $key "investigate_link") (ne $key "template") }}
{{ if $first }}
{{ $first = false }}
{{ $foundannotation = true }}
{{ else }}
,
{{ end }}
{
"name": "{{ $key }}",
"value": "{{ $value }}"
}
{{ end }}
{{ end }}
{{ if $foundannotation }},{{ end }}
{{/* Common Labels (filtered) */}}
{{$first := true}}
{{ range $key, $value := .CommonLabels }}
{{ if and $should_filter_labels (ne $key "alert_with_resolve") (ne $key "severity") }}
{{ if $first }}{{ $first = false }}{{ else }},{{ end }}
{
"name": "{{ $key }}",
"value": "{{ $value }}"
}
{{ end }}
{{ end }}
]
},
{{/* SECOND SECTION: key and value specific annotations */}}
{
"markdown": true,
"facts": [
{{ $first := true }}
{{ range $index, $alert := .Alerts }}
{{ if gt (add (len $alert.Annotations.key) (len $alert.Annotations.value)) 0 }}
{{/* Only proceed if key-value found */}}
{{ if $first }}{{ $first = false }}{{ else }},{{ end }}
{
"name": "{{ $alert.Annotations.value }}",
"value": "{{ $alert.Annotations.key }}"
}
{{ end }}
{{ end }}
]
}
]
{{ if $should_have_potential_actions }}
,
"potentialAction": [
{
"@type": "OpenUri",
"name": "View in Alertmanager",
"targets": [
{
"os": "default",
"uri": "{{ .ExternalURL }}/#?alerts/silenced=true&inhibited=true&active=true&filter=%7B{{$c := counter}}{{ range $key, $value := .CommonLabels }}{{if call $c}}%22%2C%20{{ end }}{{ $key }}%3D%22{{ $value }}{{- end }}%22%7D"
}
]
}
{{ if gt (len $alertkarma_prefix) 0 }}
,
{
"@type": "OpenUri",
"name": "View in Alertkarma",
"targets": [
{
"os": "default",
"uri": "{{ $alertkarma_prefix }}q=alertname%3D{{ .CommonLabels.alertname }}"
}
]
}
{{ end }}
{{ if gt (len .CommonAnnotations.investigate_link) 0 }}
,
{
"@type": "OpenUri",
"name": "Investigate",
"targets": [
{
"os": "default",
"uri": "{{- .CommonAnnotations.investigate_link | trim -}}"
}
]
}
{{ end }}
]
{{ end }}
{{/*====================================================================*/}}
{{ else }}
"summary": "
{{- if gt (len .CommonAnnotations.title) 0 -}}
{{- .CommonAnnotations.title }} [{{ .Status | title }}{{ if gt (len .CommonLabels.severity) 0 }}, {{ .CommonLabels.severity | title }}{{ end }}] x {{ len .Alerts -}}
{{- else if gt (len .CommonAnnotations.summary) 0 -}}
{{- .CommonAnnotations.summary -}} [{{ .Status | title }}{{ if gt (len .CommonLabels.severity) 0 }}, {{ .CommonLabels.severity | title }}{{ end }}] x {{ len .Alerts -}}
{{- else if gt (len .CommonAnnotations.alertname) 0 -}}
{{- .CommonAnnotations.alertname -}} [{{ .Status | title }}{{ if gt (len .CommonLabels.severity) 0 }}, {{ .CommonLabels.severity | title }}{{ end }}] x {{ len .Alerts -}}
{{ else }}
Alert [{{ .Status | title }}{{ if gt (len .CommonLabels.severity) 0 }}, {{ .CommonLabels.severity | title }}{{ end }}] x {{ len .Alerts -}}
{{- end -}}"
,
"title": "
{{- if gt (len .CommonAnnotations.title) 0 -}}
{{- .CommonAnnotations.title }} [{{ .Status | title }}{{ if gt (len .CommonLabels.severity) 0 }}, {{ .CommonLabels.severity | title }}{{ end }}] x {{ len .Alerts -}}
{{- else if gt (len .CommonAnnotations.summary) 0 -}}
{{- .CommonAnnotations.summary -}} [{{ .Status | title }}{{ if gt (len .CommonLabels.severity) 0 }}, {{ .CommonLabels.severity | title }}{{ end }}] x {{ len .Alerts -}}
{{- else if gt (len .CommonAnnotations.alertname) 0 -}}
{{- .CommonAnnotations.alertname -}} [{{ .Status | title }}{{ if gt (len .CommonLabels.severity) 0 }}, {{ .CommonLabels.severity | title }}{{ end }}] x {{ len .Alerts -}}
{{ else }}
Alert [{{ .Status | title }}{{ if gt (len .CommonLabels.severity) 0 }}, {{ .CommonLabels.severity | title }}{{ end }}] x {{ len .Alerts -}}
{{- end -}}"
,
"sections": [
{{$externalUrl := .ExternalURL}}
{{- range $index, $alert := .Alerts }}{{- if $index }},{{- end }}
{
"markdown": true,
"activityTitle": "[{{ $alert.Annotations.description }}]({{ $externalUrl }})",
"facts": [
{{- range $key, $value := $alert.Annotations }}
{
{{- if and (ne $key "description") (ne $key "template") (ne $key "title") (ne $key "summary") -}}
"name": "{{ $key }}",
"value": "{{ $value }}"
{{- end -}}
},
{{- end -}}
{{$c := counter}}{{ range $key, $value := $alert.Labels }}{{ if call $c }},{{ end }}
{
"name": "{{ $key }}",
"value": "{{ $value }}"
}
{{- end }}
]
{{ if $should_have_potential_actions }}
,
"potentialAction": [
{
"@type": "OpenUri",
"name": "View in Alertmanager",
"targets": [
{
"os": "default",
"uri": "{{ $externalUrl }}/#?alerts/silenced=true&inhibited=true&active=true&filter=%7B{{$c := counter}}{{ range $key, $value := $alert.Labels }}{{if call $c}}%22%2C%20{{ end }}{{ $key }}%3D%22{{ $value }}{{- end }}%22%7D"
}
]
}
{{ if gt (len $alertkarma_prefix) 0 }}
,
{
"@type": "OpenUri",
"name": "View in Alertkarma",
"targets": [
{
"os": "default",
"uri": "{{ $alertkarma_prefix }}q=alertname%3D{{ $alert.Labels.alertname }}"
}
]
}
{{ end }}
{{ if gt (len $alert.Annotations.investigate_link) 0 }}
,
{
"@type": "OpenUri",
"name": "Investigate",
"targets": [
{
"os": "default",
"uri": "{{- $alert.Annotations.investigate_link | trim -}}"
}
]
}
{{ end }}
]
{{ end }}
}
{{- end }}
]
{{ end }}
}
{{ end }}