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 c in mysql_query("select graphid from graphs where templateid=1047 "):
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"http://%s/index.php"%zabbix_host
save_graph_url = r"http://%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="http://%s/%s/%s/%s.%s.png">‘%(zabbix_host,save_graph_path.split("/")[len(save_graph_path.split("/"))-2],save_graph_path.split("/")[len(save_graph_path.split("/"))-1],screenid,graphid)
f = open(filename,"wb")
f.write(data)
f.close()
至于发送邮件,还是继续套用python的工具包来做。可以采用下面的方式来发送html格式的邮件。
def send_mail2(user,subject,content):
me = mail_head+"<"+mail_user+"@"+mail_postfix+">"
print me
# msg = MIMEText(content,‘plain‘,‘utf-8‘)
msg = MIMEText(content,_subtype="html",_charset="utf8")
#msg[‘Subject‘] = Header(subject,‘utf-8‘)
msg[‘From‘] = me
msg[‘to‘] = user
global sendstatus
global senderr
try:
smtp = smtplib.SMTP()
smtp.connect(mail_host)
#smtp.login(mail_user,mail_pass)
smtp.sendmail(me,user,msg.as_string())
smtp.close()
print ‘send ok‘
sendstatus = True
except Exception,e:
senderr = str(e)
print senderr
sendstatus = False
logging.debug(user + ‘ ‘ + subject + ‘ ‘ + content)
这两个部分都结合起来,得到图片,然后作为附件发送邮件,整个监控发送图形报表的工作就基本完成了。
可能有些地方还是没有说的很清楚,我们可以继续讨论。我也在不断完善这个部分。
原文:http://blog.itpub.net/23718752/viewspace-1781581/