Skip to content

Commit

Permalink
use a better heuristic for the apt update last run time
Browse files Browse the repository at this point in the history
As reported in prometheus-community#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é <[email protected]>
Closes: prometheus-community#183
  • Loading branch information
anarcat committed Oct 17, 2023
1 parent 66010f0 commit 598c352
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion apt_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# Daniel Swarbrick <[email protected]>

import apt
import apt_pkg
import collections
import os
from prometheus_client import CollectorRegistry, Gauge, generate_latest
Expand Down Expand Up @@ -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.get("APT::Periodic::Update-Package-Lists") != "0":
# 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

Expand Down

0 comments on commit 598c352

Please sign in to comment.