Don't let failed apt updates make package lists look fresh

apt touches /var/lib/apt/lists/partial on every update attempt,
including ones that fail, and the agent took the newest of it and the
two success stamps. So a host whose apt update failed hourly reported
lists an hour old. Use the success stamps (update-success-stamp,
update-stamp) and fall back to lists/partial only when neither exists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
patrickandClaude Opus 5.5 committed 2026-11-01 14:53:19 -05:00
1 parent 195fd035e0
commit fa6cca4f64
3 files changed
+34 -8

No files matched your search

+9 -5
View File
@@ -228,18 +228,22 @@ phased_packages() {
}
# Seconds since apt last refreshed its package lists, or empty if unknown.
# Both stamps are touched only after a successful update: update-success-stamp by
# Ubuntu's update-notifier hook and kernelwatch-apt-update.service, update-stamp by
# apt-daily. lists/partial changes on every attempt, failed ones too, so it's only
# a last resort for hosts that have neither (Debian without either timer).
apt_lists_age() {
local newest=0 f m
for f in \
"$ROOT/var/lib/apt/periodic/update-success-stamp" \
"$ROOT/var/lib/apt/periodic/update-stamp" \
"$ROOT/var/lib/apt/lists/partial"; do
[[ -e $f ]] || continue
"$ROOT/var/lib/apt/periodic/update-stamp"; do
m=$(stat -c %Y "$f" 2>/dev/null) || continue
((m > newest)) && newest=$m
done
((newest > 0)) && printf '%s' $(($(date +%s) - newest))
return 0
if ((newest == 0)); then
newest=$(stat -c %Y "$ROOT/var/lib/apt/lists/partial" 2>/dev/null) || return 0
fi
printf '%s' $(($(date +%s) - newest))
}
# Size and free space of the filesystem holding /boot, in KiB, as "SIZE FREE";
+8 -3
View File
@@ -141,9 +141,14 @@ How the agent works it out:
list also on the host page.
They never affect status, because Ubuntu doesn't phase security updates and
there's nothing to act on until apt offers them.
- **Package list freshness**: the mtime of apt's update stamps. The agent
**doesn't run `apt update`** itself; it relies on the stock `apt-daily.timer`.
If that's disabled, the host shows `update` with "package lists N days old".
- **Package list freshness**: the mtime of apt's success stamps,
`update-success-stamp` (Ubuntu, and `kernelwatch-apt-update.service`) and
`update-stamp` (`apt-daily`), both in `/var/lib/apt/periodic/` and touched
only when `apt update` succeeds. A host with neither falls back to
`/var/lib/apt/lists/partial`, which changes on failed updates too. The agent
**doesn't run `apt update`** itself; it relies on `apt-daily.timer` or
`--with-apt-update`. If the lists stop refreshing, the host shows `update`
with "package lists N days old".
The agent sends raw facts and the server decides what they mean, so you can
change the status rules without touching the agents.
+17
View File
@@ -446,6 +446,23 @@ X
os.remove(os.path.join(self.root, "var/lib/apt/periodic/update-success-stamp"))
self.assertIsNone(self.report()["apt_lists_age_seconds"])
def test_newest_success_stamp_wins(self):
self.write_root("var/lib/apt/periodic/update-stamp", age=600)
self.assertAlmostEqual(self.report()["apt_lists_age_seconds"], 600, delta=60)
def test_failed_updates_touching_partial_dont_count_as_fresh(self):
# apt touches lists/partial even when the update fails.
os.makedirs(os.path.join(self.root, "var/lib/apt/lists/partial"))
self.assertAlmostEqual(self.report()["apt_lists_age_seconds"], 7200, delta=60)
def test_partial_is_the_last_resort_without_stamps(self):
os.remove(os.path.join(self.root, "var/lib/apt/periodic/update-success-stamp"))
partial = os.path.join(self.root, "var/lib/apt/lists/partial")
os.makedirs(partial)
t = time.time() - 3600
os.utime(partial, (t, t))
self.assertAlmostEqual(self.report()["apt_lists_age_seconds"], 3600, delta=60)
def test_send_without_config_fails_cleanly(self):
proc = self.run_agent()
self.assertNotEqual(proc.returncode, 0)