此webbook仅供参考

# url: 换成你自己的sms api

# params: 你的api的POST数据

# phone_l: 接收短信的手机号

 

 #!/usr/bin/env python
# -*- coding: utf-8 -*-


from flask import Flask, request
import urllib
import urllib2
import json
import sys
app = Flask(__name__)

reload(sys)
sys.setdefaultencoding('utf8')

phone_l = [130XXXXXXXX]


def send_sms(phonenum, content):
    contents = json.dumps({"name": content})
    print contents
    params =  {'templateId': 'SMS_ID', 'signName': 'XX', 'phoneNums': phonenum, 'content': contents}
    url = "http://smsapi_url"
    params = urllib.urlencode(params)
    newurl = url + "?" + params
    req = urllib2.Request(newurl)
    result = urllib2.urlopen(req)
    res = result.read()
    return res


@app.route('/send', methods=['POST'])
def send():
    data_dic = json.loads(request.data)
    try:
        alerts_l = data_dic['alerts']
        for alert in alerts_l:
            instance = alert.get('labels').get('instance')
            startsAt = alert.get('startsAt').split('.')[0]
            description = alert.get('annotations').get('description')
            value = alert.get('annotations').get('value')
            content = "器%s发生了%s\n报警时间: %s\n报警阈值: %s\n" % (instance, description, startsAt, value)
  
            for phone in phone_l: 
                send_sms(phone, content)

    except Exception as e:
        print(e)
    return 'ok'
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐