From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 4C3FB1FF0B3 for ; Thu, 26 Nov 2026 14:42:17 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id B6FF421723; Thu, 26 Nov 2026 14:41:58 +0100 (CET) From: Jonas Theisen To: pve-devel@lists.proxmox.com Subject: [PATCH common 04/16] systemd: add helper to list the current NTP sources Date: Thu, 26 Nov 2026 14:40:01 +0100 Message-ID: <20261126134128.316681-5-j.theisen@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261126134128.316681-1-j.theisen@proxmox.com> References: <20261126134128.316681-1-j.theisen@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1795700506583 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.070 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust 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: IWOYXY2MXXZC3PM67MTCS4K2ASAQ4T4W X-Message-ID-Hash: IWOYXY2MXXZC3PM67MTCS4K2ASAQ4T4W X-MailFrom: j.theisen@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: To be able to provide a detailed state of the local clock versus the clock sources this commit introduces the helpers to get an overview. This includes the currently used sources, their state and the offset between each source. For chrony, the machine-readable output of 'chronyc -c sources' is parsed. It is queried twice, once for the numeric addresses and once for the names the sources were configured with, so that pool members can be attributed to their pool. For systemd-timesyncd, which only ever uses a single server, the output of 'timedatectl timesync-status' is parsed, as the machine-readable variant does not include the offset. Signed-off-by: Jonas Theisen --- src/PVE/Systemd.pm | 121 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/src/PVE/Systemd.pm b/src/PVE/Systemd.pm index 35551a3..5d33ea4 100644 --- a/src/PVE/Systemd.pm +++ b/src/PVE/Systemd.pm @@ -586,6 +586,127 @@ sub sync_ntp { return; } +sub is_ntp_sources_supported { + my ($service) = @_; + + return 0 if !defined($service); + return (grep { $_ eq $service } ('chrony', 'systemd-timesyncd')) ? 1 : 0; +} + +my sub get_chrony_sources { + my $read_sources = sub { + my ($option) = @_; + my $rows = []; + run( + ['chronyc', '-c', $option, 'sources'], + outfunc => sub { push @$rows, [split(/,/, shift)] }, + ); + return $rows; + }; + + # '-n' prints the numeric addresses, '-N' the names the sources were configured with, e.g. the + # name of the pool for all of its members + my $rows = $read_sources->('-n'); + my $names = $read_sources->('-N'); + $names = [] if scalar(@$names) != scalar(@$rows); # the sources changed in between + + my $states = { + '*' => 'selected', + '+' => 'combined', + '-' => 'candidate', + 'x' => 'falseticker', + '~' => 'unusable', # too variable + '?' => 'unusable', # e.g. unreachable or not enough measurements yet + }; + + my $sources = []; + for my $i (0 .. $#$rows) { + my ($mode, $state, $address, $stratum, $poll, $reach, $last_rx, $offset) = $rows->[$i]->@*; + next if !defined($offset); + + my $source = { + address => $address, + state => $states->{$state} // 'unusable', + stratum => int($stratum), + poll => 2**$poll, # logarithm to base 2 + reach => oct($reach), # octal + offset => $offset + 0, + }; + # never received samples are reported with the maximum unsigned 32-bit value + $source->{'last-rx'} = int($last_rx) if $last_rx =~ m/^\d+$/ && $last_rx < 0xffffffff; + + my $name = $names->[$i]->[2]; + $source->{name} = $name if defined($name) && $name ne $address; + + push @$sources, $source; + } + + return $sources; +} + +# Converts a time span as printed by systemd, e.g. '-1.234ms' or '34min 8s', to seconds. +my sub parse_systemd_timespan { + my ($text) = @_; + + my $factors = { us => 1e-6, ms => 1e-3, s => 1, min => 60, h => 3600, d => 86400 }; + + my $sign = $text =~ s/^([+-])// && $1 eq '-' ? -1 : 1; + my $seconds; + while ($text =~ m/\G\s*(\d+(?:\.\d+)?)(min|ms|us|s|h|d)\b/gc) { + $seconds += $1 * $factors->{$2}; + } + return undef if !defined($seconds) || pos($text) != length($text); + + return $sign * $seconds; +} + +# timesyncd only reports the server it currently uses +my sub get_timesyncd_sources { + my $status = {}; + run( + ['timedatectl', 'timesync-status', '--no-pager'], + outfunc => sub { + my ($line) = @_; + $status->{ lc($1) } = $2 if $line =~ m/^\s*([^:]+?):\s*(.*?)\s*$/; + }, + ); + + my ($address, $name) = ($status->{server} // '') =~ m/^(\S+)(?:\s+\((.*)\))?$/; + return [] if !defined($address) || $address eq 'n/a'; + + my $source = { address => $address, state => 'unusable' }; + $source->{name} = $name if defined($name) && $name ne $address; + $source->{stratum} = int($1) if ($status->{stratum} // '') =~ m/^(\d+)$/; + + # e.g. '34min 8s (min: 32s; max 34min 8s)' + if (($status->{'poll interval'} // '') =~ m/^(.*?)\s*\(/) { + my $poll = parse_systemd_timespan($1); + $source->{poll} = $poll if defined($poll); + } + + # Only reported once a sample was received. timesyncd reports the offset of the source to the + # local clock, while chrony reports it the other way round, i.e. positive if the local clock is + # ahead, so flip it to match. + my $offset = parse_systemd_timespan($status->{offset} // ''); + if (defined($offset)) { + $source->{offset} = -$offset; + $source->{state} = 'selected'; + } + + return [$source]; +} + +# Returns the sources the active time synchronization service currently uses, including the +# members of pools, see is_ntp_sources_supported() for the supported services. +sub get_ntp_sources { + my $service = get_ntp_service(); + + die "listing the sources is not supported for the active time synchronization service\n" + if !is_ntp_sources_supported($service); + + return $service eq 'chrony' ? get_chrony_sources() : get_timesyncd_sources(); +} + =head3 notify() This is a pure Perl reimplementation of systemd's C mechanism as defined in -- 2.47.3