Properly handle exceptions getting VM stats

This commit is contained in:
Joshua Boniface 2021-08-19 12:36:31 -04:00
parent dae67a1b7b
commit 42e776fac1
1 changed files with 41 additions and 37 deletions

View File

@ -1675,11 +1675,7 @@ def collect_vm_stats(queue):
domain_memory_stats = domain.memoryStats() domain_memory_stats = domain.memoryStats()
domain_cpu_stats = domain.getCPUStats(True)[0] domain_cpu_stats = domain.getCPUStats(True)[0]
except Exception as e: except Exception as e:
if debug: logger.out("Failed getting VM information for {}: {}".format(domain.name(), e), state='w', prefix='vm-thread')
try:
logger.out("Failed getting VM information for {}: {}".format(domain.name(), e), state='d', prefix='vm-thread')
except Exception:
pass
continue continue
# Ensure VM is present in the domain_list # Ensure VM is present in the domain_list
@ -1689,42 +1685,50 @@ def collect_vm_stats(queue):
if debug: if debug:
logger.out("Getting disk statistics for VM {}".format(domain_name), state='d', prefix='vm-thread') logger.out("Getting disk statistics for VM {}".format(domain_name), state='d', prefix='vm-thread')
domain_disk_stats = [] domain_disk_stats = []
for disk in tree.findall('devices/disk'): try:
disk_name = disk.find('source').get('name') for disk in tree.findall('devices/disk'):
if not disk_name: disk_name = disk.find('source').get('name')
disk_name = disk.find('source').get('file') if not disk_name:
disk_stats = domain.blockStats(disk.find('target').get('dev')) disk_name = disk.find('source').get('file')
domain_disk_stats.append({ disk_stats = domain.blockStats(disk.find('target').get('dev'))
"name": disk_name, domain_disk_stats.append({
"rd_req": disk_stats[0], "name": disk_name,
"rd_bytes": disk_stats[1], "rd_req": disk_stats[0],
"wr_req": disk_stats[2], "rd_bytes": disk_stats[1],
"wr_bytes": disk_stats[3], "wr_req": disk_stats[2],
"err": disk_stats[4] "wr_bytes": disk_stats[3],
}) "err": disk_stats[4]
})
except Exception as e:
logger.out("Failed to get disk stats for VM {}: {}".format(domain_name, e), state='w', prefix='vm-thread')
continue
if debug: if debug:
logger.out("Getting network statistics for VM {}".format(domain_name), state='d', prefix='vm-thread') logger.out("Getting network statistics for VM {}".format(domain_name), state='d', prefix='vm-thread')
domain_network_stats = [] domain_network_stats = []
for interface in tree.findall('devices/interface'): try:
interface_type = interface.get('type') for interface in tree.findall('devices/interface'):
if interface_type not in ['bridge']: interface_type = interface.get('type')
continue if interface_type not in ['bridge']:
interface_name = interface.find('target').get('dev') continue
interface_bridge = interface.find('source').get('bridge') interface_name = interface.find('target').get('dev')
interface_stats = domain.interfaceStats(interface_name) interface_bridge = interface.find('source').get('bridge')
domain_network_stats.append({ interface_stats = domain.interfaceStats(interface_name)
"name": interface_name, domain_network_stats.append({
"bridge": interface_bridge, "name": interface_name,
"rd_bytes": interface_stats[0], "bridge": interface_bridge,
"rd_packets": interface_stats[1], "rd_bytes": interface_stats[0],
"rd_errors": interface_stats[2], "rd_packets": interface_stats[1],
"rd_drops": interface_stats[3], "rd_errors": interface_stats[2],
"wr_bytes": interface_stats[4], "rd_drops": interface_stats[3],
"wr_packets": interface_stats[5], "wr_bytes": interface_stats[4],
"wr_errors": interface_stats[6], "wr_packets": interface_stats[5],
"wr_drops": interface_stats[7] "wr_errors": interface_stats[6],
}) "wr_drops": interface_stats[7]
})
except Exception as e:
logger.out("Failed to get network stats for VM {}: {}".format(domain_name, e), state='w', prefix='vm-thread')
continue
# Create the final dictionary # Create the final dictionary
domain_stats = { domain_stats = {