pyvmomi操作vmware(三)获取一些监控信息

    def collect_resource_io_put(self, vm_obj, arguments=None, counter_id=148, ):
        max_count = 10
        if arguments:
            max_count = arguments
        end_time = datetime.datetime.now()
        end_time_second = change_time_second(end_time)
        if end_time_second % 20000 >= 10000:
            end_time_second = ((end_time_second / 20000) * 20000 + 10000) / 1000
        else:
            end_time_second = ((end_time_second / 20000) * 20000 - 10000) / 1000
        end_time = datetime.datetime.utcfromtimestamp(end_time_second)
        start_time = end_time - datetime.timedelta(hours=1)

        content = self.get_content()

        content.perfManager.ResetCounterLevelMapping(counters=[counter_id])

        metric_id = vim.PerformanceManager.MetricId(counterId=counter_id,
                                                    instance="*")
        query_spec = vim.PerformanceManager.QuerySpec(maxSample=max_count,
                                                      entity=vm_obj,
                                                      metricId=[metric_id],
                                                      startTime=start_time,
                                                      endTime=end_time,
                                                      intervalId=20,
                                                      format='normal')

        result = content.perfManager.QueryPerf(querySpec=[query_spec])
        resource_io_put = []
        if result:
            for i in range(len(result[0].value)):
                child_resource = []
                for j in range(max_count):
                    value = result[0].value[i].value[j]
                    if value == -1:
                        value = None
                    child_resource.append({
                        'time_stamp': change_time_second(
                            result[0].sampleInfo[j].timestamp),
                        'value': value
                    })
                resource_io_put.append({
                    'name': result[0].value[i].id.instance,
                    'child_resource': child_resource
                })
        time.sleep(0.1)
        return resource_io_put

    def collect_host_resource_format(self, host_obj, name, resource):
        result = []
        if name == 'nic':
            for child_nic in resource:
                if child_nic['name']:
                    result.append(child_nic)
        elif name == 'disk':
            host_stores = host_obj.datastore
            for child_disk in resource:
                if child_disk['name']:
                    for child_store in host_stores:
                        if hasattr(child_store.info, 'vmfs'):
                            try:
                                if child_store.info.vmfs.extent[0].diskName == \
                                        child_disk['name']:
                                    name = child_store.name
                                    name = re.sub('%25', '%', name)
                                    result.append(
                                        {
                                            'child_resource': child_disk[
                                                'child_resource'],
                                            'name': name
                                        }
                                    )
                            except BaseException as e:
                                pass
        return result

    def collect_nic_input(self, host_obj, arguments=None):

        result = self.collect_resource_io_put(host_obj, arguments, 148)
        if result:
            result = self.collect_host_resource_format(host_obj, 'nic', result)
        else:
            result = self.collect_host_resource_make_zero(host_obj, 'nic',
                                                          arguments)
        return result

    def collect_nic_output(self, host_obj, arguments=None):

        result = self.collect_resource_io_put(host_obj, arguments, 149)
        if result:
            result = self.collect_host_resource_format(host_obj, 'nic', result)
        else:
            result = self.collect_host_resource_make_zero(host_obj, 'nic',
                                                          arguments)
        return result

    def collect_disk_input(self, host_obj, arguments=None):

        result = self.collect_resource_io_put(host_obj, arguments, 131)
        if result:
            result = self.collect_host_resource_format(host_obj, 'nic', result)
        else:
            result = self.collect_host_resource_make_zero(host_obj, 'nic',
                                                          arguments)
        return result

    def collect_disk_output(self, host_obj, arguments=None):

        result = self.collect_resource_io_put(host_obj, arguments, 130)
        if result:
            result = self.collect_host_resource_format(host_obj, 'nic', result)
        else:
            result = self.collect_host_resource_make_zero(host_obj, 'nic',
                                                          arguments)
        return result

pyvmomi操作vmware(三)获取一些监控信息pyvmomi操作vmware(三)获取一些监控信息 Aerkui 发布了22 篇原创文章 · 获赞 0 · 访问量 326 私信 关注
上一篇:java8新特性lambda和Stream新手springboot案例2020年新版


下一篇:谷粒商城day58-商品服务-API-新增商品-获取分类关联的品牌+获取分类下所有分组以及属性