之前一直调用飞信接口发送告警信息,最近购买了第三方短信接口。所以准备使用接口发送告警。
短信接口是基于https的摘要认证。https认证还是自己做的,调用接口的时候还需要load证书。感觉超级难用,不管那么多,先让它跑起来再说。废话不多说,先上代码。
#!/usr/bin/env python #coding:utf-8 import requests from requests.auth import HTTPDigestAuth import json import argparse def sendSms(mobile, content, name): url = 'https://api.david.com/sms/messages' chal = { 'algorithm': 'MD5', 'cnonce': '015b38c9119bbda5', 'content-type': 'application/json', 'nc': '00000001', 'nonce': '1389832695:d3c620a9e645420228c5c7da7d228f8c', 'qop': 'auth', 'realm': 'api.david.com', 'response': '07f0d429dffed172f4751ecadd02e68e', 'uri': '/sms/messages', 'username': '+lw4CNmllLo='} try: data = {"mobile_numbers":[{"mobile":mobile, "name":name}],"content":content} except: data = {"mobile_numbers":[{"mobile":"xxxxxxxxxxx", "name":name}],"content":content} #使用requests post数据给服务器端 #因为https证书是第三方自己造的,所以需要导入他们给的pem证书。 r = requests.post(url, auth=HTTPDigestAuth('+lw4CNmllLo=', 'qmc7pMiSrQXo7Q+lx/6c0A=='), verify='/etc/zabbix/alertscripts/david.com.pem', headers=chal, data=json.dumps(data)) return r.text if __name__ == "__main__": #mobile 接收信息的电话号码 #name 标题 #content 发送内容 parser = argparse.ArgumentParser(description='Send sms to user for zabbix alerting') parser.add_argument('mobile',action="store", help='The address of the sms that send to user ') parser.add_argument('name',action="store", help='The subject of the sms') parser.add_argument('content',action="store", help='The content of the sms') args = parser.parse_args() mobile=args.mobile name=args.name content=args.content logger = mobile+' '+name+' '+content sendSms(mobile, content, name) #注意: #1. 脚本所有者为zabbix:zabbix #2. 脚本需要有可执行权限
发送sms告警的脚本已经完成。接下来配置zabbix,使其可以使用该脚本发送短信。
首先需要创建一个告警"media":
创建一个"action",指定用户进行关联。当该“action”触发,发送告警。
zabbix已经配置好了,接下来可以将一台设备某一项监控项宕掉。观察是否会触发trigger,发送告警短信。
由于时间比较紧,脚本写的比较简单,后期还需要完善。