五郎. 发表于 2019-1-25 08:37:14

Zabbix 使用规范和生成报表

  一、软件版本
  操作系统:CentOS-6.5-x86_64
  zabbix版本:3.0.3
  
二、zabbix标准:
  
  1、主机命名规范:
BJ-monitor-h-zabbix-01
**命名规范是,我们看一台主机能够知道主机位于哪里:BJ;做什么的:monitor;虚拟化还是实体机:h;跑什么服务:zabbix第几台:01
  
  2、一台主机直接关联多个模板:不用创建多个分组(可以结合CMDB平台)
Memcached :192.168.10.100
Php
Linux:192.168.10.100
多个模板:192.168.10.100linux,memcached,port,partion

  3、自定义key开启:Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.conf
方便识别服务名和增删key。也方便结合saltstack等批量工具。

  4、使用模板继承(linux):
比如系统我们创建有,php,nginx,mysql等模板,所有服务都是基于linux服务器,那么这三个模板我们可以继承linux os模板。
  
  三、zabbix os模板报表出图:
  # cat zabbix_report.py
  #!/usr/bin/python
  #coding:utf-8
  
  import MySQLdb
  import time,datetime
  
  #zabbix数据库信息:
  zdbhost = 'localhost'
  zdbuser = 'zabbix'
  zdbpass = 'zabbix'
  zdbport = 3306
  zdbname = 'zabbix'
  
  #生成文件名称:
  xlsfilename = 'damo.xls'
  
  #需要查询的key列表 [名称,表名,key值,取值,格式化,数据整除处理]
  keys = [
      ['CPU平均空闲值','trends','system.cpu.util[,idle]','avg','%.2f',1],
      ['CPU最小空闲值','trends','system.cpu.util[,idle]','min','%.2f',1],
      ['CPU5分钟负载','trends','system.cpu.load','avg','%.2f',1],
      ['物理内存大小(单位G)','trends_uint','vm.memory.size','avg','',1048576000],
      ['可用平均内存(单位G)','trends_uint','vm.memory.size','avg','',1048576000],
      ['可用最小内存(单位G)','trends_uint','vm.memory.size','min','',1048576000],
      ['swap总大小(单位G)','trends_uint','system.swap.size[,total]','avg','',1048576000],
      ['swap平均剩余(单位G)','trends_uint','system.swap.size[,free]','avg','',1048576000],
      ['根分区总大小(单位G)','trends_uint','vfs.fs.size[/,total]','avg','',1073741824],
['根分区平均剩余(单位G)','trends_uint','vfs.fs.size[/,free]','avg','',1073741824],
]
  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 = 'monitor'
  
        #获取IP信息:
        self.IpInfoList =self.getHostList()
  
      def getgroupid(self):
        '''根据zabbix组名获取该组所有IP'''
  
        #查询组ID:
        sql = '''select groupidfrom groups where name = '%s' ''' % self.groupname
        self.cursor.execute(sql)
        groupid =self.cursor.fetchone()['groupid']
        return groupid
  
      def gethostid(self):
        #根据groupid查询该分组下面的所有主机ID(hostid):
        groupid =self.getgroupid()
        sql = '''select hostidfrom hosts_groups where groupid = %s''' % groupid
        self.cursor.execute(sql)
        hostlist = self.cursor.fetchall()
        return hostlist
  
      def getHostList(self):
        #生成IP信息字典:结构为{'119.146.207.19':{'hostid':10086L,},}
        hostlist =self.gethostid()
        IpInfoList = {}
        for i in hostlist:
              hostid = i['hostid']
              sql = '''select hostfrom hosts where status = 0 and hostid = %s''' % hostid
              ret =self.cursor.execute(sql)
              if ret:
                 IpInfoList] = {'hostid':hostid}
        return IpInfoList
  
      def getItemid(self,hostid,itemname):
        '''获取itemid'''
        sql = '''select itemidfrom items where hostid = %s and key_ = '%s' ''' % (hostid, itemname)
        ifself.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 andclock = %sand clock
页: [1]
查看完整版本: Zabbix 使用规范和生成报表