make list of procs to facilitate using threadpools
This commit is contained in:
parent
5c39259f0b
commit
eea59a20b3
@ -121,61 +121,66 @@ def read_interface_stats(ifname):
|
|||||||
return stats
|
return stats
|
||||||
|
|
||||||
def collect_kvm_metrics():
|
def collect_kvm_metrics():
|
||||||
for proc in psutil.process_iter(['pid', 'name', 'cmdline', 'cpu_percent', 'memory_percent', 'num_threads']):
|
procs = [
|
||||||
|
(
|
||||||
|
# proc object
|
||||||
|
proc,
|
||||||
|
# cmdline list
|
||||||
|
proc.cmdline(),
|
||||||
|
# VMID
|
||||||
|
flag_to_label_value(proc.cmdline(),"-id")
|
||||||
|
)
|
||||||
|
for proc in psutil.process_iter(['pid', 'name', 'cmdline', 'cpu_percent', 'memory_percent', 'num_threads'])
|
||||||
|
if proc.info['name'] == 'kvm'
|
||||||
|
]
|
||||||
|
for proc, cmdline, id in procs:
|
||||||
|
# Extract vm labels from cmdline
|
||||||
|
info_label_dict = {get_label_name(l): flag_to_label_value(cmdline,l) for l in label_flags}
|
||||||
|
info_label_dict['pid']=str(proc.pid)
|
||||||
|
logging.debug(f"got PID: {proc.pid}")
|
||||||
|
info_dict["kvm"].info(info_label_dict)
|
||||||
|
|
||||||
if 'kvm' == proc.info['name']:
|
d = {
|
||||||
cmdline = proc.cmdline()
|
"kvm_vcores": flag_to_label_value(cmdline,"-smp"),
|
||||||
id = flag_to_label_value(cmdline,"-id")
|
"kvm_maxmem": int(flag_to_label_value(cmdline,"-m"))*1024,
|
||||||
|
"kvm_memory_percent": proc.info['memory_percent'],
|
||||||
|
"kvm_threads": proc.info['num_threads'],
|
||||||
|
}
|
||||||
|
|
||||||
# Extract vm labels from cmdline
|
for k, v in d.items():
|
||||||
info_label_dict = {get_label_name(l): flag_to_label_value(cmdline,l) for l in label_flags}
|
gauge_dict[k].labels(id=id).set(v)
|
||||||
info_label_dict['pid']=str(proc.pid)
|
logging.debug(f"gauge_dict[{k}].labels(id={id}).set({v})")
|
||||||
logging.debug(f"got PID: {proc.pid}")
|
|
||||||
info_dict["kvm"].info(info_label_dict)
|
|
||||||
|
|
||||||
d = {
|
cpu_times = proc.cpu_times()
|
||||||
"kvm_vcores": flag_to_label_value(cmdline,"-smp"),
|
for mode in ['user', 'system', 'iowait']:
|
||||||
"kvm_maxmem": int(flag_to_label_value(cmdline,"-m"))*1024,
|
gauge_dict["kvm_cpu"].labels(id=id, mode=mode).set(getattr(cpu_times, mode))
|
||||||
"kvm_memory_percent": proc.info['memory_percent'],
|
|
||||||
"kvm_threads": proc.info['num_threads'],
|
|
||||||
}
|
|
||||||
|
|
||||||
for k, v in d.items():
|
io = proc.io_counters()
|
||||||
gauge_dict[k].labels(id=id).set(v)
|
for io_type, attr in itertools.product(['read', 'write'], ['count', 'bytes', 'chars']):
|
||||||
logging.debug(f"gauge_dict[{k}].labels(id={id}).set({v})")
|
gauge = globals()["gauge_dict"][f'kvm_io_{io_type}_{attr}']
|
||||||
|
gauge.labels(id=id).set(getattr(io, f"{io_type}_{attr}"))
|
||||||
|
|
||||||
cpu_times = proc.cpu_times()
|
for type in [ "voluntary", "involuntary" ]:
|
||||||
for mode in ['user', 'system', 'iowait']:
|
gauge_dict["kvm_ctx_switches"].labels(id=id, type=type).set(getattr(proc.num_ctx_switches(),type))
|
||||||
gauge_dict["kvm_cpu"].labels(id=id, mode=mode).set(getattr(cpu_times, mode))
|
|
||||||
|
|
||||||
io = proc.io_counters()
|
memory_metrics = get_memory_info(proc.pid) # Assuming proc.pid gives you the PID of the process
|
||||||
for io_type, attr in itertools.product(['read', 'write'], ['count', 'bytes', 'chars']):
|
for key, value in memory_metrics.items():
|
||||||
gauge = globals()["gauge_dict"][f'kvm_io_{io_type}_{attr}']
|
gauge_dict["kvm_memory_extended"].labels(id=id, type=key).set(value)
|
||||||
gauge.labels(id=id).set(getattr(io, f"{io_type}_{attr}"))
|
|
||||||
|
|
||||||
for type in [ "voluntary", "involuntary" ]:
|
for nic_info in extract_nic_info_from_monitor(id):
|
||||||
gauge_dict["kvm_ctx_switches"].labels(id=id, type=type).set(getattr(proc.num_ctx_switches(),type))
|
queues = nic_info["queues"]
|
||||||
|
del nic_info["queues"]
|
||||||
memory_metrics = get_memory_info(proc.pid) # Assuming proc.pid gives you the PID of the process
|
nic_labels = {"id": id, "ifname": nic_info["ifname"]}
|
||||||
for key, value in memory_metrics.items():
|
prom_nic_info = create_or_get_info("kvm_nic", nic_labels.keys())
|
||||||
gauge_dict["kvm_memory_extended"].labels(id=id, type=key).set(value)
|
prom_nic_info.labels(**nic_labels).info({k: v for k, v in nic_info.items() if k not in nic_labels.keys()})
|
||||||
|
|
||||||
for nic_info in extract_nic_info_from_monitor(id):
|
|
||||||
queues = nic_info["queues"]
|
|
||||||
del nic_info["queues"]
|
|
||||||
nic_labels = {"id": id, "ifname": nic_info["ifname"]}
|
|
||||||
prom_nic_info = create_or_get_info("kvm_nic", nic_labels.keys())
|
|
||||||
prom_nic_info.labels(**nic_labels).info({k: v for k, v in nic_info.items() if k not in nic_labels.keys()})
|
|
||||||
|
|
||||||
gauge_dict["kvm_nic_queues"].labels(**nic_labels).set(queues)
|
|
||||||
|
|
||||||
interface_stats = read_interface_stats(nic_info["ifname"])
|
|
||||||
for filename, value in interface_stats.items():
|
|
||||||
metric_name = f"kvm_nic_{filename}"
|
|
||||||
gauge = create_or_get_gauge(metric_name, nic_labels.keys())
|
|
||||||
gauge.labels(**nic_labels).set(value)
|
|
||||||
|
|
||||||
|
gauge_dict["kvm_nic_queues"].labels(**nic_labels).set(queues)
|
||||||
|
|
||||||
|
interface_stats = read_interface_stats(nic_info["ifname"])
|
||||||
|
for filename, value in interface_stats.items():
|
||||||
|
metric_name = f"kvm_nic_{filename}"
|
||||||
|
gauge = create_or_get_gauge(metric_name, nic_labels.keys())
|
||||||
|
gauge.labels(**nic_labels).set(value)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description='PVE metrics exporter for Prometheus')
|
parser = argparse.ArgumentParser(description='PVE metrics exporter for Prometheus')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user