自定义zabbix钉钉报警脚本

前言

公司换服务器了,ppc64le架构,不是常用的X86架构了,原先的报警插件不能用了,所以我用python2编写了报警脚本,解决了报警脚本不兼容的问题。

先看一下效果

在这里插入图片描述

参数
  1. webhook=xxx; 钉钉机器人地址
  2. text=xxx; 消息内容
  3. title=xxx;钉钉消息title,就是钉钉弹出消息时的标题
  4. isAtAll=false|true; 是否at所有人
  5. 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)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值