zabbixでPythonメールアラートを実現するスクリプト

コード下記:
#! /usr/bin/python
# -*- coding: utf-8 -*-
import sys,os,re,smtplib,mimetypes,pycurl,json,urllib,StringIO,time,traceback
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
leng=len(sys.argv)
def log4py(logpath,logtxt):
f=open(logpath,’a’)
logtime=time.strftime('%Y%m%d%H%M%S’,time.localtime())
f.writelines(logtime+":")
f.writelines(logtxt)
f.writelines('\n’)
f.close()
def printmlinestr(mlinestr):
d=mlinestr.splitlines()
res=""
for i in d:
i="
“+i+""
res+=i+’\n’
return res

#getvalue 正規表現で相応itemIDを取得
def getvalue(string,rexp):
r=re.compile(rexp,re.M)
#r=re.compile(“^ITEMID:[0-9]*",re.M)
log4py(logpath,’method getvalue’)
log4py(logpath,’string’)
log4py(logpath,string)
#for i in string:
d=r.findall(string)
log4py (logpath,’rexp:’)
log4py (logpath,rexp)
str1=d[0]
log4py(logpath, 'rexresult:’)
log4py(logpath,str1)
print str1
# arrstr=str.split(“\xa3\xba")
arrstr=str1.split(“:")
result=arrstr[1]
log4py(logpath,’result:’)
log4py(logpath,result)
log4py(logpath,’end method getvalue’)
return result

#getpic  アラート画像を取得してファイルに書き込む
def getpic(itemid):
log4py(logpath,’method getpic’)
# try:
c = pycurl.Curl()
pathroot=’/etc/zabbix/alert/’
#path=’c:/alert.png’
#head=’Content-Type:application/json’
global stime
endtime=time.strftime('%Y%m%d%H%M%S’,time.localtime())
tmptime=int(endtime)-10000
stime = repr(tmptime)
post_data_dic={“itemid":itemid,"period":"3600″,"stime":stime}
picpath=(pathroot)+(stime)+(itemid)+’.png’
print 'picpath:’+picpath
str=file(picpath,’wb’)
#zabbixアドレスを変更
c.setopt(pycurl.URL, “http://10.1.1.34/zabbix/chart.php")
c.setopt(pycurl.WRITEFUNCTION, str.write)
c.setopt(pycurl.FOLLOWLOCATION, 1)
#c.setopt(pycurl.HEADER, True)
c.setopt(c.POSTFIELDS,urllib.urlencode(post_data_dic))
c.perform()
log4py(logpath,’end method getpic’)
return picpath
# except Exception,e:
# f.writelines('expforgetpic:\n’)
# f.writelines(e)
def SendMail(receivers,subject,data,picpath,itemid):
msg = MIMEMultipart()
msg['From’] = “zbadmin@arkgame.com"
#msg['To’] = “receive@arkgame.com"
receives=receivers
msg['To’] = receivers
msg['Subject’] = subject
body=printmlinestr(data)
file1 = picpath
image = MIMEImage(open(file1,’rb’).read())
image.add_header('Content-ID’,")
msg.attach(image)
#print txt
#print 'txt end’
#print type (txt)
#zabbixアドレスを修正
url="
#msg.attach(txt)
html = “""

“""
html+=body
html+="
"
html+="

URLLINK

"

html+="""
“""
htm = MIMEText(html,’html’,’UTF-8′)
msg.attach(htm)
server = smtplib.SMTP()
server.connect('mail.arkgame.com’)
#server.sendmail(msg['From’],msg['To’],msg.as_string())
server.sendmail(msg['From’],receives,msg.as_string())
server.quit()
#if __name__ == “__main__":

 

#def main
#パラメータ1-受信アドレス、パタメータ2-メール件名 パラメータ3–アラート内容
#ログパスを修正
global logpath
logpath=’/etc/zabbix/alert/ts.log’
log4py(logpath,"————————–startx")
rece=sys.argv[1]
subject=sys.argv[2]
data=sys.argv[3]
#picpath=’c:/alert.png’
#picpath=’/etc/zabbix/alert/alert.png’

#異常処理
#itemid=getvalue(data,"^ITEMID\xa3\xba[0-9]*")
#itemid=getvalue(data,"^ITEMID:[0-9]*)
#f.writelines(data)
try:
itemid=getvalue(data,"ITEMID:[0-9]*")
picpath= getpic(itemid)
SendMail(rece,subject,data,picpath,itemid)
os.remove(picpath)
except Exception,e:
log4py(logpath,e)
#os.system('echo $3|mail -s $2 $1 ')
shellcmd=’echo '+data+’|mail -s '+subject+’ '+rece
print shellcmd
os.system(shellcmd)
log4py(logpath,"————————–end")

Linux

Posted by arkgame