版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
注册钉钉忽略; url:https://www.dingtalk.com/
1、登录windows版本钉钉客户端创建机器人
进入需要发送告警的群组,点击设置
PS:记住加签和webhook
2、查看Zabbix Script存放位置
grep -i AlertScriptsPath /etc/zabbix/zabbix_server.conf
#AlertScriptsPath=/usr/lib/zabbix/alertscripts
3、编写Python Code
import time
import hmac
import hashlib
import base64
import urllib.parse
import requests
import json
import sys
api_url = 'https://oapi.dingtalk.com/robot/send?access_token=' \
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
# 计算签名
def get_timestamp_sign():
"""
official url https://open.dingtalk.com/document/robots/customize-robot-security-settings
:return: timestamp, sign
"""
timestamp = str(round(time.time() * 1000))
secret = 'this is secret'
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
return timestamp, sign
# 获取签名计算后的链接
def get_signed_url():
"""
:拼接url format --> https://oapi.dingtalk.com/robot/send?access_token=XXXXXX×tamp=XXX&sign=XXX
:return: webhook
"""
timestamp, sign = get_timestamp_sign()
webhook = api_url + "×tamp=" + timestamp + "&sign=" + sign
return webhook
# 定义webhook消息模式
def get_webhook(mode):
if mode == 0: # 仅关键字
webhook = api_url
elif mode == 1 or mode == 2: # 关键字+加签 或 关键字+加签+ip
webhook = get_signed_url()
else:
webhook = ""
print("error! mode: ", mode, " webhook : ", webhook)
print(webhook)
return webhook
def get_message(text, user_info):
message = {
"msgtype": "text", # 有text, "markdown"、link、整体跳转ActionCard 、独立跳转ActionCard、FeedCard类型等
"text": {
"content": text # 消息内容
},
"at": {
"atMobiles": [
user_info,
],
"isAtAll": False # 是否是发送群中全体成员
}
}
return message
def send_ding_message(text, user_info):
"""
model: 0 --> 关键字; 1 --> 关键字 +加签; 2 --> 关键字+加签+IP
:param text:
:param user_info:
:return:
"""
webhook = get_webhook(1)
# 构建请求头部
header = {
"Content-Type": "application/json",
"Charset": "UTF-8"
}
message = get_message(text, user_info)
message_json = json.dumps(message)
info = requests.post(url=webhook, data=message_json, headers=header).json()
code = info["errcode"]
errmsg = info["errmsg"]
now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
if code == 0:
print(now_time, ':Message sent successfully, return info:{} {}\n'.format(code, errmsg))
else:
print(now_time + ":Message sending failed, return info:{} {}\n".format(code, errmsg))
exit(3)
if __name__ == "__main__":
t = sys.argv[3]
u = sys.argv[1]
send_ding_message(t, u)
4、赋予脚本执行权限并重启Zabbix Service
chmod +x dingding.py
systemctl restart zabbix-server
5、测试脚本可用性
./dingding.py all subject ‘test zabbix message’ #第二个空格可以直接写"all"这个单词,也可以写电话号码,写电话号码告警内容会显示"@某某“
6、配置Zabbix
6.1、Create Media types
Script parameters
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
6.2、Create Actions —> Trigger actions
Message
当前状态: {TRIGGER.STATUS}
告警主机: {HOST.NAME}
告警 IP: {HOST.IP}
告警时间: {EVENT.DATE}-{EVENT.TIME}
告警等级: {TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目: {TRIGGER.KEY1}
6.3、配置告警通知用户
7、Zabbix告警类型分组
比如Server、Network、DB等系统类型按不同的组进行告警通知,单独通知到指定系统负责人
具体告警分组操作方式如下:
1、在钉钉APP额外创建其它类型的组,再在组里创建机器人
2、在/usr/lib/zabbix/alertscripts目录拷贝修改Python Code,只需要修改
api_url = 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx
secret = ‘xxxxxxxxxxxxxxxxxxxxxxxxxxx’
systemctl restart zabbix-server
3、配置zabbix Media types
4、配置Trigger actions
>最后的显示结果
5、配置用户
完成。这样zabbix告警调用不同的脚本,脚本内分别对应不同的钉钉机器人应用,user Media配置不同的钉钉触发名称,不同的用户在不同的钉钉群,各类告警信息就可以相互隔离,互不干扰了。