From feb943f6df8cbc536eaf7fca59a088d59bb39e82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= Date: Tue, 17 Oct 2023 10:27:30 -0400 Subject: [PATCH] use a better heuristic for the apt update last run time MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As reported in #182, `pkgcache.bin` gets updated on more than just `apt update`. In particular, it gets modified when a package is installed, upgraded or removed. So let's fallback on a better heuristic, which is the `APT::Periodic` timestamp. This should give us a much more precise and deliberate status. We also fallback to the `lists` directory, which gets updated when new mirror lists gets moved into place. This does run the risk of staying silently unchanged if there's no change on mirrors but (a) that's rather infrequent and (b) we might actually want to warn on that anyway. Signed-off-by: Antoine Beaupré Closes: #183 --- apt_info.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/apt_info.py b/apt_info.py index ad489d5..c783227 100755 --- a/apt_info.py +++ b/apt_info.py @@ -21,6 +21,7 @@ # Daniel Swarbrick import apt +import apt_pkg import collections import os from prometheus_client import CollectorRegistry, Gauge, generate_latest @@ -87,8 +88,16 @@ def _write_autoremove_pending(registry, cache): def _write_cache_timestamps(registry): g = Gauge('apt_package_cache_timestamp_seconds', "Apt update last run time.", registry=registry) + apt_pkg.init_config() + if apt_pkg.config.find_b("APT::Periodic::Update-Package-Lists"): + # if we run updates automatically with APT::Periodic, we can + # check this timestamp file + stamp_file = "/var/lib/apt/periodic/update-success-stamp" + else: + # if not, let's just fallback on the lists directory + stamp_file = '/var/lib/apt/lists' try: - g.set(os.stat('/var/cache/apt/pkgcache.bin').st_mtime) + g.set(os.stat(stamp_file).st_mtime) except OSError: pass