关于python3.x:python3-pyVmomi-操作-获取-vcenter-下数据中心-集群-主机-存储-虚拟机等信息

39次阅读

共计 6876 个字符,预计需要花费 18 分钟才能阅读完成。

vim 报错能够疏忽

#!/usr/bin/python3
# coding:utf-8

from pyVmomi import vim
from pyVim.connect import SmartConnect, Disconnect, SmartConnectNoSSL
import atexit


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


def get_esxi_all_info():
    esxi_host = {}
    esxi_list = []
    # connect this thing
    si = SmartConnectNoSSL(
        host='10.23.1xx.xx',
        user='rexxxl',
        pwd='xxxxx',
        port='443')
    # disconnect this thing
    atexit.register(Disconnect, si)
    content = si.RetrieveContent()
    esxi_obj = get_obj(content, [vim.HostSystem])

    for esxi in esxi_obj:
        esxi_host[esxi.name] = {'esxi_info': {}, 'datastore': {}, 'network': {}, 'vm': {}}
        esxi_host[esxi.name]['esxi_info']['厂商'] = esxi.summary.hardware.vendor
        esxi_host[esxi.name]['esxi_info']['型号'] = esxi.summary.hardware.model
        for i in esxi.summary.hardware.otherIdentifyingInfo:
            if isinstance(i, vim.host.SystemIdentificationInfo):
                esxi_host[esxi.name]['esxi_info']['SN'] = i.identifierValue
        esxi_host[esxi.name]['esxi_info']['处理器'] = '数量:%s 核数:%s 线程数:%s 频率:%s(%s)' % (esxi.summary.hardware.numCpuPkgs,
                                                                                      esxi.summary.hardware.numCpuCores,
                                                                                      esxi.summary.hardware.numCpuThreads,
                                                                                      esxi.summary.hardware.cpuMhz,
                                                                                      esxi.summary.hardware.cpuModel)
        try:
            esxi_host[esxi.name]['esxi_info']['处理器使用率'] = '%.1f%%' % (esxi.summary.quickStats.overallCpuUsage / (esxi.summary.hardware.numCpuPkgs * esxi.summary.hardware.numCpuCores * esxi.summary.hardware.cpuMhz) * 100)
        except Exception as e:
            esxi_host[esxi.name]['esxi_info']['处理器使用率'] = None
            print(e)

        esxi_host[esxi.name]['esxi_info']['内存 (MB)'] = esxi.summary.hardware.memorySize / 1024 / 1024
        try:
            esxi_host[esxi.name]['esxi_info']['可用内存 (MB)'] = '%.1f MB' % ((esxi.summary.hardware.memorySize / 1024 / 1024) - esxi.summary.quickStats.overallMemoryUsage)
        except Exception as e:
            esxi_host[esxi.name]['esxi_info']['可用内存 (MB)'] = None
            print(e)
        try:
            esxi_host[esxi.name]['esxi_info']['内存使用率'] = '%.1f%%' % ((esxi.summary.quickStats.overallMemoryUsage / (esxi.summary.hardware.memorySize / 1024 / 1024)) * 100)
        except Exception as e:
            esxi_host[esxi.name]['esxi_info']['内存使用率'] = None
            print(e)
        esxi_host[esxi.name]['esxi_info']['零碎'] = esxi.summary.config.product.fullName

        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)
        for nt in esxi.network:
            esxi_host[esxi.name]['network'][nt.name] = {}
            esxi_host[esxi.name]['network'][nt.name]['标签 ID'] = nt.name
        for vm in esxi.vm:
            esxi_host[esxi.name]['vm'][vm.name] = {}
            esxi_host[esxi.name]['vm'][vm.name]['电源状态'] = vm.runtime.powerState
            esxi_host[esxi.name]['vm'][vm.name]['CPU( 内核总数)'] = vm.config.hardware.numCPU
            esxi_host[esxi.name]['vm'][vm.name]['内存 ( 总数 MB)'] = vm.config.hardware.memoryMB
            esxi_host[esxi.name]['vm'][vm.name]['零碎信息'] = vm.config.guestFullName
            if vm.guest.ipAddress:
                esxi_host[esxi.name]['vm'][vm.name]['IP'] = vm.guest.ipAddress
            else:
                esxi_host[esxi.name]['vm'][vm.name]['IP'] = '服务器须要开机后才能够获取'

            for d in vm.config.hardware.device:
                if isinstance(d, vim.vm.device.VirtualDisk):
                    esxi_host[esxi.name]['vm'][vm.name][d.deviceInfo.label] = str((d.capacityInKB) / 1024 / 1024) + 'GB'
    print("####esxi_host", esxi_host)

    return esxi_host


def get_esxi_list():
    esxi_list = []

    si = SmartConnectNoSSL(
        host='10.23.1xx.xx',
        user='rexxxl',
        pwd='xxxxx',
        port='443')
    atexit.register(Disconnect, si)
    content = si.RetrieveContent()
    esxi_obj = get_obj(content, [vim.HostSystem])

    # print(esxi_obj[1].name) -- 10.23.140.242
    for h in esxi_obj:
        print('ESXI IP:', h.name)
        esxi_list.append(h.name)

    return esxi_list


def get_esxi_single_info(esxi_ip):
    esxi_host = {}
    si = SmartConnectNoSSL(
        host='10.23.1xx.xx',
        user='rexxxl',
        pwd='xxxxx',
        port='443')
    atexit.register(Disconnect, si)
    content = si.RetrieveContent()
    esxi_obj = get_obj(content, [vim.HostSystem])

    for esxi in esxi_obj:
        if esxi.name == esxi_ip:
            esxi_host[esxi.name] = {'esxi_info': {}, 'datastore': {}, 'network': {}, 'vm': {}}
            esxi_host[esxi.name]['esxi_info']['厂商'] = esxi.summary.hardware.vendor
            esxi_host[esxi.name]['esxi_info']['型号'] = esxi.summary.hardware.model
            for i in esxi.summary.hardware.otherIdentifyingInfo:
                if isinstance(i, vim.host.SystemIdentificationInfo):
                    esxi_host[esxi.name]['esxi_info']['SN'] = i.identifierValue
            esxi_host[esxi.name]['esxi_info']['处理器'] = '数量:%s 核数:%s 线程数:%s 频率:%s(%s)' % (
                esxi.summary.hardware.numCpuPkgs,
                esxi.summary.hardware.numCpuCores,
                esxi.summary.hardware.numCpuThreads,
                esxi.summary.hardware.cpuMhz,
                esxi.summary.hardware.cpuModel)
            try:
                esxi_host[esxi.name]['esxi_info']['处理器使用率'] = '%.1f%%' % (esxi.summary.quickStats.overallCpuUsage / (esxi.summary.hardware.numCpuPkgs * esxi.summary.hardware.numCpuCores * esxi.summary.hardware.cpuMhz) * 100)
            except Exception as e:
                esxi_host[esxi.name]['esxi_info']['处理器使用率'] = None
                print(e)

            esxi_host[esxi.name]['esxi_info']['内存 (MB)'] = esxi.summary.hardware.memorySize / 1024 / 1024
            try:
                esxi_host[esxi.name]['esxi_info']['可用内存 (MB)'] = '%.1f MB' % ((esxi.summary.hardware.memorySize / 1024 / 1024) - esxi.summary.quickStats.overallMemoryUsage)
            except Exception as e:
                esxi_host[esxi.name]['esxi_info']['可用内存 (MB)'] = None
                print(e)
            try:
                esxi_host[esxi.name]['esxi_info']['内存使用率'] = '%.1f%%' % ((esxi.summary.quickStats.overallMemoryUsage / (esxi.summary.hardware.memorySize / 1024 / 1024)) * 100)
            except Exception as e:
                esxi_host[esxi.name]['esxi_info']['内存使用率'] = None
                print(e)
            esxi_host[esxi.name]['esxi_info']['零碎'] = esxi.summary.config.product.fullName

            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)
            for nt in esxi.network:
                esxi_host[esxi.name]['network'][nt.name] = {}
                esxi_host[esxi.name]['network'][nt.name]['标签 ID'] = nt.name
            for vm in esxi.vm:
                esxi_host[esxi.name]['vm'][vm.name] = {}
                esxi_host[esxi.name]['vm'][vm.name]['电源状态'] = vm.runtime.powerState
                esxi_host[esxi.name]['vm'][vm.name]['CPU( 内核总数)'] = vm.config.hardware.numCPU
                esxi_host[esxi.name]['vm'][vm.name]['内存 ( 总数 MB)'] = vm.config.hardware.memoryMB
                esxi_host[esxi.name]['vm'][vm.name]['零碎信息'] = vm.config.guestFullName
                if vm.guest.ipAddress:
                    esxi_host[esxi.name]['vm'][vm.name]['IP'] = vm.guest.ipAddress
                else:
                    esxi_host[esxi.name]['vm'][vm.name]['IP'] = '服务器须要开机后才能够获取'

                for d in vm.config.hardware.device:
                    if isinstance(d, vim.vm.device.VirtualDisk):
                        esxi_host[esxi.name]['vm'][vm.name][d.deviceInfo.label] = str((d.capacityInKB) / 1024 / 1024) + 'GB'
    print("####esxi_host", esxi_host)

    return esxi_host



def get_esxi_datacenter():
    si = SmartConnectNoSSL(
        host='10.23.1xx.xx',
        user='rexxxl',
        pwd='xxxxx',
        port='443')
    atexit.register(Disconnect, si)
    content = si.RetrieveContent()
    esxi_obj = get_obj(content, [vim.Datacenter])
    #print(esxi_obj[0].hostFolder.childEntity[0].name)
    data = []
    for i in esxi_obj:
        esxi_list = []
        datacenter_data = {'datacenter_name': i.name}
        for j in i.hostFolder.childEntity:
            esxi_list.append(j.name)
        datacenter_data['host_list'] = esxi_list
        data.append(datacenter_data)

    return data

正文完
 0