歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python 實現Zabbix自動發送報表

Python 實現Zabbix自動發送報表

日期:2017/3/1 9:42:17   编辑:Linux編程

實現將Zabbix中的screens中的圖片以郵件的方式發送到管理員郵箱,大家都知道Zabbix的screens一般是定義管理員比較關注的監控指標好方便浏覽,那麼我們就根據管理員自定義的screens name獲取相關圖信息的。

一些Zabbix相關教程集合

安裝部署分布式監控系統Zabbix 2.06 http://www.linuxidc.com/Linux/2013-07/86942.htm

《安裝部署分布式監控系統Zabbix 2.06》 http://www.linuxidc.com/Linux/2013-07/86942.htm

CentOS 6.3下Zabbix安裝部署 http://www.linuxidc.com/Linux/2013-05/83786.htm

Zabbix分布式監控系統實踐 http://www.linuxidc.com/Linux/2013-06/85758.htm

CentOS 6.3下Zabbix監控apache server-status http://www.linuxidc.com/Linux/2013-05/84740.htm

CentOS 6.3下Zabbix監控MySQL數據庫參數 http://www.linuxidc.com/Linux/2013-05/84800.htm

簡介

首先我們來看下圖片的url (右擊screens中隨便一張圖片,選復制圖片地址)

http://zabbix.xxx.com/chart2.php?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如下

http://zabbix.xxx.com/chart2.php?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代碼發送到管理員郵箱

大概的流程是這樣的,代碼如下,代碼中所涉及的參數變量根據自己的環境做出相應的配置即可

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"

# mail

to_list = ["[email protected]","[email protected]"]

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"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()

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

上效果圖

《Python核心編程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

在Ubuntu下用Python搭建桌面算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm

Python 的詳細介紹:請點這裡
Python 的下載地址:請點這裡

本文永久更新鏈���地址:http://www.linuxidc.com/Linux/2014-07/104093.htm

Copyright © Linux教程網 All Rights Reserved