From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id AFC2C1FF13C for ; Thu, 02 Apr 2026 14:49:32 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 86BA817142; Thu, 2 Apr 2026 14:48:58 +0200 (CEST) From: Daniel Kral To: pve-devel@lists.proxmox.com Subject: [PATCH ha-manager v4 04/28] env: pve2: implement dynamic node and service stats Date: Thu, 2 Apr 2026 14:43:58 +0200 Message-ID: <20260402124817.416232-5-d.kral@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260402124817.416232-1-d.kral@proxmox.com> References: <20260402124817.416232-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: 1775134042737 X-SPAM-LEVEL: Spam detection results: 0 AWL -2.419 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 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 1 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 1 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 1 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. 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: 5GITMQ5TXJHA7OSDRWAE6S7AF5RE45L3 X-Message-ID-Hash: 5GITMQ5TXJHA7OSDRWAE6S7AF5RE45L3 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 v3 -> v4: - introduce $HOSTNAME_RE and use it for nodename matching in get_dynamic_node_stats() src/PVE/HA/Env/PVE2.pm | 65 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/PVE/HA/Env/PVE2.pm b/src/PVE/HA/Env/PVE2.pm index 04cd1bfe..fc815fe0 100644 --- a/src/PVE/HA/Env/PVE2.pm +++ b/src/PVE/HA/Env/PVE2.pm @@ -42,8 +42,23 @@ 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, +}; + +my $HOSTNAME_RE = qr/(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]{,61}?[a-zA-Z0-9])?)/; + sub new { my ($this, $nodename) = @_; @@ -569,6 +584,30 @@ sub get_static_service_stats { return $stats; } +sub get_dynamic_service_stats { + my ($self) = @_; + + 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 +627,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\/($HOSTNAME_RE)$/; + + 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