Python 一键巡检服务器资源使用率

一.分析需求与解决方案

1.需要:定期的去统计所有主机的cpu mem disk的使用率。

2.思路方案:1):安装云商的agent,调用api统计。

                    2):方案是接入自己的监控,查询数据库或者zabbix api统计。

                    3):通过ansible批量执行脚本来统计。

3.这里使用简单快速的方式,直接查询zabbix数据库的统计趋势值来统计。且windows和Linux的监控指标有个别参数不太一样,所以区分2个脚本,后面可以优化为一个脚本。

4.环境:python3.x  ,主要使用包:MySQLdb   xlsxwriter,xlwt

 

二.Zabbix item环境准备

 

Python 一键巡检服务器资源使用率_第1张图片 内存使用率计算item

 

 

Python 一键巡检服务器资源使用率_第2张图片 实际内存使用item

 

 

 

   

Python 一键巡检服务器资源使用率_第3张图片 实际内存使用率item

 

 

   

Python 一键巡检服务器资源使用率_第4张图片 实际内存空闲率item

 

 

   

Python 一键巡检服务器资源使用率_第5张图片 Linux cpu使用率简单统计

 

   

 

   

Python 一键巡检服务器资源使用率_第6张图片 windows  cpu使用率简单计算

 

二.代码分享

    1.Linux 系统

   

#coding:utf-8
  
import MySQLdb
#import pymysql
import time,datetime
import xlsxwriter,xlwt

  
  
#zabbix数据库信息:
zdbhost = 'www.csdn.net'
zdbuser = 'python_test'
zdbpass = '123456'
zdbport = 3306
zdbname = 'zabbix'
  

#需要查询的key列表 [名称,表名,key值,取值,格式化,数据整除处理]
'''
注意:keys中的参数要在存在开启状态,才会统计到数据库中,检查下!!!
Linux:
keys = [
  ['CPU总大小','trends_uint','system.cpu.num[online]','avg','',1],
  ['CPU平均avg','trends','system.cpu.load[percpu,avg5]','avg','%.2f',1],
  ['MEM总大小(G)','trends_uint','vm.memory.size[total]','avg','',1048576000],
  ['MEM剩余(单位G)','trends_uint','vm.memory.size[available]','avg','',1048576000],
  ['MEM使用(G)','trends_uint','vm.memory.size[used]','avg','',1048576000],
  ['MEM剩余率avg(%)','trends','vm.memory.size[apercent]','avg','%.3f',1],
  ['MEM使用率avg(%)','trends','vm.memory.size[upercent]','avg','%.3f',1],
  ['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],
  ['DISK使用率avg(%)','trends','vfs.fs.size[/,pused]','avg','%.3f',1],
 
]



windows:
keys = [
  ['CPU 总大小','trends_uint','system.cpu.num[online]','avg','',1],
  ['CPU 平均avg','trends','system.cpu.load[percpu,avg5]','avg','%.2f',1],
  ['MEM总大小(G)','trends_uint','vm.memory.size[total]','avg','',1048576000],
  ['MEM可用(单位G)','trends_uint','vm.memory.size[free]','avg','',1048576000],
  ['MEM使用率(%)','trends','vm.memory.size[pused]','avg','%.2f',1],
  ['DISK总大小(单位G)','trends_uint','vfs.fs.size[C:,total]','avg','',1073741824],
  ['DISK剩余(单位G)','trends_uint','vfs.fs.size[C:,free]','avg','',1073741824],
  ['DISK使用率(%)','trends','vfs.fs.size[C:,pfree]','avg','%.2f',1],

]
  
'''

keys = [
  ['CPU总大小','trends_uint','system.cpu.num[online]','avg','',1],
  ['CPU平均avg','trends','system.cpu.load[percpu,avg5]','avg','%.2f',1],
  ['MEM总大小(G)','trends_uint','vm.memory.size[total]','avg','',1048576000],
  ['MEM剩余(单位G)','trends_uint','vm.memory.size[available]','avg','',1048576000],
  ['MEM使用(G)','trends_uint','vm.memory.size[used]','avg','',1048576000],
  ['MEM使用率avg(%)','trends','vm.memory.size[upercent]','avg','%.3f',1],
  ['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],
  ['DISK使用率avg(%)','trends','vfs.fs.size[/,pused]','avg','%.3f',1],
 
]



  
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)
  
    #生成zabbix哪个分组报表
    self.groupname = "test"
    
  
    #获取IP信息:
    self.IpInfoList = self.__getHostList()
  
  def __getHostList(self):
    '''根据zabbix组名获取该组所有IP'''
  
    #查询组ID:
    sql = '''select groupid from groups where name = '%s' ''' % self.groupname
    self.cursor.execute(sql)
    groupid = self.cursor.fetchone()['groupid']
  
    #根据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['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):
    '''根据hostid,itemname获取该监控项的值'''
    #获取上个月的第一天和最后一天
    first="2020-07-21 00:00:00"
    last = "2020-07-22 10:17:00"
    ts_first = int(time.mktime(time.strptime(first, "%Y-%m-%d %H:%M:%S")))
    ts_last = int(time.mktime(time.strptime(last, "%Y-%m-%d %H:%M:%S")))
  
    itemid = self.__getItemid(hostid, itemname)
    function = getattr(self,'get%sValue' % table.capitalize())
  
    return function(type,itemid, ts_first, ts_last)
  
  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)
    try:
      wbk = xlwt.Workbook(encoding='utf-8')
      # 创建一个表格
      worksheet = wbk.add_sheet('sheet1', cell_overwrite_ok=True)
    
  
      #写入第一列:
      worksheet.write(0,0,"主机名")
      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])
        #写入该列内容:
        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
      wbk.save(xlsfilename)
    except Exception as e:
      print (e)

  
  
  
  

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

Python 一键巡检服务器资源使用率_第7张图片

Python 一键巡检服务器资源使用率_第8张图片

 

    2.Windows 系统

 

#coding:utf-8
  
import MySQLdb
#import pymysql
import time,datetime
import xlsxwriter,xlwt

  
  
#zabbix数据库信息:
zdbhost = 'www.csdn.net'
zdbuser = 'python_test'
zdbpass = '123456'
zdbport = 3306
zdbname = 'zabbix'
  

#需要查询的key列表 [名称,表名,key值,取值,格式化,数据整除处理]
'''
keys = [
  ['CPU5分钟负载','trends','system.cpu.load[percpu,avg5]','avg','%.2f',1],
  ['物理内存大小(单位G)','trends_uint','vm.memory.size[total]','avg','',1048576000],
  ['可用平均内存(单位G)','trends_uint','vm.memory.size[available]','avg','',1048576000],
  ['可用最小内存(单位G)','trends_uint','vm.memory.size[available]','min','',1048576000],
  ['根分区总大小(单位G)','trends_uint','vfs.fs.size[/,total]','avg','',1073741824],
  ['根分区平均剩余(单位G)','trends_uint','vfs.fs.size[/,free]','avg','',1073741824],
]


['磁盘总大小(单位G)','trends_uint','vfs.fs.size[C:,total]','avg','',1073741824],
['磁盘平均剩余(单位G)','trends_uint','vfs.fs.size[C:,free]','avg','',1073741824],
['磁盘平均使用率(%)','trends','vfs.fs.size[C:,pfree]','avg','%.2f',1],
system.cpu.num
'''

keys = [
  ['CPU 总大小','trends_uint','system.cpu.num[online]','avg','',1],
  ['CPU 平均avg','trends','system.cpu.load[percpu,avg5]','avg','%.2f',1],
  ['MEM总大小(G)','trends_uint','vm.memory.size[total]','avg','',1048576000],
  ['MEM可用(单位G)','trends_uint','vm.memory.size[free]','avg','',1048576000],
  ['MEM使用率(%)','trends','vm.memory.size[pused]','avg','%.2f',1],
  ['DISK总大小(单位G)','trends_uint','vfs.fs.size[C:,total]','avg','',1073741824],
  ['DISK剩余(单位G)','trends_uint','vfs.fs.size[C:,free]','avg','',1073741824],
  ['DISK使用率(%)','trends','vfs.fs.size[C:,pfree]','avg','%.2f',1],

]
  
  
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)
  

    
  
    #生成zabbix哪个分组报表
    self.groupname = "windows"
  
    #获取IP信息:
    self.IpInfoList = self.__getHostList()
  
  def __getHostList(self):
    '''根据zabbix组名获取该组所有IP'''
  
    #查询组ID:
    sql = '''select groupid from groups where name = '%s' ''' % self.groupname
    self.cursor.execute(sql)
    groupid = self.cursor.fetchone()['groupid']
  
    #根据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['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):
    '''根据hostid,itemname获取该监控项的值'''
    #获取上个月的第一天和最后一天
    first="2020-07-20 16:00:00"
    last = "2020-07-21 17:35:00"
    ts_first = int(time.mktime(time.strptime(first, "%Y-%m-%d %H:%M:%S")))
    ts_last = int(time.mktime(time.strptime(last, "%Y-%m-%d %H:%M:%S")))
  
    itemid = self.__getItemid(hostid, itemname)
    function = getattr(self,'get%sValue' % table.capitalize())
  
    return function(type,itemid, ts_first, ts_last)
  
  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)
    try:
      import xlwt
  
      wbk = xlwt.Workbook(encoding='utf-8')
      # 创建一个表格
      worksheet = wbk.add_sheet('sheet1', cell_overwrite_ok=True)
    
  
      #写入第一列:
      worksheet.write(0,0,"主机")
      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])
        #写入该列内容:
        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
      wbk.save(xlsfilename)
    except Exception as e:
      print (e)

  
  
  

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


Python 一键巡检服务器资源使用率_第9张图片
 

 

三.Linux 与Windows精简合一

#coding:utf-8
  
import MySQLdb
#import pymysql
import time,datetime
import xlsxwriter,xlwt

  
  
#zabbix数据库信息:
zdbhost = 'www.csdn.net'
zdbuser = 'python_test'
zdbpass = '123456'
zdbport = 3306
zdbname = 'zabbix'
  

#需要查询的key列表 [名称,表名,key值,取值,格式化,数据整除处理]
'''
注意:keys中的参数要在存在开启状态,才会统计到数据库中,检查下!!!
windows 与 Linux 区别就在于keys不同,注意检查item是否采集。
  
'''




  
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)
  
    #生成zabbix哪个分组报表
    windows_keys = [
      ['CPU 总大小', 'trends_uint', 'system.cpu.num[online]', 'avg', '', 1],
      ['CPU 平均avg', 'trends', 'system.cpu.load[percpu,avg5]', 'avg', '%.2f', 1],
      ['MEM总大小(G)', 'trends_uint', 'vm.memory.size[total]', 'avg', '', 1048576000],
      ['MEM可用(单位G)', 'trends_uint', 'vm.memory.size[free]', 'avg', '', 1048576000],
      ['MEM使用率(%)', 'trends', 'vm.memory.size[pused]', 'avg', '%.2f', 1],
      ['DISK总大小(单位G)', 'trends_uint', 'vfs.fs.size[C:,total]', 'avg', '', 1073741824],
      ['DISK剩余(单位G)', 'trends_uint', 'vfs.fs.size[C:,free]', 'avg', '', 1073741824],
      ['DISK使用率(%)', 'trends', 'vfs.fs.size[C:,pfree]', 'avg', '%.2f', 1],

    ]

    linux_keys = [
      ['CPU总大小', 'trends_uint', 'system.cpu.num[online]', 'avg', '', 1],
      ['CPU平均avg', 'trends', 'system.cpu.load[percpu,avg5]', 'avg', '%.2f', 1],
      ['MEM总大小(G)', 'trends_uint', 'vm.memory.size[total]', 'avg', '', 1048576000],
      ['MEM剩余(单位G)', 'trends_uint', 'vm.memory.size[available]', 'avg', '', 1048576000],
      ['MEM使用(G)', 'trends_uint', 'vm.memory.size[used]', 'avg', '', 1048576000],
      ['MEM使用率avg(%)', 'trends', 'vm.memory.size[upercent]', 'avg', '%.3f', 1],
      ['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],
      ['DISK使用率avg(%)', 'trends', 'vfs.fs.size[/,pused]', 'avg', '%.3f', 1],

    ]

    self.keys=[]
    self.groupname = "windows"
    if self.groupname=="windows":
      self.keys=windows_keys
    else:
      self.keys=linux_keys
  
    #获取IP信息:
    self.IpInfoList = self.__getHostList()
  
  def __getHostList(self):
    '''根据zabbix组名获取该组所有IP'''
  
    #查询组ID:
    sql = '''select groupid from groups where name = '%s' ''' % self.groupname
    self.cursor.execute(sql)
    groupid = self.cursor.fetchone()['groupid']
  
    #根据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['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):
    '''根据hostid,itemname获取该监控项的值'''
    #获取上个月的第一天和最后一天
    first="2020-07-21 20:00:00"
    last = "2020-07-22 20:35:00"
    ts_first = int(time.mktime(time.strptime(first, "%Y-%m-%d %H:%M:%S")))
    ts_last = int(time.mktime(time.strptime(last, "%Y-%m-%d %H:%M:%S")))
  
    itemid = self.__getItemid(hostid, itemname)
    function = getattr(self,'get%sValue' % table.capitalize())
  
    return function(type,itemid, ts_first, ts_last)
  
  def getInfo(self):
    #循环读取IP列表信息
    for ip,resultdict in zabbix.IpInfoList.items():
      print("正在查询 IP:%-15s hostid:%5d 的信息!" % (ip, resultdict['hostid']))
      #循环读取keys,逐个key统计数据:
      for value in self.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)
    try:
      wbk = xlwt.Workbook(encoding='utf-8')
      # 创建一个表格
      worksheet = wbk.add_sheet('sheet1', cell_overwrite_ok=True)
    
  
      #写入第一列:
      worksheet.write(0,0,"主机名")
      i = 1
      for ip in self.IpInfoList:
        worksheet.write(i,0,ip)
        i = i + 1
      #写入其他列:
      i = 1
      for value in self.keys:
        worksheet.write(0,i,value[0])
        #写入该列内容:
        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
      wbk.save(xlsfilename)
    except Exception as e:
      print (e)

  
  
  
  

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

 

 

四.统一定义用户组和时间以及表头样式

#coding:utf-8
  
import MySQLdb
#import pymysql
import time,datetime
import xlsxwriter,xlwt

  
  
#zabbix数据库信息:
zdbhost = 'www.csdn.net'
zdbuser = 'csdn'
zdbpass = 'csdn'
zdbport = 3306
zdbname = 'zabbix'
  

#需要查询的key列表 [名称,表名,key值,取值,格式化,数据整除处理]
'''
注意:keys中的参数要在存在开启状态,才会统计到数据库中,检查下!!!
Linux:
keys = [
  ['CPU总大小(num)','trends_uint','system.cpu.num[online]','avg','',1],
  ['CPU平均avg(float)','trends','system.cpu.load[percpu,avg5]','avg','%.2f',1],
  ['MEM总大小(G)','trends_uint','vm.memory.size[total]','avg','',1048576000],
  ['MEM剩余(单位G)','trends_uint','vm.memory.size[available]','avg','',1048576000],
  ['MEM使用(G)','trends_uint','vm.memory.size[used]','avg','',1048576000],
  ['MEM剩余率avg(%)','trends','vm.memory.size[apercent]','avg','%.3f',1],
  ['MEM使用率avg(%)','trends','vm.memory.size[upercent]','avg','%.3f',1],
  ['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],
  ['DISK使用率avg(%)','trends','vfs.fs.size[/,pused]','avg','%.3f',1],
 
]

windows:
keys = [
  ['CPU 总大小(num)','trends_uint','system.cpu.num[online]','avg','',1],
  ['CPU 平均avg(float)','trends','system.cpu.load[percpu,avg5]','avg','%.2f',1],
  ['MEM总大小(G)','trends_uint','vm.memory.size[total]','avg','',1048576000],
  ['MEM可用(单位G)','trends_uint','vm.memory.size[free]','avg','',1048576000],
  ['MEM使用率(%)','trends','vm.memory.size[pused]','avg','%.2f',1],
  ['DISK总大小(G)','trends_uint','vfs.fs.size[C:,total]','avg','',1073741824],
  ['DISK剩余(G)','trends_uint','vfs.fs.size[C:,free]','avg','',1073741824],
  ['DISK使用率(%)','trends','vfs.fs.size[C:,pfree]','avg','%.2f',1],

]
  
'''




  
class ReportForm:
  
  def __init__(self,groupname):
    '''打开数据库连接'''
    self.conn = MySQLdb.connect(host=zdbhost,user=zdbuser,passwd=zdbpass,port=zdbport,db=zdbname)
    self.cursor = self.conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
  
    #生成zabbix哪个分组报表
    windows_keys = [
      ['CPU总数(num)', 'trends_uint', 'system.cpu.num[online]', 'avg', '', 1],
      ['CPU负载(f)', 'trends', 'system.cpu.load[percpu,avg5]', 'avg', '%.2f', 1],
      ['MEM总大小(G)', 'trends_uint', 'vm.memory.size[total]', 'avg', '', 1048576000],
      ['MEM可用(G)', 'trends_uint', 'vm.memory.size[free]', 'avg', '', 1048576000],
      ['MEM使用率(%)', 'trends', 'vm.memory.size[pused]', 'avg', '%.2f', 1],
      ['DISK总大小(G)', 'trends_uint', 'vfs.fs.size[C:,total]', 'avg', '', 1073741824],
      ['DISK剩余(G)', 'trends_uint', 'vfs.fs.size[C:,free]', 'avg', '', 1073741824],
      ['DISK使用率(%)', 'trends', 'vfs.fs.size[C:,pfree]', 'avg', '%.2f', 1],

    ]

    linux_keys = [
      ['CPU总数(num)', 'trends_uint', 'system.cpu.num[online]', 'avg', '', 1],
      ['CPU负载(f)', 'trends', 'system.cpu.load[percpu,avg5]', 'avg', '%.2f', 1],
      ['MEM总大小(G)', 'trends_uint', 'vm.memory.size[total]', 'avg', '', 1048576000],
      ['MEM使用(G)', 'trends_uint', 'vm.memory.size[used]', 'avg', '', 1048576000],
      ['MEM使用率avg(%)', 'trends', 'vm.memory.size[upercent]', 'avg', '%.3f', 1],
      ['DISK总大小(G)', 'trends_uint', 'vfs.fs.size[/,total]', 'avg', '', 1073741824],
      ['DISK剩余(G)', 'trends_uint', 'vfs.fs.size[/,free]', 'avg', '', 1073741824],
      ['DISK使用率(%)', 'trends', 'vfs.fs.size[/,pused]', 'avg', '%.3f', 1],

    ]

    self.keys=[]
    #self.groupname = "windows"
    self.groupname = groupname
    if self.groupname=="windows":
      self.keys=windows_keys
    else:
      self.keys=linux_keys
  
    #获取IP信息:
    self.IpInfoList = self.__getHostList()
  
  def __getHostList(self):
    '''根据zabbix组名获取该组所有IP'''
  
    #查询组ID:
    sql = '''select groupid from groups where name = '%s' ''' % self.groupname
    self.cursor.execute(sql)
    groupid = self.cursor.fetchone()['groupid']
  
    #根据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['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,first,last):
    '''根据hostid,itemname获取该监控项的值'''
    #获取上个月的第一天和最后一天
    '''first="2020-08-14 22:00:00"
    last = "2020-08-15 17:00:00"'''
    ts_first = int(time.mktime(time.strptime(first, "%Y-%m-%d %H:%M:%S")))
    ts_last = int(time.mktime(time.strptime(last, "%Y-%m-%d %H:%M:%S")))
  
    itemid = self.__getItemid(hostid, itemname)
    function = getattr(self,'get%sValue' % table.capitalize())
  
    return function(type,itemid, ts_first, ts_last)
  
  def getInfo(self,first,last):
    #循环读取IP列表信息
    for ip,resultdict in zabbix.IpInfoList.items():
      print("正在查询 IP:%-15s hostid:%5d 的信息!" % (ip, resultdict['hostid']))
      #循环读取keys,逐个key统计数据:
      for value in self.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],first,last)
        zabbix.IpInfoList[ip][value[2]][value[3]] = data
  
  
  def writeToXls2(self):
    '''生成xls文件'''
    xlsfilename = '%s-%s.xls'%(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 self.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)

  
  
  
  

 
if __name__ == "__main__":
  # 获取上个月的第一天和最后一天
  first = "2020-08-14 22:00:00"
  last = "2020-08-15  17:00:00"


  groups=['windows','Linux servers']
  for g in groups:
    zabbix = ReportForm(g)
    zabbix.getInfo(first,last)
    zabbix.writeToXls2()






'''
MySQL错误1042-Can't get hostname for your address解决方法
打开编辑,在
[mysqld]节点下新增或修改如下两行行
skip-name-resolve #忽略主机名的方式访问
lower_case_table_names=1 #忽略数据库表名大小写

重启mysql服务,问题得到解决。 
'''

 

你可能感兴趣的:(python基础及实践,监控系统及应用)