需求:
1、取cacti的图时不需要登陆(只对可访问用户开放),但其它操作时需要登陆。
2、每天早上把报表发送到指定邮件
需求1:
file:/cacti/graph_image.php
//include("./include/auth.php"); include("./include/global.php");
品茶:首先去掉验证模块,发现少了涵数,再去auth.php里看,发现此涵数是在./include/global.php中,载入此文件后问题得到解决。
需求2:首先需要连接邮件,down图,并把图发送到收件中。最后做成一个定时任务。
/code/cacti_report/downimage.py #/usr/bin/python import requests,sys def DownImage(url,filename): r = requests.get(url) try: ImageFile = open(filename,‘w‘) ImageFile.write(r.content) ImageFile.close() return (0,"Success") except: return (1,"Write imagefile Error!")
/code/cacti_report/send_cacti.py #!/usr/bin/env python #coding: utf-8 import smtplib,time from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage from downimage import DownImage ltime = time.strftime(‘%Y-%m-%d %H:%M‘,time.localtime(time.time())) sender = ‘发件人‘ receiver = ‘收件人‘ subject = ltime + "\tNginx状态图,100段网络流量图,200段网络流量图的报表" smtpserver = ‘smtp.163.com‘ username = ‘163帐户‘ password = ‘163密码‘ msgRoot = MIMEMultipart(‘related‘) msgRoot[‘Subject‘] = subject msgRoot[‘To‘] = "收件人" NginxImage = "/code/cacti_report/image/nginx1.png" t100 = "/code/cacti_report/image/t100.png" t200 = "/code/cacti_report/image/t200.png" #download image out = DownImage("http://www.cacti.com/cacti/graph_image.php?action=view&local_graph_id=6",NginxImage) out2 = DownImage("http://www.cacti.com/cacti/graph_image.php?local_graph_id=51",t100) out3 = DownImage("http://www.cacti.com/cacti/graph_image.php?local_graph_id=196",t200) #define email conent msgText = MIMEText(‘<b>报表(昨天 8:00 - 今天 8:00):</b><br>Nginx状态图:<br><img src="cid:webxxxload8"><br> 100段网络流量图:<br><img src="cid:switch100"><br> 200段网络流量图:<br><img src="cid:switch200"><br>‘,‘html‘,‘utf-8‘) #add email conent to msgRoot msgRoot.attach(msgText) #webxxx load 8 nginx static msgImage = MIMEImage(open(NginxImage, ‘rb‘).read()) msgImage.add_header(‘Content-ID‘, ‘<webxxxload8>‘) msgRoot.attach(msgImage) #switch 100 range traffic msgImage = MIMEImage(open(t100, ‘rb‘).read()) msgImage.add_header(‘Content-ID‘, ‘<switch100>‘) msgRoot.attach(msgImage) #switch 10048 range traffic msgImage = MIMEImage(open(t200, ‘rb‘).read()) msgImage.add_header(‘Content-ID‘, ‘<switch200>‘) msgRoot.attach(msgImage) #Send mail smtp = smtplib.SMTP() smtp.connect(smtpserver) smtp.login(username, password) smtp.sendmail(sender, receiver, msgRoot.as_string()) smtp.quit()
定时任务:
# crontab -l 0 8 * * * /code/cacti_report/send_cacti.py
品茶:记得需要一个image文件夹,这样每天早上8点记得收信看报表,结果可能是这样子的。
品茶:是不是觉着特别高大上!
原文地址:http://cwtea.blog.51cto.com/4500217/1533263