[pve-devel] [PATCH manager v2 2/5] api2tools: extract stats: handle existence of new pve-{type}-9.0 data
Aaron Lauterer
a.lauterer at proxmox.com
Wed Jul 9 18:36:53 CEST 2025
We add a new function to handle different key names, as it would
otherwise become quite unreadable.
It checks which key format exists for the type and resource:
* the old pve2-{type} / pve2.3-vm
* the new pve-{type}-{version}
and will return the one that was found. Since we will only have one key
per resource, we can return on the first hit.
Signed-off-by: Aaron Lauterer <a.lauterer at proxmox.com>
---
Notes:
This patch also needs to be in PVE8 as otherwise those hosts won't be able
to extract stats from newer nodes -> question marks and missing info in
the GUI
changes since:
RFC:
* switch from pve9- to pve-{type}-9.0 schema
PVE/API2Tools.pm | 28 ++++++++++++++++++++++++----
1 file changed, 24 insertions(+), 4 deletions(-)
diff --git a/PVE/API2Tools.pm b/PVE/API2Tools.pm
index 1e235c47..08548524 100644
--- a/PVE/API2Tools.pm
+++ b/PVE/API2Tools.pm
@@ -41,6 +41,24 @@ sub get_hwaddress {
return $hwaddress;
}
+# each rrd key for a resource will only exist once. The key format might be different though. Therefore return on first hit
+sub get_rrd_key {
+ my ($rrd, $type, $id) = @_;
+
+ # check for old formats: pve2-{type}/{id}. For VMs and CTs the version number is different than for nodes and storages
+ if ($type ne "vm" && exists $rrd->{"pve2-${type}/${id}"}) {
+ return "pve2-${type}/${id}";
+ } elsif ($type eq "vm" && exists $rrd->{"pve2.3-${type}/${id}"}) {
+ return "pve2.3-${type}/${id}";
+ }
+
+ # if no old key has been found, we expect on in the newer format: pve-{type}-{version}/{id}
+ # We accept all new versions, as the expectation is that they are only allowed to add new colums as non-breaking change
+ for my $k (keys %$rrd) {
+ return $k if $k =~ m/^pve-\Q${type}\E-\d\d?.\d\/\Q${id}\E$/;
+ }
+}
+
sub extract_node_stats {
my ($node, $members, $rrd, $exclude_stats) = @_;
@@ -51,8 +69,8 @@ sub extract_node_stats {
status => 'unknown',
};
- if (my $d = $rrd->{"pve2-node/$node"}) {
-
+ my $key = get_rrd_key($rrd, "node", $node);
+ if (my $d = $rrd->{$key}) {
if (
!$members || # no cluster
($members->{$node} && $members->{$node}->{online})
@@ -96,8 +114,9 @@ sub extract_vm_stats {
};
my $d;
+ my $key = get_rrd_key($rrd, "vm", $vmid);
- if ($d = $rrd->{"pve2.3-vm/$vmid"}) {
+ if (my $d = $rrd->{$key}) {
$entry->{uptime} = ($d->[0] || 0) + 0;
$entry->{name} = $d->[1];
@@ -135,7 +154,8 @@ sub extract_storage_stats {
content => $content,
};
- if (my $d = $rrd->{"pve2-storage/$node/$storeid"}) {
+ my $key = get_rrd_key($rrd, "storage", "${node}/${storeid}");
+ if (my $d = $rrd->{$key}) {
$entry->{maxdisk} = ($d->[1] || 0) + 0;
$entry->{disk} = ($d->[2] || 0) + 0;
$entry->{status} = 'available';
--
2.39.5
More information about the pve-devel
mailing list