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
Aerkui
发布了22 篇原创文章 · 获赞 0 · 访问量 326
私信
关注