需求:
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,或者在圈子里解决问题。真是谢谢了