Skip to content

Commit

Permalink
update wechat alert message format
Browse files Browse the repository at this point in the history
  • Loading branch information
zgfh committed Jul 11, 2019
1 parent a5da510 commit 5782d1f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
37 changes: 22 additions & 15 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
@time: 2019/7/4 4:56 PM
"""


from flask import Flask, request, Response
import requests
import arrow
import logging
import json
import os

LOG = logging.getLogger(__name__)
app = Flask(__name__)

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

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

title = "{namespace}{resource_name}: {alertname}".format(
resource = "{namespace}{resource_name}".format(
namespace="{}/".format(labels.get("namespace")) if labels.get("namespace") else '',
resource_name=labels.get("pod", labels.get("pod_name", labels.get("instance", 'cluster'))),
alertname=labels.get("alertname", ' '))
resource_name=labels.get("pod", labels.get("pod_name", labels.get("instance", 'cluster'))))

message = annotations.get("message", annotations.get("description", ''))

msg += '''
<font color="{_status_color}">{_status}</font>: [{_title}]({alert_namager_url}) \n
action = annotations.get("Action", '')
runbook_url = annotations.get("runbook_url", '')
action_msg = ""
if action:
if runbook_url:
action_msg = ">处理建议: {}[more]({}) ".format(action, runbook_url)
else:
action_msg = '''>处理建议: <font color="comment">{}</font> '''.format(action)
msg = '''
<font color="{_status_color}">{_status}</font>: [{_title}]({_alert_namager_url})
>级别: <font color="comment">{_severity}</font>
>资源: <font color="comment">{_resource}</font>
>描述: <font color="comment">{_message}</font>
>开始时间: <font color="comment">{startsAt}</font>
>结束时间: <font color="comment">{endsAt}</font>
{_action_msg}
\n
'''.format(_title=title, _status_color=status_color, _status=status, _message=message,
startsAt=alert.get('startsAt', ' '), endsAt=alert.get('endsAt', ' '),
alert_namager_url=ALEAT_MANAGER_URL if ALEAT_MANAGER_URL else alert.get('generatorURL', ' '))
'''.format(_title=labels.get("alertname", ' '), _resource=resource, _status_color=status_color, _status=status,
_message=message, _action_msg=action_msg, _severity=annotations.get("Severity", ' '),
_alert_namager_url=ALEAT_MANAGER_URL if ALEAT_MANAGER_URL else alert.get('generatorURL', ' '))

result = send_wechat_msg(receiver, msg)
result = send_wechat_msg(receiver, msg)

return get_result(error=result)

Expand Down
Binary file added docs/image/alert_demo_pc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 9 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ docker run -d --name=work_wechat_robot -p 8080:8080 daozzg/work_wechat_robot
"http_config":
"bearer_token": "<your robot key>"
```
1. 建议配置Prometheus 启动参数 `--web.external-url`,这样点击告警名能够跳转到 prometheus查看告警到监控信息(如果是prometheus-operator,增加spec.externalUrl)
### 规范
为了更好的显示告警信息,请参考[告警规范 Kubernetes Alert Runbooks](https://github.com/kubernetes-monitoring/kubernetes-mixin/blob/master/runbook.md#kubernetes-alert-runbooks):
如果按照规范,会接收到如下的告警信息
![alert_dmeo](./docs/image/alert_demo_pc.png)



### 开发
```
Expand All @@ -55,4 +62,5 @@ source venv/bin/activate
pip install -r requirements.txt
python app.py
```
```
更新依赖
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
arrow==0.14.2
certifi==2019.6.16
chardet==3.0.4
Click==7.0
Expand All @@ -6,6 +7,8 @@ idna==2.8
itsdangerous==1.1.0
Jinja2==2.10.1
MarkupSafe==1.1.1
python-dateutil==2.8.0
requests==2.22.0
six==1.12.0
urllib3==1.25.3
Werkzeug==0.15.4

0 comments on commit 5782d1f

Please sign in to comment.