需求:
1、取cacti的圖時不需要登陸(只對可訪問用戶開放),但其它操作時需要登陸。
2、每天早上把報表發送到指定郵件
仿StackOverFlow的國內站正在成長,國內的問題解決論壇需要大家出一份力!
需求1:
file:/cacti/graph_image.php
1 2 | //include("./include/auth.php"); include ( "./include/global.php" ); |
品茶:首先去掉驗證模塊,發現少了涵數,再去auth.php裏看,發現此涵數是在./include/global.php中,載入此文件後問題得到解決。
需求2:首先需要連接郵件,down圖,並把圖發送到收件中。最後做成一個定時任務。
1 2 3 4 5 6 7 8 9 10 11 12 | / 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!" ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | / 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() |
定時任務:
1 2 | # crontab -l 0 8 * * * /code/cacti_report/send_cacti .py |
品茶:記得需要一個image文件夾,這樣每天早上8點記得收信看報表,結果可能是這樣子的。
品茶:是不是覺着特別高大上!
目的想整合統一問答解決方案,希望大家能爲這份目標盡一份力推薦下soexception,或者在圈子裏解決問題。真是謝謝了