#!/usr/local/python/bin/python3.5
importsys, os, shutilimportos.pathimportdatetimeimporthttp.cookiejarimporturllib.requestimporturllib.errorimporturllib.parseimportsmtplibfrom email.mime.text importMIMETextfrom email.header importHeaderfrom email.mime.multipart importMIMEMultipartfrom email.mime.image importMIMEImageclassZabbixGraph(object):def __init__(self, url, name, password):
self.url=url
self.name=name
self.password=password
cookiejar=http.cookiejar.CookieJar()
urlOpener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookiejar))
values= {"name":self.name,'password':self.password,'autologin':1,"enter":'Sign in'}
data= urllib.parse.urlencode(values).encode(encoding='UTF8')
request=urllib.request.Request(url, data)try:
urlOpener.open(request, timeout=10)
self.urlOpener=urlOpenerexcepturllib.error.HTTPError as e:print(e)defgetgraph(self, url, values, image_dir):
key=values.keys()if 'graphid' not inkey:#print('请确认是否输入graphid')
sys.exit(1)if 'period' not inkey:
values['period'] = 86400
if 'stime' not inkey:
values['stime'] = datetime.datetime.now().strftime('%Y%m%d%H%M%S')if 'width' not inkey:
values["width"] = 800
if 'height' not inkey:
values["height"] = 200data= urllib.parse.urlencode(values).encode(encoding='UTF8')
request=urllib.request.Request(url, data)
url=self.urlOpener.open(request)
image=url.read()
imagename= "%s/%s.png" % (image_dir, values["graphid"])
f= open(imagename, 'wb')
f.write(image)defyesterday():
now_time=datetime.datetime.now()
yes_time= now_time + datetime.timedelta(days=-1)
yes_time_nyr= yes_time.strftime('%Y%m%d')
yes_time_nyr1= yes_time.strftime('%Y-%m-%d')returnyes_time_nyr, yes_time_nyr1defemail():
sender= '发送者'receiver= ['接收者1', '接收者2', '接收者3']
subject= '每日重点监控对象'smtpserver= 'smtp.126.com'username= '发送者邮箱'password= 'smtp密码'msgRoot= MIMEMultipart('related')
text= yesterday1+'重点监控数据报告已生成。\n请访问:http://xxx/'+yesterday+'/index.html'msg= MIMEText(text, 'plain', 'utf-8') #中文需参数‘utf-8',单字节字符不需要
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'Robot'msg['To'] = '接收者别名'smtp=smtplib.SMTP()
smtp.connect('smtp.126.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()defhtml():
all_the_text= '''
body{
text-align: center;
}
.middle{
text-align: center;
}
.hide{
display: none;
}
.show{
display: block;
}
每日数据报告''' + yesterday1 + '''监控数据报告
xxx
xxx
xxx
xxx
$('#sel').change(function(){
var cid = $(this).val();
$('#'+cid).show();
$('#'+cid).siblings().hide();
});
'''with open(image_dir+ '/index.html', 'w') as f:
f.write(all_the_text)
src= '/usr/monitor/day/jquery-3.1.0.min.js'dst= '/usr/monitor/day/' + yesterday + '/jquery-3.1.0.min.js'shutil.copyfile(src, dst)if __name__=='__main__':
yesterday, yesterday1=yesterday()
gr_url="http://xxx/zabbix/chart2.php"indexURL="http://xxx/zabbix/index.php"username= 'xxx'password= 'xxx'os.mkdir('/usr/monitor/day/%s' %yesterday)
image_dir='/usr/monitor/day/'+yesterday
values1={"graphid":"1148","period":86400,"stime":yesterday+'000000',"width":800,"height":200}
values4={"graphid":"1145","period":86400,"stime":yesterday+'000000',"width":800,"height":200}
values5 = {"graphid": "1079", "period": 86400, "stime": yesterday + '000000', "width": 800, "height": 200}
values5_1 = {"graphid": "792", "period": 86400, "stime": yesterday + '000000', "width": 800, "height": 200}
b=ZabbixGraph(indexURL,username,password)for i in(values1, values4, values5, values5_1):
b.getgraph(gr_url, i, image_dir)
html()
email()