#!/usr/bin/python # -*- coding: utf-8 -*- import urllib2 import json import os import sys import time reload(sys) sys.setdefaultencoding('utf8') #准备日志文件 if not os.path.exists("/opt/matrix/agent/bin/alert_cmd"): os.makedirs("/opt/matrix/agent/bin/alert_cmd") f = open("/opt/matrix/agent/bin/alert_cmd/send_sms.log", "a+") #日志输出 def log(s): dt=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) print(dt+" "+s+"\n") f.write(dt+" "+s+"\n") try: # date_time=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) tmp_date_time=time.strftime('%Y%m%d%H%M%S',time.localtime(time.time())) tmp_date=time.strftime('%Y-%m-%d',time.localtime(time.time())) tmp_time=time.strftime('%H:%M:%S',time.localtime(time.time())) #接收参数 log("=============================================================================") data1 = sys.argv[1] log("data1=" + data1) data2 = sys.argv[2] log("data2=" + data2) #参数解析 #hjson1= json.loads(data1.decode('utf-8')) hjson1= json.loads(data1) ipaddress=hjson1['netgate']['address'] port=hjson1['netgate']['port'] protocol=hjson1['netgate']['protocol'] brNo=hjson1['netgate']['brNo'] chanNo=hjson1['netgate']['chanNo'] srvId=hjson1['netgate']['srvId'] svcId=hjson1['netgate']['svcId'] #log(hjson1['jdbc']['driver']) #hjson2= json.loads(data2.decode('utf-8')) hjson2= json.loads(data2) #f.write(date_time+" "+hjson1+" "+hjson2+"\n") #处理 for msg in hjson2: message=msg['msg'] for names in msg['to']: touser=names['mobile'] #url=protocol+'://'+ipaddress+':'+port+'/' url=protocol+'://'+str(ipaddress)+':'+str(port)+'/' body='' \ '' \ '' \ ''+tmp_date+'' \ ''+tmp_time+'' \ ''+svcId+'' \ '' \ '' \ ''+srvId+'' \ ''+chanNo+'' \ ''+brNo+'' \ ''+touser+'' \ ''+message+'' \ ''+tmp_date_time+'' \ 'null' \ 'null' \ '' \ '' #data = urllib.urlencode(body) log("发送请求:"+date_time+" "+url+" "+body) response = urllib2.urlopen(urllib2.Request(url, body), timeout=3).read() log("请求返回:"+date_time+" "+response) #r = requests.post(url,data=body.decode("gbk").encode("utf-8")) #r = requests.post(url,data=body.encode("utf-8")) #f.write(date_time+" "+url+" "+body+"\n") #f.write(date_time+" "+touser+" "+message+r.text+"\n") #f.write(date_time+" "+touser+" "+message+"\n") #print(hjson1) #print(hjson2) #send wechart except Exception as e: log("{}".format(e)) #关闭日志文件 f.close()