coding=utf8
Author:dongml
import sys, json,socket
import urllib2
import urllib
解决中文转码
reload(sys)
sys.setdefaultencoding( “utf-8” )
构建告警信息
def messages(msg):
hostname = socket.gethostname()
values = {
“app”: ‘xxx’, #appkey 对应的应用
“eventType”: ‘trigger’,
“eventId”: 9000,
“priority”: “2”,
“alarmName”: “docker-swarm监控:” + hostname,
“alarmContent”: “主机:”+hostname+”发生告警:”+msg
}
msges = urllib.urlencode(values)
return msges
发送告警信息
def send_message(data):
send_url = ‘http://api.onealert.com/alert/api/event/’
respone = urllib2.urlopen(urllib2.Request(url=send_url, data=data)).read()
result = json.loads(respone.decode())[‘result’]
if result == ‘failed’:
print (‘failed’)
else:
print (‘Succesfully’)
调用接口
msg = “test”
data = messages(msg)
send_message(data)