python 实现zabbix自动发送报表

实现将zabbix中的screens中的图片以邮件的方式发送到管理员邮箱

大家都知道zabbix的screens一般是定义管理员比较关注的监控指标好方便浏览,那么我们就根据管理员自定义的screens name获取相关图信息的.

简介

首先我们来看下图片的url (右击screens中随便一张图片,选复制图片地址)

?graphid=524&screenid=16&width=400&height=156&updateProfile=1&profileIdx=web.screens&profileIdx2=16&period=86400&stime=20150629172712&sid=f6dd0d127bba0123&curtime=1404120808037

可以看到上面的url后面有很多的参数,当然我们需要的没有那么多,简化后的url如下

?graphid=524&screenid=16&width=400&height=156&period=86400

简化后我们只需要5个参数

graphid#图片的ID,对应的值为某一个graph的唯一标识screenid#screenID,对应的值为某一个screen的唯一标识width#图片的宽度,对应的值为图片的宽度(可根据需要自己定义)height#图片的高度,对应的值为图片的高度(可根据需要自己定义)period#图片展示数据的时钟周期单位为秒(86400=1天)

通过上面的信息我们知道要通过zabbix获取screen中的图片需要的参数下面我们需要做的就是通过screen name到数据库中查询相关参数即可

1、根据管理员自定义的screens name在数据库中查询此screen的ID (screens 表)

2、根据所查到的screenID查询此screen中所包含的resourceid(resourceid==graphid)(screens_items 表)

3、通过1、2两步查到的参数获取图片并保存至zabbix服务器上的web目录并生成html代码

4、通过smtplib模块把html代码发送到管理员邮箱

大概的流程是这样的,代码如下,代码中所涉及的参数变量根据自己的环境做出相应的配置即可

catzabbix_send_report.py#!/usr/bin/envpython#coding=utf-8#Andy_fimporttime,osimporturllibimporturllib2importcookielibimportMySQLdbimportsmtplibfromemail.mime.textimportMIMETextscreens=[“xxx”,”xxx”]#save_graph_path=”/var/www/zabbix/reports/%s”%time.strftime(“%Y-%m-%d”)ifnotos.path.exists(save_graph_path):os.makedirs(save_graph_path)#zabbixhostzabbix_host=”zabbix.xxx.com”#zabbixloginusernameusername=”admin”#zabbixloginpasswordpassword=”zabbix”#graphwidthwidth=600#graphheightheight=100#graphTimeperiod,speriod=86400#zabbixDBdbhost=”xxx.xxx.xxx.xxx”dbport=3306dbuser=”zabbix”dbpasswd=”xxxxx”dbname=”zabbix”#mailto_list=[“xxx@xxx.com”,”aaa@xxx.com”]smtp_server=”smtp.163.com”mail_user=”xxxx”mail_pass=”xxxxx”domain=”163.com”defmysql_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)ifcount==0:result=0else:result=cur.fetchall()returnresultcur.close()conn.close()exceptMySQLdb.Error,e:print”mysqlerror:”,edefget_graph(zabbix_host,username,password,screen,width,height,period,save_graph_path):screenid_list=[]globalhtmlhtml=”foriinmysql_query(“selectscreenidfromscreenswherename=’%s'”%(screen)):forscreenidini:graphid_list=[]forcinmysql_query(“selectresourceidfromscreens_itemswherescreenid=’%s'”%(int(screenid))):fordinc:graphid_list.append(int(d))forgraphidingraphid_list:login_opt=urllib.urlencode({“name”:username,”password”:password,”autologin”:1,”enter”:”Signin”})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_hostsave_graph_url=r”http://%s/chart2.php”%zabbix_hostopener.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+='<imgwidth=”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()defsend_mail(username,password,smtp_server,to_list,sub,content):printto_listme=”运维”+”<“+username+”@”+domain+”>”msg=MIMEText(content,_subtype=”html”,_charset=”utf8″)msg[“Subject”]=submsg[“From”]=memsg[“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”sendmailOk!”exceptException,e:printeif__name__==’__main__’:forscreeninscreens:get_graph(zabbix_host,username,password,screen,width,height,period,save_graph_path)send_mail(mail_user,mail_pass,smtp_server,to_list,”testemail”,html)

设置crontab 每天早上上班前执行一次

crontab-e4508***python/root/zabbix_send_report.py

上效果图

本文出自 “小蚂蚁” 博客,,请务必保留此出处

于是夜莺会在黎明到来之前勇敢的将胸膛顶住蔷薇的刺,

python 实现zabbix自动发送报表

相关文章:

你感兴趣的文章:

标签云: