#!/usr/bin/python3
# @Date: 2020/8/20 14:16
# @Author: zhangcheng
# @email: 3359957053@qq.com
# -*- coding: utf-8 -*-import pymysql
import time,datetime
import math#zabbix数据库信息:
zdbhost = "192.168.63.141"
zdbuser = "zabbix"
zdbpass = "zabbix"
zdbport = 3306
zdbname = "zabbix"#需要查询的key列表
keys = {#'trends_uint':[# # 'vfs.fs.size[/,used]',# 'vm.memory.size[available]',# 'vm.memory.size[total]',#],'trends':['system.cpu.util[,user,avg5]',#'system.cpu.util[,idle]',# 'system.cpu.num',],}class ReportForm:def __init__(self):'''打开数据库连接'''self.conn = pymysql.connect(host=zdbhost,user=zdbuser,passwd=zdbpass,port=zdbport,db=zdbname,charset="utf8")self.cursor = self.conn.cursor()#生成zabbix哪个分组报表self.groupname = "总部"#获取IP信息:self.IpInfoList = self.__getHostList()def __getHostList(self):'''根据zabbix组名获取该组所有IP'''#查询组ID:sql = '''select groupid from hstgrp where name = "{0}"'''.format(self.groupname)self.cursor.execute(sql)groupid = self.cursor.fetchone()[0]#根据groupid查询该分组下面的所有主机ID(hostid):sql = '''select hostid from hosts_groups where groupid = {0}'''.format(groupid)self.cursor.execute(sql)hostlist = [i for i in list("%s" %j for j in self.cursor.fetchall())]# ['10550', '10551', '10552', '10553', '10555', '10556', '10557', '10558', '10559', '10560', '10561', '10562','10563', '10564']#生成IP信息字典:结构为{'112.111.222.55':{'hostid':'10086L'},}IpInfoList = {}for hostid in hostlist:sql = '''select host from hosts where status = 0 and hostid = {0}'''.format(hostid)ret = self.cursor.execute(sql)if ret:IpInfoList["".join("%s" %i for i in list(self.cursor.fetchone()))] = {'hostid':hostid}return IpInfoListdef __getItemid(self,hostid,itemname):'''获取itemid'''sql = '''select itemid from items where hostid = {0} and key_ = "{1}" '''.format(hostid, itemname)#print(sql)if self.cursor.execute(sql):itemid = "".join("%s" %i for i in list(self.cursor.fetchone()))else:print(hostid,itemname)itemid = Nonereturn itemiddef getTrendsValue(self,itemid, start_time, stop_time):'''查询trends_uint表的值,type的值为min,max,avg三种'''resultlist = {}for type in ['avg']:sql = '''select {0}(value_{1}) as result from trends where itemid = '{2}' and clock >= '{3}' and clock <= '{4}' '''.format(type, type, itemid, start_time, stop_time)self.cursor.execute(sql)print(sql)result = self.cursor.fetchone()[0]if result == None:result = 0resultlist[type] = resultreturn resultlistdef getTrends_uintValue(self,itemid, start_time, stop_time):'''查询trends_uint表的值,type的值为min,max,avg三种'''resultlist = {}for type in ['avg']:sql = '''select {0}(value_{1}) as result from trends_uint where itemid = {2} and clock >= {3} and clock <= {4}'''.format(type, type, itemid, start_time, stop_time)self.cursor.execute(sql)result = self.cursor.fetchone()[0]if result:resultlist[type] = resultelse:resultlist[type] = 0return resultlistdef getLastMonthData(self,hostid,table,itemname):'''根据hostid,itemname获取该监控项的值'''#获取上个月的第一天和最后一天ts_first = int(time.mktime(datetime.date(datetime.date.today().year,datetime.date.today().month,1).timetuple()))lst_last = datetime.date(datetime.date.today().year,datetime.date.today().month,1)-datetime.timedelta(1)# lst_last = datetime.date(datetime.date.today().year,datetime.date.today().month+1,1)-datetime.timedelta(1)ts_last = int(time.mktime(lst_last.timetuple()))# print(ts_lnow_time =int(time.mktime(datetime.datetime.now().timetuple()))#print(now_time)#print(now_time)itemid = self.__getItemid(hostid, itemname)function = getattr(self,'get{0}Value'.format(table.capitalize()))return function(itemid, now_time, ts_last)def getInfo(self):#循环读取IP列表信息for ip,resultdict in zabbix.IpInfoList.items():print("正在查询 IP:%-15s hostid:%5d 的信息" %(ip,int(resultdict['hostid'])))#循环读取keys,逐个key统计数据:for table, keylists in keys.items():for key in keylists:print(" 正在统计 key_:'{0}'".format(key))#print(" 正在统计 key_:'{0}'".format(key))#print(" 正在统计 key_:'{0}'".format(table))data = zabbix.getLastMonthData(resultdict['hostid'],table,key)zabbix.IpInfoList[ip][key] = datadef writeToXls(self):'''生成xls文件'''try:import xlsxwriterdate = time.strftime("%Y-%m",time.localtime())#创建文件'filename=self.groupname + "-" +date + ".xlsx"workbook = xlsxwriter.Workbook(filename)#创建工作薄worksheet = workbook.add_worksheet()#写入标题(第一行)i = 0#for value in ["归属部门","主机","CPU核数","CPU平均空闲值","CPU最小空闲值","CPU15分钟负载"]:for value in ["归属部门", "主机", "CPU负载"]:worksheet.write(0,i, value)i = i + 1#写入内容:j = 1for ip,value in self.IpInfoList.items():worksheet.write(j, 0, self.groupname)worksheet.write(j,1, ip)#print(value['system.cpu.num']['avg'])#worksheet.write(j,2, '{}'.format(value['system.cpu.num']['avg']))worksheet.write(j,2, '{:.2f}%'.format(value['system.cpu.util[,user,avg5]']['avg']))# worksheet.write(j,4, '{:.2f}%'.format(value['system.cpu.util[,idle]']['min']))#worksheet.write(j,1, '{:.2f}'.format(value['system.cpu.util[all,user,avg5]']['avg']))#worksheet.write(j,6, '{}GB'.format(math.ceil(value['vm.memory.size[total]']['avg'] / 1024 / 1024 / 1024)))#worksheet.write(j,7, '{}GB'.format(math.ceil(value['vm.memory.size[available]']['avg'] / 1024 / 1024 / 1024)))#worksheet.write(j,8, '{}GB'.format(math.ceil(value['vm.memory.size[available]']['min'] / 1024 / 1024 / 1024)))j = j + 1workbook.close()except Exception as e:print(e)def __del__(self):'''关闭数据库连接'''self.cursor.close()self.conn.close()if __name__ == "__main__":zabbix = ReportForm()zabbix.getInfo()zabbix.writeToXls()