利用python+zabbix查询服务器利用率

需求:统计现有服务器的资源利用率,降本增效

方案: 利用现有zabbix监控信息,使用python检索zabbix的mysql库,筛选出可用信息进行计算,倒出表格

问题:zabbix mysql表信息需要查询,zabbix4以后数据库结构发生变化,groups表被取消,无法直接查询出组名对应组id

脚本简介:

根据主机组名称->通过模版表hstgrp查询出主机组id->通过主机与主机组对应表hosts_groups查询出该组所有主机的ip->遍历ip去监控项表item查询监控项键值key_的值 →根据进制 进行需求计算(键值与zabbix监控项模版相关,如有特殊需求,需先创建监控项)→计算结果在当前目录生成 主机组名+当前日期的.xlsx 文件

#!/usr/bin/python
#coding=utf-8
import sys
reload(sys) 
sys.setdefaultencoding('utf-8')
import pymysql as MySQLdb
import time
import datetime
import xlsxwriter,xlwt

# zabbix数据库信息:
zdbhost = 'localhost'
zdbuser = 'zabbix'
zdbpass = 'yoyi@2018'
zdbport = 3306
zdbname = 'zabbix'

# 生成文件名称:
xlsfilename = 'Group_Production_Server.xlsx'

# 需要查询的key列表 [名称,表名,key值,取值,格式化,数据整除处理]
keys = [
  ['CPU平均空闲值','trends','system.cpu.util[,idle]','avg','%.2f',1],
  ['CPU最小空闲值','trends','system.cpu.util[,idle]','min','%.2f',1],
  ['内存大小(单位G)','trends_uint','vm.memory.size[total]','avg','',1073741824],
  ['剩余内存(单位G)','trends_uint','vm.memory.size[available]','avg','',1073741824],
  ['内存使用(单位G)','trends_uint','vm.memory.size[used]','avg','',1073741824],
  ['可用平均内存(单位G)','trends_uint','vm.memory.size[available]','avg','',1073741824],
  ['可用最小内存(单位G)','trends_uint','vm.memory.size[available]','min','',1073741824],
  #['swap总大小(单位G)','trends_uint','system.swap.size[,total]','avg','',1048576000],
  #['swap平均剩余(单位G)','trends_uint','system.swap.size[,free]','avg','',1048576000],
  ['DISK总大小(G)','trends_uint','vfs.fs.size[/,total]','avg','',1073741824],
  ['DISK剩余(G)','trends_uint','vfs.fs.size[/,free]','avg','',1073741824],
  ['DISK使用(G)','trends_uint','vfs.fs.size[/,used]','avg','',1073741824],
  #['磁盘使用率(%)','trends','fs01_used','avg','%.2f',1],
  #['进入最大流量(单位Kbps)','trends_uint','net.if.in[eth0]','max','',1000],
  #['进入平均流量(单位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):
    '''打开数据库连接'''
    self.conn = MySQLdb.connect(host=zdbhost,user=zdbuser,passwd=zdbpass,port=zdbport,db=zdbname)
    #self.cursor = self.conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
    self.cursor = self.conn.cursor()
    
    #生成zabbix哪个分组报表
    self.groupname = 'JXQ-algo'

    #获取IP信息:
    self.IpInfoList = self.__getHostList()

  def __getHostList(self):
    '''根据zabbix组名获取该组所有IP'''
    
    #查询组ID:
    sql = '''select groupid from hstgrp where name = '%s'; ''' % self.groupname
    self.cursor.execute(sql)
    groupid = self.cursor.fetchone()[0]
    #groupid = '49'
    
    #根据groupid查询该分组下面的所有主机ID(hostid):
    sql = '''select hostid from hosts_groups where groupid = '%s' ''' % groupid
    self.cursor.execute(sql)
    hostlist = self.cursor.fetchall()
    
    #生成IP信息字典:结构为{'119.146.207.19':{'hostid':10086L,},}
    IpInfoList = {}
    for i in hostlist:
      hostid = i[0]
      sql = '''select ip from interface where hostid = '%s' ''' % hostid
      ret = self.cursor.execute(sql)
      if ret:
        IpInfoList[self.cursor.fetchone()[0]] = {'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']
      itemid = self.cursor.fetchone()[0]
    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']
    result = self.cursor.fetchone()[0]
    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']
    result = self.cursor.fetchone()[0]
    if result:
      result = int(result)
    else:
      result = 0
    return result
    
  def getLastMonthData(self,type,hostid,table,itemname):
    '''根据hostid,itemname获取该监控项的值'''
    #获取上个月的第20天和最后1天
    ts_first = int(time.mktime(datetime.date(datetime.date.today().year,datetime.date.today().month-1,20).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 getNowData(self):
    nowtime = datetime.datetime.now().strftime('%Y-%m-%d')
    return nowtime
    
  def getInfo(self):
    #循环读取IP列表信息
    for ip,resultdict in zabbix.IpInfoList.items():
      print "正在查询 IP:%-15s hostid:%5d 的信息!" % (ip, resultdict['hostid'])
      
      #循环读取keys,逐个key统计数据:
      for value in keys:
        print "\t正在统计 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文件'''
    #xlsfilename = '%s.xls'%(self.groupname)
    xlsfilename = '%s-%s.xlsx'%(self.groupname,datetime.date.today())
    try:
      wbk = xlsxwriter.Workbook(xlsfilename)
      worksheet = wbk.add_worksheet()
      wbk.add_format({'bold': 1})
      worksheet.set_column("A:I", 18)
      worksheet.set_row(row=0,height=30,cell_format=None)
      #设置样式
      title_formatter = wbk.add_format()
      title_formatter.set_border(1)
      title_formatter.set_bg_color('#4682B4')
      title_formatter.set_align('center')
      title_formatter.set_bold()
      title_formatter.set_color("#F8F8FF")
      title_formatter.set_font_size(14)
 
      content_formatter=wbk.add_format()
      content_formatter.set_font_size(12)
      content_formatter.set_align("center")
 
      #写入第一列:
      worksheet.write(0,0,"主机名",title_formatter)
      i = 1
      for ip in self.IpInfoList:
        worksheet.write(i,0,ip,content_formatter)
        i = i + 1
      #写入其他列:
      i = 1
      for value in keys:
        worksheet.write(0,i,value[0],title_formatter)
        #写入该列内容:
        j = 1
        for ip,result in self.IpInfoList.items():
          if value[4]:
            worksheet.write(j,i, value[4] % result[value[2]][value[3]],content_formatter)
          else:
            worksheet.write(j,i, result[value[2]][value[3]] / value[5],content_formatter)
          j = j + 1 
        i = i + 1
      #wbk.save(xlsfilename)
      wbk.close()
    except Exception as e:
      print (e)
  #def __del__(self):
  #  '''关闭数据库连接'''
  #  self.cursor.close()
  #  self.conn.close()

if __name__ == "__main__":
  zabbix = ReportForm()
  zabbix.getInfo()
  zabbix.writeToXls2()

 结果事例

 

你可能感兴趣的:(zabbix,zabbix,服务器,运维)