Skip to content

Commit 5782d1f

Browse files
author
zzg
committed
update wechat alert message format
1 parent a5da510 commit 5782d1f

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed

app.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
@time: 2019/7/4 4:56 PM
99
"""
1010

11-
1211
from flask import Flask, request, Response
1312
import requests
13+
import arrow
1414
import logging
1515
import json
1616
import os
1717

1818
LOG = logging.getLogger(__name__)
1919
app = Flask(__name__)
2020

21-
URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={}"
21+
WECAHT_API = os.getenv('WECAHT_API', "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={}")
2222
KEY = os.getenv('ROBOT_KEY', '')
2323
ALEAT_MANAGER_URL = os.getenv('ALEAT_MANAGER_URL', '')
2424

@@ -34,7 +34,7 @@ def send_wechat_msg(key, message):
3434
key = KEY
3535
if not isinstance(message, str):
3636
data = message
37-
respose = requests.post(URL.format(key), json=data)
37+
respose = requests.post(WECAHT_API.format(key), json=data)
3838
LOG.info("send message result: %s:%s", respose.status_code, respose.text)
3939
if respose.status_code == 200:
4040
return True
@@ -58,24 +58,31 @@ def prometheus_webhook():
5858
labels = alert.get('labels', {})
5959
annotations = alert.get('annotations', {})
6060

61-
title = "{namespace}{resource_name}: {alertname}".format(
61+
resource = "{namespace}{resource_name}".format(
6262
namespace="{}/".format(labels.get("namespace")) if labels.get("namespace") else '',
63-
resource_name=labels.get("pod", labels.get("pod_name", labels.get("instance", 'cluster'))),
64-
alertname=labels.get("alertname", ' '))
63+
resource_name=labels.get("pod", labels.get("pod_name", labels.get("instance", 'cluster'))))
6564

6665
message = annotations.get("message", annotations.get("description", ''))
67-
68-
msg += '''
69-
<font color="{_status_color}">{_status}</font>: [{_title}]({alert_namager_url}) \n
66+
action = annotations.get("Action", '')
67+
runbook_url = annotations.get("runbook_url", '')
68+
action_msg = ""
69+
if action:
70+
if runbook_url:
71+
action_msg = ">处理建议: {}[more]({}) ".format(action, runbook_url)
72+
else:
73+
action_msg = '''>处理建议: <font color="comment">{}</font> '''.format(action)
74+
msg = '''
75+
<font color="{_status_color}">{_status}</font>: [{_title}]({_alert_namager_url})
76+
>级别: <font color="comment">{_severity}</font>
77+
>资源: <font color="comment">{_resource}</font>
7078
>描述: <font color="comment">{_message}</font>
71-
>开始时间: <font color="comment">{startsAt}</font>
72-
>结束时间: <font color="comment">{endsAt}</font>
79+
{_action_msg}
7380
\n
74-
'''.format(_title=title, _status_color=status_color, _status=status, _message=message,
75-
startsAt=alert.get('startsAt', ' '), endsAt=alert.get('endsAt', ' '),
76-
alert_namager_url=ALEAT_MANAGER_URL if ALEAT_MANAGER_URL else alert.get('generatorURL', ' '))
81+
'''.format(_title=labels.get("alertname", ' '), _resource=resource, _status_color=status_color, _status=status,
82+
_message=message, _action_msg=action_msg, _severity=annotations.get("Severity", ' '),
83+
_alert_namager_url=ALEAT_MANAGER_URL if ALEAT_MANAGER_URL else alert.get('generatorURL', ' '))
7784

78-
result = send_wechat_msg(receiver, msg)
85+
result = send_wechat_msg(receiver, msg)
7986

8087
return get_result(error=result)
8188

docs/image/alert_demo_pc.png

76.3 KB
Loading

readme.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ docker run -d --name=work_wechat_robot -p 8080:8080 daozzg/work_wechat_robot
4242
"http_config":
4343
"bearer_token": "<your robot key>"
4444
```
45+
1. 建议配置Prometheus 启动参数 `--web.external-url`,这样点击告警名能够跳转到 prometheus查看告警到监控信息(如果是prometheus-operator,增加spec.externalUrl)
46+
### 规范
47+
为了更好的显示告警信息,请参考[告警规范 Kubernetes Alert Runbooks](https://github.com/kubernetes-monitoring/kubernetes-mixin/blob/master/runbook.md#kubernetes-alert-runbooks):
48+
如果按照规范,会接收到如下的告警信息
49+
![alert_dmeo](./docs/image/alert_demo_pc.png)
50+
51+
4552

4653
### 开发
4754
```
@@ -55,4 +62,5 @@ source venv/bin/activate
5562
pip install -r requirements.txt
5663
python app.py
5764
58-
```
65+
```
66+
更新依赖

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
arrow==0.14.2
12
certifi==2019.6.16
23
chardet==3.0.4
34
Click==7.0
@@ -6,6 +7,8 @@ idna==2.8
67
itsdangerous==1.1.0
78
Jinja2==2.10.1
89
MarkupSafe==1.1.1
10+
python-dateutil==2.8.0
911
requests==2.22.0
12+
six==1.12.0
1013
urllib3==1.25.3
1114
Werkzeug==0.15.4

0 commit comments

Comments
 (0)