zabbix发screen报表
本帖最后由 51itpub 于 2015-07-01 17:02 编辑领导要求:每天收到服务器负荷报表,参照网上Python的脚本,但发出来的邮件都是红叉叉:
这是什么原因?
晕,这里禁了贴URL。
不能贴URL,把参照的这段代码贴在这里:
cat zabbix_send_report.py
#! /usr/bin/env python
#coding=utf-8
# Andy_f
import time,os
import urllib
import urllib2
import cookielib
import MySQLdb
import smtplib
from email.mime.text import MIMEText
screens = ["xxx","xxx"]
#
save_graph_path = "/var/www/zabbix/reports/%s"%time.strftime("%Y-%m-%d")
if not os.path.exists(save_graph_path):
os.makedirs(save_graph_path)
# zabbix host
zabbix_host = "zabbix.xxx.com"
# zabbix login username
username = "admin"
# zabbix login password
password = "zabbix"
# graph width
width = 600
# graph height
height = 100
# graph Time period, s
period = 86400
# zabbix DB
dbhost = "xxx.xxx.xxx.xxx"
dbport = 3306
dbuser = "zabbix"
dbpasswd = "xxxxx"
dbname = "zabbix"
to_list = ["xxx@xxx.com","aaa@xxx.com"]
smtp_server = "smtp.163.com"
mail_user = "xxxx"
mail_pass = "xxxxx"
domain= "163.com"
def mysql_query(sql):
try:
conn = MySQLdb.connect(host=dbhost,user=dbuser,passwd=dbpasswd,port=dbport,connect_timeout=20)
conn.select_db(dbname)
cur = conn.cursor()
count = cur.execute(sql)
if count == 0:
result = 0
else:
result = cur.fetchall()
return result
cur.close()
conn.close()
except MySQLdb.Error,e:
print "mysql error:" ,e
def get_graph(zabbix_host,username,password,screen,width,height,period,save_graph_path):
screenid_list = []
global html
html = ''
for i in mysql_query("select screenid from screens where name='%s'"%(screen)):
for screenid in i:
graphid_list = []
for c in mysql_query("select resourceid from screens_items where screenid='%s'"%(int(screenid))):
for d in c:
graphid_list.append(int(d))
for graphid in graphid_list:
login_opt = urllib.urlencode({
"name": username,
"password": password,
"autologin": 1,
"enter": "Sign in"})
get_graph_opt = urllib.urlencode({
"graphid": graphid,
"screenid": screenid,
"width": width,
"height": height,
"period": period})
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_url = r"URL://%s/index.php"%zabbix_host
save_graph_url = r"URL://%s/chart2.php"%zabbix_host
opener.open(login_url,login_opt).read()
data = opener.open(save_graph_url,get_graph_opt).read()
filename = "%s/%s.%s.png"%(save_graph_path,screenid,graphid)
html += '<img width="600" height="250" src="URL://%s/%s/%s/%s.%s.png">'%(zabbix_host,save_graph_path.split("/"),save_graph_path.split("/"),screenid,graphid)
f = open(filename,"wb")
f.write(data)
f.close()
def send_mail(username,password,smtp_server,to_list,sub,content):
print to_list
me = "运维"+"<"+username+"@"+domain +">"
msg = MIMEText(content,_subtype="html",_charset="utf8")
msg["Subject"] = sub
msg["From"] = me
msg["To"] = ";".join(to_list)
try:
server = smtplib.SMTP()
server.connect(smtp_server)
server.login(username,password)
server.sendmail(me,to_list,msg.as_string())
server.close()
print "send mail Ok!"
except Exception, e:
print e
if __name__ == '__main__':
for screen in screens:
get_graph(zabbix_host,username,password,screen,width,height,period,save_graph_path)
send_mail(mail_user,mail_pass,smtp_server,to_list,"test email",html)
设置crontab 每天早上上班前执行一次
crontab -e
45 08 * * * python /root/zabbix_send_report.py 我把关健字H^T^TP:换成RUL:了,在上面的代码里。
我目前的判断是html里只有html代码,但实际的图片没有发出来。图片在服务上能正常生成。
首先:save_graph_path = "/var/www/zabbix/reports/××ד
这下面是否正常生成了图片?
其次
URL://%s/%s/%s/%s.%s.png
直接访问该路径可以看到图片吗? 跑个nginx 然后根路径配置到你的save_graph_path 然后 你本地试试能不能ip访问到 然后在跑report脚本试试
这个问题,后来的排查是生产的html所指的图片路径与生成图片的路径不一样. 回复 7# bun
这个百度一下呀,到处都是。
页:
[1]