ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

python获取zabbix数据库数据生成月度报表

2020-08-26 08:32:51  阅读:392  来源: 互联网

标签:itemid hostid python 数据库 cursor zabbix sql self


#!/usr/bin/python3
# @Date: 2020/8/20 14:16
# @Author: zhangcheng
# @email: 3359957053@qq.com
# -*- coding: utf-8 -*-

import pymysql
import time,datetimeimport math

#zabbix数据库信息:
zdbhost = "192.168.200.41"
zdbuser = "root"
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.load[percpu,avg15]',
        '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 IpInfoList

    def __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 = None
        return itemid

    def getTrendsValue(self,itemid, start_time, stop_time):
        '''查询trends_uint表的值,type的值为min,max,avg三种'''
        resultlist = {}
        for type in ['min','max','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)
            result = self.cursor.fetchone()[0]
            if result == None:
                result = 0
            resultlist[type] = result
        return resultlist

    def getTrends_uintValue(self,itemid, start_time, stop_time):
        '''查询trends_uint表的值,type的值为min,max,avg三种'''
        resultlist = {}
        for type in ['min','max','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] = result
            else:
                resultlist[type] = 0
        return resultlist


    def 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()))
        itemid = self.__getItemid(hostid, itemname)

        function = getattr(self,'get{0}Value'.format(table.capitalize()))

        return  function(itemid, ts_first, 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("\t正在统计 key_:'{0}'".format(key))
                    data =  zabbix.getLastMonthData(resultdict['hostid'],table,key)
                    zabbix.IpInfoList[ip][key] = data


    def writeToXls(self):
        '''生成xls文件'''
        try:
            import xlsxwriter
            date = time.strftime("%Y-%m",time.localtime())
            #创建文件
            workbook = xlsxwriter.Workbook(self.groupname + "-" + date + '.xls')
            #创建工作薄
            worksheet = workbook.add_worksheet()
            #写入标题(第一行)
            i = 0
            for value in ["归属部门","主机","CPU核数","CPU平均空闲值","CPU最小空闲值","CPU15分钟负载","总内存","平均可用内存","最小可用内存"]:
                worksheet.write(0,i, value)
                i = i + 1
                #写入内容:
            j = 1
            for ip,value in self.IpInfoList.items():
                worksheet.write(j,0, self.groupname)
                worksheet.write(j,1, ip)
                worksheet.write(j,2, '{}'.format(value['system.cpu.num']['avg']))
                worksheet.write(j,3, '{:.2f}%'.format(value['system.cpu.util[,idle]']['avg']))
                worksheet.write(j,4, '{:.2f}%'.format(value['system.cpu.util[,idle]']['min']))
                worksheet.write(j,5, '{:.2f}'.format(value['system.cpu.load[percpu,avg15]']['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 + 1
            workbook.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()

效果图如下,由于CPU核数是新增的,故在本月是没有数据的:

 

标签:itemid,hostid,python,数据库,cursor,zabbix,sql,self
来源: https://www.cnblogs.com/zhangcheng94/p/13563045.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有