fr2w 发表于 2015-1-28 09:04:36

zabbix 实现短信告警

    之前一直调用飞信接口发送告警信息,最近购买了第三方短信接口。所以准备使用接口发送告警。
      短信接口是基于https的摘要认证。https认证还是自己做的,调用接口的时候还需要load证书。感觉超级难用,不管那么多,先让它跑起来再说。废话不多说,先上代码。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/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":

       创建一个用户,将该"media"和用户进行关联:
   
      创建一个"action",指定用户进行关联。当该“action”触发,发送告警。
         zabbix已经配置好了,接下来可以将一台设备某一项监控项宕掉。观察是否会触发trigger,发送告警短信。
       由于时间比较紧,脚本写的比较简单,后期还需要完善。


itwzz 发表于 2016-10-11 13:49:58

thanks share
页: [1]
查看完整版本: zabbix 实现短信告警