Skip to content

Commit 3e9f2a1

Browse files
committed
Add a Feishu notification example based on JSON webhook.
Signed-off-by: 孙林耀 <[email protected]>
1 parent 9d897b6 commit 3e9f2a1

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed

examples/feishu/README.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
### Alert
2+
```json
3+
[
4+
{
5+
"labels": {
6+
"alertname": "Test Alert Temiydom wgatpelzn eiesvt.",
7+
"env": "Test",
8+
"group": "Ops",
9+
"name": "Teft dwqokj yqka jbroocrw xwz.",
10+
"severity": "critical"
11+
},
12+
"annotations": {
13+
"message": "Zxoiyg mnukiqqa imgxyh gwahxoe rixwdzgfn.\"\nLcxdl dpcmu gcrwhvyb qbipfn ifipq omumebq whwmupxag rnbryqneyu gih mszuxf.Tfwbmtj dxuwskbctr ostv kpmxujo tlydykp."
14+
},
15+
"startsAt": "2023-05-04T03:45:54.173Z",
16+
"endsAt": "2023-05-04T04:45:54.173Z",
17+
"generatorURL": "https://www.github.com"
18+
}
19+
]
20+
```
21+
### Template
22+
```gotemplate
23+
{{- define "feishu.title" -}}
24+
{{- if eq .Status "firing" -}}
25+
[Firing {{- .Alerts.Firing | len -}}/{{- .Alerts | len -}}]
26+
{{- else if eq .Status "resolved" -}}
27+
[Resolved {{- .Alerts.Resolved | len -}}]
28+
{{- else -}}
29+
[{{- .Status | toUpper | safeJson -}}]
30+
{{- end -}}
31+
{{" "}}{{- range .GroupLabels.SortedPairs -}} {{- if eq .Name "alertname" -}}{{- .Value | safeJson -}}{{- end -}} {{- end -}}
32+
{{- end -}}
33+
34+
{{- define "feishu.msg" -}}
35+
### {{ template "feishu.title" . -}}\n
36+
{{- range $idx,$elem := .Alerts.Firing -}}
37+
{{- range .Labels.SortedPairs -}}
38+
**{{- .Name | safeJson -}}**:{{" "}}{{- .Value | safeJson -}}\n
39+
{{- end -}}
40+
{{- range .Annotations.SortedPairs -}}
41+
**{{- .Name | safeJson -}}**:{{" "}}{{- .Value | safeJson -}}\n
42+
{{- end -}}
43+
**Srart Time**:{{" "}}{{- .StartsAt -}}\n
44+
{{- if eq .Status "firing" -}}
45+
[View data]({{.GeneratorURL | safeJson}})
46+
{{- end -}}
47+
{{- end -}}
48+
{{- end -}}
49+
{{- define "feishu.json" -}}
50+
{
51+
"msg_type": "interactive",
52+
"card": {
53+
"elements": [
54+
{
55+
"content": "{{- template "feishu.msg" . -}}",
56+
"tag": "markdown"
57+
}
58+
],
59+
"config": {
60+
"wide_screen_mode": true
61+
}
62+
}
63+
}
64+
{{- end -}}
65+
66+
```
67+
### Alertmanager Config
68+
```yaml
69+
70+
receivers:
71+
- name: alert_test
72+
webhook_configs:
73+
- url: 'https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxxxxx'
74+
json: '{{ template "feishu.json" . }}'
75+
send_resolved: true
76+
```
77+
78+
### Webhook Content
79+
```
80+
{
81+
"msg_type": "interactive",
82+
"card": {
83+
"elements": [
84+
{
85+
"content": "### [Firing1/1] Test Alert Temiydom wgatpelzn eiesvt.\n**alertname**: Test Alert Temiydom wgatpelzn eiesvt.\n**env**: Test\n**group**: Ops\n**name**: Teft dwqokj yqka jbroocrw xwz.\n**severity**: critical\n**message**: Zxoiyg mnukiqqa imgxyh gwahxoe rixwdzgfn.\"\nLcxdl dpcmu gcrwhvyb qbipfn ifipq omumebq whwmupxag rnbryqneyu gih mszuxf.Tfwbmtj dxuwskbctr ostv kpmxujo tlydykp.\n**Srart Time**: 2023-05-04 03:45:54.173 +0000 UTC\n[View data](https://www.github.com)",
86+
"tag": "markdown"
87+
}
88+
],
89+
"config": {
90+
"wide_screen_mode": true
91+
}
92+
}
93+
}
94+
```
95+
96+
### Feishu message
97+
![image](./feishu_message.png)

examples/feishu/alertmanager.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
global:
2+
resolve_timeout: 5m
3+
route:
4+
group_by: ['alertname']
5+
group_wait: 10s
6+
group_interval: 10s
7+
repeat_interval: 24h
8+
9+
receiver: 'alert_test'
10+
11+
receivers:
12+
- name: alert_test
13+
webhook_configs:
14+
- url: 'https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxxxxxxxxxxxxx'
15+
json: '{{ template "feishu.json" . }}'
16+
send_resolved: true
17+
18+
inhibit_rules:
19+
- source_match:
20+
severity: 'critical'
21+
target_match:
22+
severity: 'warning'
23+
equal: ['alertname', 'dev', 'instance']
24+
25+
templates:
26+
- "json.tmpl"

examples/feishu/feishu_message.png

67.2 KB
Loading

examples/feishu/json.tmpl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{{- define "feishu.title" -}}
2+
{{- if eq .Status "firing" -}}
3+
[Firing {{- .Alerts.Firing | len -}}/{{- .Alerts | len -}}]
4+
{{- else if eq .Status "resolved" -}}
5+
[Resolved {{- .Alerts.Resolved | len -}}]
6+
{{- else -}}
7+
[{{- .Status | toUpper | safeJson -}}]
8+
{{- end -}}
9+
{{" "}}{{- range .GroupLabels.SortedPairs -}} {{- if eq .Name "alertname" -}}{{- .Value | safeJson -}}{{- end -}} {{- end -}}
10+
{{- end -}}
11+
12+
{{- define "feishu.msg" -}}
13+
### {{ template "feishu.title" . -}}\n
14+
{{- range $idx,$elem := .Alerts.Firing -}}
15+
{{- range .Labels.SortedPairs -}}
16+
**{{- .Name | safeJson -}}**:{{" "}}{{- .Value | safeJson -}}\n
17+
{{- end -}}
18+
{{- range .Annotations.SortedPairs -}}
19+
**{{- .Name | safeJson -}}**:{{" "}}{{- .Value | safeJson -}}\n
20+
{{- end -}}
21+
**Srart Time**:{{" "}}{{- .StartsAt -}}\n
22+
{{- if eq .Status "firing" -}}
23+
[View data]({{.GeneratorURL | safeJson}})
24+
{{- end -}}
25+
{{- end -}}
26+
{{- end -}}
27+
{{- define "feishu.json" -}}
28+
{
29+
"msg_type": "interactive",
30+
"card": {
31+
"elements": [
32+
{
33+
"content": "{{- template "feishu.msg" . -}}",
34+
"tag": "markdown"
35+
}
36+
],
37+
"config": {
38+
"wide_screen_mode": true
39+
}
40+
}
41+
}
42+
{{- end -}}

0 commit comments

Comments
 (0)