需求:
1、取cacti的图时不需要登陆(只对可访问用户开放),但其它操作时需要登陆。
2、每天早上把报表发送到指定邮件
需求1:
file:/cacti/graph_p_w_picpath.php//include("./include/auth.php");
include("./include/global.php");
品茶:首先去掉验证模块,发现少了涵数,再去auth.php里看,发现此涵数是在./include/global.php中,载入此文件后问题得到解决。
需求2:首先需要连接邮件,down图,并把图发送到收件中。最后做成一个定时任务。/code/cacti_report/downp_w_picpath.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 p_w_picpathfile 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.p_w_picpath import MIMEImage
from downp_w_picpath 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/p_w_picpath/nginx1.png"
t100 = "/code/cacti_report/p_w_picpath/t100.png"
t200 = "/code/cacti_report/p_w_picpath/t200.png"
#download p_w_picpath
out = DownImage("http://www.cacti.com/cacti/graph_p_w_picpath.php?action=view&local_graph_id=6",NginxImage)
out2 = DownImage("http://www.cacti.com/cacti/graph_p_w_picpath.php?local_graph_id=51",t100)
out3 = DownImage("http://www.cacti.com/cacti/graph_p_w_picpath.php?local_graph_id=196",t200)
#define email conent
msgText = MIMEText('报表(昨天 8:00 - 今天 8:00):
Nginx状态图:
\
100段网络流量图:
\
200段网络流量图:
','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', '')
msgRoot.attach(msgImage)
#switch 100 range traffic
msgImage = MIMEImage(open(t100, 'rb').read())
msgImage.add_header('Content-ID', '')
msgRoot.attach(msgImage)
#switch 10048 range traffic
msgImage = MIMEImage(open(t200, 'rb').read())
msgImage.add_header('Content-ID', '')
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
品茶:记得需要一个p_w_picpath文件夹,这样每天早上8点记得收信看报表,结果可能是这样子的。
品茶:是不是觉着特别高大上!