Python呼叫第三方介面實現nagios簡訊報警
1 2 3 4 5 6 7 8 9 10 11 |
# 'notify-host-by-smsbao' command definition define command {
command_name notify-host-by-smsbao
command_line /usr/local/nagios/libexec/smsbao .py "主機報警 IP:$HOSTADDRESS$:$HOSTSTATE$ 資訊:$HOSTOUTPUT$"
}
# 'notify-service-by-smsbao' command definition
define command {
command_name notify-service-by-smsbao
command_line /usr/local/nagios/libexec/smsbao .py "服務報警 主機:$HOSTNAME$服務:$SERVICEDESC$ IP地址:$HOSTADDRESS$ 狀態:$SERVICESTATE$ 資訊:$SERVICEOUTPUT$"
}
|
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 |
#!/usr/bin/python #coding:utf8
#exp #python smsbao.py "傳送一個測試監控資訊"
import requests,sys,time
import sys
import time
#coding is Error so
default_encoding = 'utf-8'
if sys.getdefaultencoding() ! = default_encoding:
reload (sys)
sys.setdefaultencoding(default_encoding)
#get localtime 2014-7-11 10:01:01
ltime = time.strftime( '%Y-%m-%d %H:%M:%S' ,time.localtime(time.time()))
#u:username p:password c:content m:Phone
Content = str (sys.argv[ 1 ])
payload = {
'u' : 'zwhset' ,
'p' : 'password_md5' ,
'm' : 'you phone' ,
'c' :Content
}
#send msmbao message to phone
r = requests.get( 'http://www.smsbao.com/sms' ,params = payload)
date = ltime + "\t" + Content + "\t" + r.text + "\n"
f = open (
|