zabbix使用自动发现监控esxi的磁盘存储storage

zabbix使用自动发现监控esxi的磁盘存储storage

在任意一台可以访问vcenter的zabbix-agent服务器上添加exsi的磁盘监控模板即可

 

创建模板过程:

custom.esxi.discovery

画图:

注意要选择浮点型,否则可能不支持

添加触发器:当磁盘使用率超过85%时触发报警

自动发现的脚本:
[root@wondershareID_web03:~]# cat /usr/local/zabbix_agents_3.2.0/scripts/zabbix_esxi_storage.py
#!/usr/bin/python3
#coding:utf-8
#Author: ziming

"""
只用于模拟开发功能测试
"""
from pyVmomi import vim
from pyVim.connect import SmartConnect, Disconnect, SmartConnectNoSSL
import sys
import atexit
import argparse
from optparse import OptionParser
import json


class Exsi(object):

    # 获取vcenter的相关参数
    def __init__(self, host = '192.168.254.69', port=443, user = 'zabbixmonitor', password='zabbixmonitor'):
        self._host = host
        self._port = port
        self._user = user
        self._password = password

    def get_obj(self,content, vimtype, name=None):
        '''
        列表返回,name 可以指定匹配的对象
        '''
        container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
        obj = [ view for view in container.view]
        return obj

    def get_conn(self):
        # 连接vcenter
        si = SmartConnectNoSSL(
                host=self._host,
                user=self._user,
                pwd=self._password,
                port=self._port)
        # disconnect this thing
        atexit.register(Disconnect, si)
        content = si.RetrieveContent()
        return content

    # 获取exsi的信息ip,磁盘使用情况等
    def get_esxi_host(self):
        esxi_host = {}
        content = self.get_conn()
        esxi_obj = self.get_obj(content, [vim.HostSystem])
        for esxi in esxi_obj:
            esxi_host[esxi.name] = {'datastore':{}}

            # for ds in esxi.datastore:
            #     esxi_host[esxi.name]['datastore'][ds.name] = {}
            #     esxi_host[esxi.name]['datastore'][ds.name]['总容量(G)'] = int((ds.summary.capacity)/1024/1024/1024)
            #     esxi_host[esxi.name]['datastore'][ds.name]['空闲容量(G)'] = int((ds.summary.freeSpace)/1024/1024/1024)
            #     esxi_host[esxi.name]['datastore'][ds.name]['类型'] = (ds.summary.type)

            total_capacity = 0
            free_capacity = 0
            for ds in esxi.datastore:
                # esxi_host[esxi.name]['datastore'][ds.name] = {}
                esxi_host[esxi.name] = {}
                total_capacity += int((ds.summary.capacity)/1024/1024/1024)
                free_capacity += int((ds.summary.freeSpace)/1024/1024/1024)

            esxi_host[esxi.name]['sotrage_used_percent'] = 100*(total_capacity - free_capacity)/total_capacity
        # print(esxi_host)
        return esxi_host

    # 获取exsi的host即ip列表
    def get_esxi_iplist(self):
        esxi_host = self.get_esxi_host()
        data = list()
        for exsi_ip in esxi_host:
            data.append({"{#EXSI_IP}": exsi_ip})

        return json.dumps({'data': data}, sort_keys=True, indent=7, separators=(",",":"))

    # 获取指定esxi的磁盘信息
    def get_esxi_storageinfo(self, exsi_ip):
        esxi_host = self.get_esxi_host()

        return esxi_host[exsi_ip]['sotrage_used_percent']


def main():
    try:

        usage = "usage: %prog [options]\ngGet exsi Stat"
        parser = OptionParser(usage)
        
        # 接收参数
        parser.add_option("-l", "--list",  
                          action="store_true", dest="is_list", default=False,  help="if list all exsi ip")
        
        parser.add_option("--ip", "--ipaddr", 
                          action="store", dest="ipaddr", type="string", 
                          default="192.168.254.56", help="execute 'exsi info' to see more infomation")
        
        
        (options, args) = parser.parse_args()
        if 1 >= len(sys.argv):
            parser.print_help()
            return
        
        #exsi_ins = Exsi(options.ipaddr)
        exsi_ins = Exsi()
        if options.is_list == True:
            print(exsi_ins.get_esxi_iplist())
            return

        # print(redis.ins.get_esxi_storageinfo(optins.ip, exsi_ip = ip))
        print(exsi_ins.get_esxi_storageinfo(options.ipaddr))

    except Exception as expt:
        import traceback
        tb = traceback.format_exc()
        print(tb)


if __name__ == '__main__':
    main()

 配置文件

# vim /usr/local/zabbix_agents_3.2.0/conf/zabbix_agentd.conf

UserParameter=custom.esxi.discovery, /usr/bin/python3 /usr/local/zabbix_agents_3.2.0/scripts/zabbix_esxi_storage.py --list
UserParameter=custom.esxi.item[*],/usr/bin/python3 /usr/local/zabbix_agents_3.2.0/scripts/zabbix_esxi_storage.py --ip $1

posted @ 2018-09-13 22:37  reblue520  阅读(861)  评论(0编辑  收藏  举报