python
cat /etc/zabbix/zabbix_server.conf | grep "alert"
ll /usr/lib/zabbix/alertscripts
钉钉机器人接入文档: 自定义机器人接入 - 钉钉开放平台
钉钉告警文件放在/usr/lib/zabbix/alertscripts/dingding2.py中
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import requests
import time
import hmac
import hashlib
import base64
import urllib.parse
import sys
filename = sys.argv[1].strip()
filename2 = sys.argv[2].strip()
# 将三个参数组合在一起,并过滤掉空行
Alarm_content = "\n".join([line for line in [filename, filename2,] if line])
timestamp = str(round(time.time() * 1000))
secret = 'Sxxxxxxxxxxxxxx0'
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))
url=f"https://oapi.dingtalk.com/robot/send?access_token=ev×tamp={timestamp}&sign={sign}"
data = {'msgtype': 'text',
'text': { 'content': Alarm_content,}}
req =requests.post(url, json=data)
print(req)
print(req.text)
zabbix设置: