欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ZABBIX3.2使用python腳本實(shí)現(xiàn)監(jiān)控報(bào)表的方法

 更新時(shí)間:2019年07月02日 09:41:56   作者:思考v  
今天小編就為大家分享一篇ZABBIX3.2使用python腳本實(shí)現(xiàn)監(jiān)控報(bào)表的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

如下所示:

#!/usr/bin/python
#coding:utf-8
 
import MySQLdb
import time,datetime
 
 
#zabbix數(shù)據(jù)庫信息:
zdbhost = '172.16.8.200'
zdbuser = 'zabbix'
zdbpass = 'zabbix'
zdbport = 3306
zdbname = 'zabbix'
 
#生成文件名稱:
xlsfilename = 'zabbix.xls'
 
#需要查詢的key列表 [名稱,表名,key值,取值,格式化,數(shù)據(jù)整除處理]
keys = [
  ['CPU核心數(shù)','trends_uint','system.cpu.num','avg','',1],
  ['CPU平均空閑值','trends','system.cpu.util[,idle]','avg','%.2f',1],
  ['CPU最小空閑值','trends','system.cpu.util[,idle]','min','%.2f',1],
  ['CPU5分鐘負(fù)載','trends','system.cpu.load[percpu,avg5]','avg','%.2f',1],
  ['物理內(nèi)存大小(單位G)','trends_uint','vm.memory.size[total]','avg','',1048576000],
  ['可用平均內(nèi)存(單位G)','trends_uint','vm.memory.size[available]','avg','',1048576000],
  ['可用最小內(nèi)存(單位G)','trends_uint','vm.memory.size[available]','min','',1048576000],
  ['swap總大小(單位G)','trends_uint','system.swap.size[,total]','avg','',1048576000],
  ['swap平均剩余(單位G)','trends_uint','system.swap.size[,free]','avg','',1048576000],
  ['根分區(qū)總大小(單位G)','trends_uint','vfs.fs.size[/,total]','avg','',1073741824],
  ['根分區(qū)平均剩余(單位G)','trends_uint','vfs.fs.size[/,free]','avg','',1073741824],
  ['進(jìn)入最大流量(單位Kbps)','trends_uint','net.if.in[eth0]','max','',1000],
  ['進(jìn)入平均流量(單位Kbps)','trends_uint','net.if.in[eth0]','avg','',1000],
  ['出去最大流量(單位Kbps)','trends_uint','net.if.out[eth0]','max','',1000],
  ['出去平均流量(單位Kbps)','trends_uint','net.if.out[eth0]','avg','',1000],
]
 
 
class ReportForm:
 
  def __init__(self):
    '''打開數(shù)據(jù)庫連接'''
    self.conn = MySQLdb.connect(host=zdbhost,user=zdbuser,passwd=zdbpass,port=zdbport,db=zdbname)
    self.cursor = self.conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
 
    #生成zabbix哪個(gè)分組報(bào)表
    self.groupname = 'zabbix server'
 
    #獲取IP信息:
    self.IpInfoList = self.__getHostList()
 
  def __getHostList(self):
    '''根據(jù)zabbix組名獲取該組所有IP'''
 
    #查詢組ID:
    sql = '''select groupid from groups where name = '%s' ''' % self.groupname
    self.cursor.execute(sql)
    groupid = self.cursor.fetchone()['groupid']
 
    #根據(jù)groupid查詢?cè)摲纸M下面的所有主機(jī)ID(hostid):
    sql = '''select hostid from hosts_groups where groupid = %s''' % groupid
    self.cursor.execute(sql)
    hostlist = self.cursor.fetchall()
 
    #生成IP信息字典:結(jié)構(gòu)為{'119.146.207.19':{'hostid':10086L,},}
    IpInfoList = {}
    for i in hostlist:
      hostid = i['hostid']
      sql = '''select host from hosts where status = 0 and hostid = %s''' % hostid
      ret = self.cursor.execute(sql)
      if ret:
        IpInfoList[self.cursor.fetchone()['host']] = {'hostid':hostid}
    return IpInfoList
 
  def __getItemid(self,hostid,itemname):
    '''獲取itemid'''
    sql = '''select itemid from items where hostid = %s and key_ = '%s' ''' % (hostid, itemname)
    if self.cursor.execute(sql):
      itemid = self.cursor.fetchone()['itemid']
    else:
      itemid = None
    return itemid
 
  def getTrendsValue(self,type, itemid, start_time, stop_time):
    '''查詢trends_uint表的值,type的值為min,max,avg三種'''
    sql = '''select %s(value_%s) as result from trends where itemid = %s and clock >= %s and clock <= %s''' % (type, type, itemid, start_time, stop_time)
    self.cursor.execute(sql)
    result = self.cursor.fetchone()['result']
    if result == None:
      result = 0
    return result
 
  def getTrends_uintValue(self,type, itemid, start_time, stop_time):
    '''查詢trends_uint表的值,type的值為min,max,avg三種'''
    sql = '''select %s(value_%s) as result from trends_uint where itemid = %s and clock >= %s and clock <= %s''' % (type, type, itemid, start_time, stop_time)
    self.cursor.execute(sql)
    result = self.cursor.fetchone()['result']
    if result:
      result = int(result)
    else:
      result = 0
    return result
 
 
  def getLastMonthData(self,type,hostid,table,itemname):
    '''根據(jù)hostid,itemname獲取該監(jiān)控項(xiàng)的值'''
    #獲取上個(gè)月的第一天和最后一天
    ts_first = int(time.mktime(datetime.date(datetime.date.today().year,datetime.date.today().month-1,1).timetuple()))
    lst_last = datetime.date(datetime.date.today().year,datetime.date.today().month,1)-datetime.timedelta(1)
    ts_last = int(time.mktime(lst_last.timetuple()))
 
    itemid = self.__getItemid(hostid, itemname)
 
    function = getattr(self,'get%sValue' % table.capitalize())
 
    return function(type,itemid, ts_first, ts_last)
 
  def getInfo(self):
    #循環(huán)讀取IP列表信息
    for ip,resultdict in zabbix.IpInfoList.items():
      print "正在查詢 IP:%-15s hostid:%5d 的信息!" % (ip, resultdict['hostid'])
      #循環(huán)讀取keys,逐個(gè)key統(tǒng)計(jì)數(shù)據(jù):
      for value in keys:
        print "\t正在統(tǒng)計(jì) key_:%s" % value[2]
        if not value[2] in zabbix.IpInfoList[ip]:
          zabbix.IpInfoList[ip][value[2]] = {}
        data = zabbix.getLastMonthData(value[3], resultdict['hostid'],value[1],value[2])
        zabbix.IpInfoList[ip][value[2]][value[3]] = data
 
 
  def writeToXls2(self):
    '''生成xls文件'''
    try:
      import xlsxwriter
 
      #創(chuàng)建文件
      workbook = xlsxwriter.Workbook(xlsfilename)
 
      #創(chuàng)建工作薄
      worksheet = workbook.add_worksheet()
 
      #寫入第一列:
      worksheet.write(0,0,"主機(jī)".decode('utf-8'))
      i = 1
      for ip in self.IpInfoList:
        worksheet.write(i,0,ip)
        i = i + 1
 
      #寫入其他列:
      i = 1
      for value in keys:
        worksheet.write(0,i,value[0].decode('utf-8'))
 
        #寫入該列內(nèi)容:
        j = 1
        for ip,result in self.IpInfoList.items():
          if value[4]:
            worksheet.write(j,i, value[4] % result[value[2]][value[3]])
          else:
            worksheet.write(j,i, result[value[2]][value[3]] / value[5])
          j = j + 1
 
        i = i + 1
    except Exception,e:
      print e
 
 
 
  def __del__(self):
    '''關(guān)閉數(shù)據(jù)庫連接'''
    self.cursor.close()
    self.conn.close()
 
if __name__ == "__main__":
  zabbix = ReportForm()
  zabbix.getInfo()
  zabbix.writeToXls2()

以上這篇ZABBIX3.2使用python腳本實(shí)現(xiàn)監(jiān)控報(bào)表的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python和Golang協(xié)程的區(qū)別

    Python和Golang協(xié)程的區(qū)別

    這篇文章主要為大家介紹了Python和Golang協(xié)程的區(qū)別示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • 詳解python中各種文件打開模式

    詳解python中各種文件打開模式

    在python中,總的來說有三種大的模式打開文件,分別是:a, w, r,這篇文章主要介紹了python中各種文件打開模式,需要的朋友可以參考下
    2020-01-01
  • 詳解KMP算法以及python如何實(shí)現(xiàn)

    詳解KMP算法以及python如何實(shí)現(xiàn)

    這篇文章主要介紹了KMP算法的相關(guān)知識(shí)以及python如何實(shí)現(xiàn),幫助大家更好的進(jìn)行數(shù)據(jù)分析,感興趣的朋友可以了解下
    2020-09-09
  • Python編寫一個(gè)優(yōu)美的下載器

    Python編寫一個(gè)優(yōu)美的下載器

    這篇文章主要教大家如何使用Python編寫一個(gè)優(yōu)美的下載器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • python_mask_array的用法

    python_mask_array的用法

    今天小編就為大家分享一篇python_mask_array的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • 用python批量移動(dòng)文件

    用python批量移動(dòng)文件

    這篇文章主要介紹了如何用python批量移動(dòng)文件,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2021-01-01
  • Python3實(shí)現(xiàn)取圖片中特定的像素替換指定的顏色示例

    Python3實(shí)現(xiàn)取圖片中特定的像素替換指定的顏色示例

    這篇文章主要介紹了Python3實(shí)現(xiàn)取圖片中特定的像素替換指定的顏色,涉及Python3針對(duì)圖片文件的讀取、轉(zhuǎn)換、生成等相關(guān)操作技巧,需要的朋友可以參考下
    2019-01-01
  • 如何使用python?docx模塊操作word文檔

    如何使用python?docx模塊操作word文檔

    這篇文章主要介紹了如何使用python?docx模塊操作word文檔,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • pandas.dataframe中根據(jù)條件獲取元素所在的位置方法(索引)

    pandas.dataframe中根據(jù)條件獲取元素所在的位置方法(索引)

    今天小編就為大家分享一篇pandas.dataframe中根據(jù)條件獲取元素所在的位置方法(索引),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • Pycharm中python調(diào)用另一個(gè)文件類或者函數(shù)

    Pycharm中python調(diào)用另一個(gè)文件類或者函數(shù)

    本文主要介紹了Pycharm中python調(diào)用另一個(gè)文件類或者函數(shù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07

最新評(píng)論