前言
公司换服务器了,ppc64le架构,不是常用的X86架构了,原先的报警插件不能用了,所以我用python2编写了报警脚本,解决了报警脚本不兼容的问题。
先看一下效果
参数
- webhook=xxx; 钉钉机器人地址
- text=xxx; 消息内容
- title=xxx;钉钉消息title,就是钉钉弹出消息时的标题
- isAtAll=false|true; 是否at所有人
- phone_list=xxx; 手机号,以英文逗号分开,shell命令行传参都是字符串类型,在脚本中以逗号为分隔符切割成list类型。
源码
#!/bin/python2
# coding:utf8
import requests
import json
import sys
print(len(sys.argv))
webhook = 'https://oapi.dingtalk.com/robot/send?access_token=xxx'
def dingding(title="zabbix报警", text="报警默认信息", webhook=webhook, phone_list=None, isAtAll=True):
if type(phone_list) is str:
phone_list = phone_list.split(",")
text = "@" + "@".join(phone_list) + "\n\n" + text
elif phone_list is not None and len(phone_list) > 0:
for i,j in enumerate(phone_list):
phone_list[i] = str(j)
text = "@" + "@".join(phone_list) + "\n\n" + text
headers = {'Content-Type': 'application/json'}
Words = {
"msgtype": "markdown",
"markdown": {
"title": "{:}".format(title),
"text": "{:} \n\n".format(text),
},
"at": {
"atMobiles":
phone_list
,
"isAtAll": isAtAll
}
}
req = requests.post(url=webhook, headers=headers, data=json.dumps(Words))
if __name__ == "__main__":
arg_dict = {}
for arg in sys.argv[1:]:
tmp_list = arg.split("=", 1)
arg_dict[tmp_list[0]] = tmp_list[1]
del tmp_list
print(arg_dict)
if len(arg_dict) != 0:
dingding(**arg_dict)