美文网首页基于django业务的存储光交管理系统的的设计-菜鸟开发日记
django存储光交业务管理系统-外篇-vmware虚拟化的采集

django存储光交业务管理系统-外篇-vmware虚拟化的采集

作者: python菜鸟 | 来源:发表于2018-08-29 11:21 被阅读21次
    虚拟化前端

    通过采集器采集vmware的数据到mongodb数据库,在通过前端进行展示及显示。

    虚拟机,存储,节点之间的关联

    虚拟机实例Instance UUID,磁盘uuid,以及esxi节点的IP来确定。每个信息之间都会打一个tag标签。标签也是独一无二。标签的作用是为后期采集性能信息使用。

    该脚本功能主要是采集vmwre的虚拟机,存储,及节点的信息。
    # -*- coding: utf-8 -*-
    from __future__ import absolute_import, unicode_literals
    import atexit
    import requests
    from pyVmomi import vim
    from pyVim import connect
    import json
    def sizeof_fmt(num):
        """
    
    ###### 通过写入mongodb数据库
        Returns the human readable version of a file size
    
        :param num:
        :return:
        """
        for item in ['bytes', 'KB', 'MB', 'GB']:
            if num < 1024.0:
                return "%3.1f%s" % (num, item)
            num /= 1024.0
        return "%3.1f%s" % (num, 'TB')
    class vsphere:
        def __init__(self,host,user,password,port=443):
            """
            初始化实例,
            :param host:vc主机名
            :param user: vc用户
            :param password: vc密码
            :param port: vc端口,默认443
            """
            self.service_instance = connect.SmartConnectNoSSL(host=host,user = user,pwd = password,port = int(port))
            atexit.register(connect.Disconnect, self.service_instance)
            self.content = self.service_instance.RetrieveContent()
        #v1.0
        def getvm(self):
            """
            {'bootTime': None,
            'Bios UUID': '423f330a-a578-2c6e-3fb1-dc514a38184d',
            'Annotation': '',
            'Name': 'GZGL_10.239.37.57',
            'VMware-tools': 'toolsOld',
            'Template': False,
            'memorySizeMB': 16384,
            'numdisk': 2,
            'Path': '[X10_K01_HITACHI_Cluster03_LUN15] GZGL_10.239.37.57/GZGL_10.239.37.57.vmx',
            'IP': 'toolsOld',
            'Instance UUID':
            '503fd0c6-1379-f1d0-c2ce-2a6ca446b34c',
            'Guest': 'Red Hat Enterprise Linux 6 (64-bit)',
            'State': 'poweredOn',
            'numCpu': 4}
            :return: list[dict]
            """
            container = self.content.rootFolder  # starting point to look into
            viewType = [vim.VirtualMachine]  # object types to look for
            recursive = True  # whether we should look into it recursively
            containerView = self.content.viewManager.CreateContainerView(
                container, viewType, recursive)
            children = containerView.view
            data = []
            for child in children:
                summary = child.summary
                tempdata = {
                    "Name":summary.config.name,
                    "vm_tag":str(summary.vm),
                    "Template":summary.config.template,
                    "Path":summary.config.vmPathName,
                    "Guest":summary.config.guestFullName,
                    "Instance UUID":summary.config.instanceUuid,
                    "Bios UUID": summary.config.uuid,
                    "Annotation":summary.config.annotation,
                    "State":summary.runtime.powerState,
                    "VMware-tools":summary.guest.toolsStatus,
                    "IP":summary.guest.ipAddress,
                    "memorySizeMB":summary.config.memorySizeMB,
                    "numCpu":summary.config.numCpu,
                    "numdisk":summary.config.numVirtualDisks,
                    "bootTime":summary.runtime.bootTime,
                    "exsi_tag":str(summary.runtime.host),
                    "exsi_ip":summary.runtime.host.name,
                    "storage_committed":summary.storage.committed,
                    "storage_uncommitted":summary.storage.uncommitted,
                    "storage_unshared":summary.storage.unshared,
                    "quickStats_status":summary.quickStats.guestHeartbeatStatus,
                    "quickStats_uptimeSeconds":summary.quickStats.uptimeSeconds,
                    "quickStats_hostMemoryUsage":summary.quickStats.hostMemoryUsage,
                }
                data.append(tempdata)
            return data
    
        def getvm_uuid(self):
            container = self.content.rootFolder  # starting point to look into
            viewType = [vim.VirtualMachine]  # object types to look for
            recursive = True  # whether we should look into it recursively
            containerView = self.content.viewManager.CreateContainerView(
                container, viewType, recursive)
            children = containerView.view
            data = []
            for child in children:
                summary = child.summary
                tempdata = {
                    "Instance UUID":summary.config.instanceUuid,
                }
                data.append(tempdata)
            return data
        #废弃
        def getexsihost_storage(self):
            """
            {
                ip:
                    磁盘名称。
                        {磁盘信息}
    
            }
            :return:dict
            """
            objview = self.content.viewManager.CreateContainerView(self.content.rootFolder,
                                                              [vim.HostSystem],
                                                              True)
            esxi_hosts = objview.view
            objview.Destroy()
            datastores = []
            for esxi_host in esxi_hosts:
                # All Filesystems on ESXi host
                # print(esxi_host)
                storage_system = esxi_host.configManager.storageSystem
                host_file_sys_vol_mount_info = storage_system.fileSystemVolumeInfo.mountInfo
                datastore_dict = {}
                # Map all filesystems
    
                for host_mount_info in host_file_sys_vol_mount_info:
                    # Extract only VMFS volumes
    
                    if host_mount_info.volume.type == "VMFS":
                        extents = host_mount_info.volume.extent
                        datastore_details = {
                            'uuid': host_mount_info.volume.uuid,
                            'capacity': host_mount_info.volume.capacity,
                            'vmfs_version': host_mount_info.volume.version,
                            'local': host_mount_info.volume.local,
                            'ssd': host_mount_info.volume.ssd,
                        }
                        extent_arr = []
                        extent_count = 0
                        for extent in extents:
                            # print("{}\t{}\t".format("Extent[" + str(extent_count) + "]:",extent.diskName))
                            extent_count += 1
                            # create an array of the devices backing the given
                            # datastore
                            extent_arr.append(extent.diskName)
                            # add the extent array to the datastore info
                            datastore_details['extents'] = extent_arr
                            # associate datastore details with datastore name
                            # datastore_dict[host_mount_info.volume.name] = datastore_details
                            datastore_details["storagename"] = host_mount_info.volume.name
                            datastore_details["exsi_host"] = esxi_host.name
    
                # associate ESXi host with the datastore it sees
                # datastores[esxi_host.name] = datastore_dict
                    datastores.append(datastore_details)
    
            return datastores
        #v1.0
        # def getstorage01(self):
        #     """
        #     "Capacity(GB)": "1.1TB",
        #       "URL": "ds:///vmfs/volumes/5b126d7e-fbee7582-8d66-5c546d5798c3/",
        #       "VmNum": "0",
        #       "hostNum": "1",
        #       "free cap(GB)": "1.1TB",
        #       "Name": "datastore1 (25)"
        #       完成
        #     :return:[{}]
        #     """
        #     ds_obj_list = self.content.viewManager.CreateContainerView(self.content.rootFolder,
        #                                                       [vim.Datastore],
        #                                                       True)
        #     datastore = []
        #     for ds in ds_obj_list.view:
        #         summary = ds.summary
        #         ds_capacity = summary.capacity
        #         ds_freespace = summary.freeSpace
        #         ipdata = []
        #         vmdata = []
        #         for i in ds.host:
        #             ipdata.append({str(i.key):i.key.name})
        #         for i in ds.vm:
        #             vmdata.append({str(i):i.name})
        #         datadict  = {
        #             "Name":format(summary.name),
        #             "URL":format(summary.url),
        #             "Capacity":format(sizeof_fmt(ds_capacity)),
        #             "free cap":format(sizeof_fmt(ds_freespace)),
        #             "hostNum":format(len(ds.host)),
        #             "VmNum":format(len(ds.vm)),
        #             "host_tag":ipdata,
        #             "vm_tag":vmdata,
        #         }
        #         if datadict:
        #             datastore.append(datadict)
        #     return datastore
        def getstorage01(self):
            """
            "Capacity(GB)": "1.1TB",
              "URL": "ds:///vmfs/volumes/5b126d7e-fbee7582-8d66-5c546d5798c3/",
              "VmNum": "0",
              "hostNum": "1",
              "free cap(GB)": "1.1TB",
              "Name": "datastore1 (25)"
              完成
            :return:[{}]
            """
            ds_obj_list = self.content.viewManager.CreateContainerView(self.content.rootFolder,
                                                              [vim.Datastore],
                                                              True)
            datastore = []
            for ds in ds_obj_list.view:
                summary = ds.summary
                ds_capacity = summary.capacity
                ds_freespace = summary.freeSpace
                # ipdata = []
                # vmdata = []
                esxiip = []
                vmip = []
                for i in ds.host:
                    # ipdata.append({str(i.key):i.key.name})
                    esxiip.append({
                        "esxiip_tag":str(i.key),
                        "esxiip":i.key.name
                    })
                for i in ds.vm:
                    # vmdata.append({str(i):i.name})
                    vmip.append(
                        {
                            "vmip_tag":str(i),
                            "vmipname":i.name,
                        }
                    )
                datadict  = {
                    "Name":format(summary.name),
                    "URL":format(summary.url),
                    "Capacity":format(sizeof_fmt(ds_capacity)),
                    "free cap":format(sizeof_fmt(ds_freespace)),
                    "hostNum":format(len(ds.host)),
                    "VmNum":format(len(ds.vm)),
                    "esxiip":esxiip,
                    "vmip":vmip
                }
                if datadict:
                    datastore.append(datadict)
            return datastore
        #v1.0
        def gethost(self):
            """
            :return:list
            """
            objview = self.content.viewManager.CreateContainerView(self.content.rootFolder,
                                                              [vim.HostSystem],
                                                              True)
            esxi_hosts = objview.view
            objview.Destroy()
            datastores = []
            for esxi_host in esxi_hosts:
                systemInfo = esxi_host.hardware.systemInfo
                cpuInfo = esxi_host.hardware.cpuInfo
                storage_system = esxi_host.configManager.storageSystem
                host_file_sys_vol_mount_info = storage_system.fileSystemVolumeInfo.mountInfo
                data = {
                "esxi_host":esxi_host.name
                ,"esxi_tag":str(esxi_host)
                ,"vendor":systemInfo.vendor
                ,"model":systemInfo.model
                ,"host_uuid":systemInfo.uuid
                ,"numCpuPackages":cpuInfo.numCpuPackages
                ,"numCpuCores":cpuInfo.numCpuCores
                ,"numCpuThreads":cpuInfo.numCpuThreads
                ,"hz":cpuInfo.hz
                ,"memorySize":esxi_host.hardware.memorySize
                }
                datastores.append(data)
            return datastores
        #v1.0
        def getcluster(self):
            """
            获取集群下的IP信息
            :return:
            """
            objview = self.content.viewManager.CreateContainerView(self.content.rootFolder,
                                                              [vim.ClusterComputeResource],
                                                              True)
            datavcenter = {}
            for i in objview.view:
                datahost = []
                for k in i.host:
                    datahost.append({str(k):k.name})
                datavcenter[i.name] = datahost
            # print(json.dumps(datavcenter,indent=1))
            return datavcenter
    
    目前这个采集主要是为我的备份数据整理而生,目前虚拟化有2000多台,有3套备份系统,需要根据虚拟机的特性,使用哪套备份系统。
    通过选择及筛选

    通过指定虚拟机的备份厂商,给每个厂商建立一个账号,可以查询自己需要备份哪些虚拟机,备份成功后打上成功的标签。之后有空我会对接3套备份系统的数据库,通过自动化来处理是否成功备份。

    查看存储信息

    由于虚拟化节点太多,通过该界面,可以快速查看存储相关信息,抽空会写一个告警管理,对剩余容量太少的进行告警。


    存储相关信息
    查看节点信息

    有空写个详细的,这个比较简单。

    节点查询

    相关文章

      网友评论

        本文标题:django存储光交业务管理系统-外篇-vmware虚拟化的采集

        本文链接:https://www.haomeiwen.com/subject/tmnbdftx.html