首页 > 代码库 > Cacti 自动报表实现
Cacti 自动报表实现
需求:
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="http://www.mamicode.com/cid:webxxxload8"><br> 100段网络流量图:<br><img src="http://www.mamicode.com/cid:switch100"><br> 200段网络流量图:<br><img src="http://www.mamicode.com/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点记得收信看报表,结果可能是这样子的。
品茶:是不是觉着特别高大上!
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。