From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id 86BC71FF144 for ; Tue, 24 Mar 2026 19:31:38 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 27F981A025; Tue, 24 Mar 2026 19:31:17 +0100 (CET) From: Daniel Kral To: pve-devel@lists.proxmox.com Subject: [PATCH ha-manager v2 20/40] env: pve2: implement dynamic node and service stats Date: Tue, 24 Mar 2026 19:30:04 +0100 Message-ID: <20260324183029.1274972-21-d.kral@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260324183029.1274972-1-d.kral@proxmox.com> References: <20260324183029.1274972-1-d.kral@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1774376988916 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.943 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment KAM_MAILER 2 Automated Mailer Tag Left in Email SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: AVWFIZZD7L35QYIDDNJXTZCYCEIW4DDJ X-Message-ID-Hash: AVWFIZZD7L35QYIDDNJXTZCYCEIW4DDJ X-MailFrom: d.kral@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Fetch the dynamic node and service stats with rrd_dump(), which is periodically sampled and broadcasted by the PVE nodes' pvestatd service and propagated through the pmxcfs. Signed-off-by: Daniel Kral --- changes v1 -> v2: - use constants for the RRD entry indices - add note about the capping of the maxcpu property for guests src/PVE/HA/Env/PVE2.pm | 63 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/PVE/HA/Env/PVE2.pm b/src/PVE/HA/Env/PVE2.pm index 04cd1bfe..4dfb304e 100644 --- a/src/PVE/HA/Env/PVE2.pm +++ b/src/PVE/HA/Env/PVE2.pm @@ -42,6 +42,19 @@ my $lockdir = "/etc/pve/priv/lock"; # taken from PVE::Service::pvestatd::update_{lxc,qemu}_status() use constant { RRD_VM_INDEX_STATUS => 2, + RRD_VM_INDEX_MAXCPU => 5, + RRD_VM_INDEX_CPU => 6, + RRD_VM_INDEX_MAXMEM => 7, + RRD_VM_INDEX_MEM => 8, +}; + +# rrd entry indices for PVE nodes +# taken from PVE::Service::pvestatd::update_node_status() +use constant { + RRD_NODE_INDEX_MAXCPU => 4, + RRD_NODE_INDEX_CPU => 5, + RRD_NODE_INDEX_MAXMEM => 7, + RRD_NODE_INDEX_MEM => 8, }; sub new { @@ -569,6 +582,30 @@ sub get_static_service_stats { return $stats; } +sub get_dynamic_service_stats { + my ($self, $id) = @_; + + my $rrd = PVE::Cluster::rrd_dump(); + + my $stats = get_cluster_service_stats(); + for my $sid (keys %$stats) { + my $id = $stats->{$sid}->{id}; + my $rrdentry = $rrd->{"pve-vm-9.0/$id"} // []; + + # NOTE the guests' broadcasted vmstatus() caps maxcpu at the node's maxcpu + my $maxcpu = ($rrdentry->[RRD_VM_INDEX_MAXCPU] || 0.0) + 0.0; + + $stats->{$sid}->{usage} = { + maxcpu => $maxcpu, + cpu => (($rrdentry->[RRD_VM_INDEX_CPU] || 0.0) + 0.0) * $maxcpu, + maxmem => int($rrdentry->[RRD_VM_INDEX_MAXMEM] || 0), + mem => int($rrdentry->[RRD_VM_INDEX_MEM] || 0), + }; + } + + return $stats; +} + sub get_static_node_stats { my ($self) = @_; @@ -588,6 +625,32 @@ sub get_static_node_stats { return $stats; } +sub get_dynamic_node_stats { + my ($self) = @_; + + my $rrd = PVE::Cluster::rrd_dump(); + + my $stats = {}; + for my $key (keys %$rrd) { + my ($nodename) = $key =~ m/^pve-node-9.0\/(\w+)$/; + + next if !$nodename; + + my $rrdentry = $rrd->{$key} // []; + + my $maxcpu = int($rrdentry->[RRD_NODE_INDEX_MAXCPU] || 0); + + $stats->{$nodename} = { + maxcpu => $maxcpu, + cpu => (($rrdentry->[RRD_NODE_INDEX_CPU] || 0.0) + 0.0) * $maxcpu, + maxmem => int($rrdentry->[RRD_NODE_INDEX_MAXMEM] || 0), + mem => int($rrdentry->[RRD_NODE_INDEX_MEM] || 0), + }; + } + + return $stats; +} + sub get_node_version { my ($self, $node) = @_; -- 2.47.3