Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

钉某人失败,钉所有人成功 #122

Open
dxg0716 opened this issue Dec 11, 2020 · 2 comments
Open

钉某人失败,钉所有人成功 #122

dxg0716 opened this issue Dec 11, 2020 · 2 comments

Comments

@dxg0716
Copy link

dxg0716 commented Dec 11, 2020

作者好,我是部署在k8s里面的现在钉某人不成功,钉所有人的时候是正常的,请问是什么问题呢,我使用的是1.4.0 @timonwong

配置如下
webhook_mention_users:
url: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx
secret: SECxxxxxxx
mention:
mobiles: ['188xxxx1557']

@fency12
Copy link

fency12 commented May 8, 2021

+1

@howge
Copy link

howge commented Jun 1, 2021

尝试很多了方法,包括在labels里面添加@电话号码,出现诡异的情况,时好时坏,花了点时间彻底解决这个问题,改用python flask + jinja2 + dingtalkchatbot

app.py

from flask import Flask, request
import time
import json
from toolkit import Ding
from jinja2 import Template

with open('./templates/prom.tpl') as file_:
    prom_tpl = Template(file_.read()) 

@app.route('/alerts/<target>/send',methods=['POST'])
def send_alerts(target):
    data = json.loads(request.get_data())
    print(data)
    alertname = data['groupLabels']['alertname']
    title = '{0}业务报警'.format(target.upper())
    now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    resolveds = list(filter(lambda x: x['status'] == 'resolved', data['alerts']))
    alerts = list(filter(lambda x: x['status'] == 'firing', data['alerts']))
    msg = prom_tpl.render(alerts=alerts,resolveds=resolveds,alertname=alertname, now=now)
    #此处try一下,钉钉最大msg size 为20000,为方便测试省略
    resp = Ding.bot.send_markdown(title=title, text=msg, at_mobiles=['xxxxxxxx']) 
    return 'OK',200

prom.tpl(此处是重点,因为jinja2空格问题,经本人测试下面模板可用,)

测试数据

{'receiver': 'proj',
 'status': 'firing',
 'alerts': [{'status': 'firing',
   'labels': {'alertname': 'InstanceDown',
    'app': 'go_core',
    'env': 'prod',
    'instance': 'xxxxxxx',
    'job': 'proj',
    'location': 'sg-01',
    'notify_user': '@xxxxxx',
    'project': 'proj',
    'severity': 'warning'},
   'annotations': {'description': '节点监控服务器Down',
    'summary': '(xxxxxxx) 下线了'},
   'startsAt': '2021-05-31T04:25:26.134Z',
   'endsAt': '0001-01-01T00:00:00Z',
   'generatorURL': 'http://prometheus:9090/graph?g0.expr=up+%3D%3D+0&g0.tab=1',
   'fingerprint': 'e46c7937b3ec44bc'},
  {'status': 'resolved',
   'labels': {'alertname': 'InstanceDown',
    'app': 'go_core',
    'env': 'prod',
    'instance': 'xxxxxxx',
    'job': 'proj',
    'location': 'us-01',
    'notify_user': '@xxxxxxx',
    'project': 'proj',
    'severity': 'warning'},
   'annotations': {'description': '节点监控服务器Down',
    'summary': '(xxxxxx) 下线了'},
   'startsAt': '2021-05-31T04:36:56.134Z',
   'endsAt': '2021-05-31T09:24:26.134Z',
   'generatorURL': 'http://prometheus:9090/graph?g0.expr=up+%3D%3D+0&g0.tab=1',
   'fingerprint': '296ecb19db3dd0d2'},
  {'status': 'resolved',
   'labels': {'alertname': 'InstanceDown',
    'app': 'go_core',
    'env': 'prod',
    'instance': 'xxxxxx',
    'job': 'proj_service',
    'location': 'us-01',
    'notify_user': '@xxxxxx',
    'project': 'proj',
    'severity': 'warning'},
   'annotations': {'description': '节点监控服务器Down',
    'summary': '(xxxxxx) 下线了'},
   'startsAt': '2021-05-31T04:37:06.134Z',
   'endsAt': '2021-05-31T09:24:36.134Z',
   'generatorURL': 'http://prometheus:9090/graph?g0.expr=up+%3D%3D+0&g0.tab=1',
   'fingerprint': '4cbd0374b7727006'}],
 'groupLabels': {'alertname': 'InstanceDown'},
 'commonLabels': {'alertname': 'InstanceDown',
  'app': 'go_core',
  'env': 'prod',
  'notify_user': '@xxxxxxx',
  'project': 'proj',
  'severity': 'warning'},
 'commonAnnotations': {'description': '节点监控服务器Down'},
 'externalURL': 'http://prometheus:9093',
 'version': '4',
 'groupKey': '{}/{project="proj"}:{alertname="InstanceDown"}',
 'truncatedAlerts': 0}

测试方式

In [246]: with open('./templates/prom.tpl') as file_:
     ...:             template = Template(file_.read())
     ...: 
     ...: 
     ...: 

In [250]: alerts=list(filter(lambda x: x['status'] == 'firing',obj['alerts']))

In [251]: resolved=list(filter(lambda x: x['status'] == 'resolved',obj['alerts']))

In [252]: 

In [247]: msg = template.render(alerts=alerts,resolveds=resolved,alertname='InstanceDown',now="12121")

In [248]: Ding.bot.send_markdown(title='asda', text=msg, at_mobiles=['you cellphone'])
Out[248]: {'errcode': 0, 'errmsg': 'ok'}

Uploading 测试.png…
测试2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants