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:
1 parent
195fd035e0
commit
fa6cca4f64
3 files changed
+34
-8
No files matched your search
@@ -228,18 +228,22 @@ phased_packages() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Seconds since apt last refreshed its package lists, or empty if unknown.
|
# 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() {
|
apt_lists_age() {
|
||||||
local newest=0 f m
|
local newest=0 f m
|
||||||
for f in \
|
for f in \
|
||||||
"$ROOT/var/lib/apt/periodic/update-success-stamp" \
|
"$ROOT/var/lib/apt/periodic/update-success-stamp" \
|
||||||
"$ROOT/var/lib/apt/periodic/update-stamp" \
|
"$ROOT/var/lib/apt/periodic/update-stamp"; do
|
||||||
"$ROOT/var/lib/apt/lists/partial"; do
|
|
||||||
[[ -e $f ]] || continue
|
|
||||||
m=$(stat -c %Y "$f" 2>/dev/null) || continue
|
m=$(stat -c %Y "$f" 2>/dev/null) || continue
|
||||||
((m > newest)) && newest=$m
|
((m > newest)) && newest=$m
|
||||||
done
|
done
|
||||||
((newest > 0)) && printf '%s' $(($(date +%s) - newest))
|
if ((newest == 0)); then
|
||||||
return 0
|
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";
|
# Size and free space of the filesystem holding /boot, in KiB, as "SIZE FREE";
|
||||||
|
|||||||
@@ -141,9 +141,14 @@ How the agent works it out:
|
|||||||
list also on the host page.
|
list also on the host page.
|
||||||
They never affect status, because Ubuntu doesn't phase security updates and
|
They never affect status, because Ubuntu doesn't phase security updates and
|
||||||
there's nothing to act on until apt offers them.
|
there's nothing to act on until apt offers them.
|
||||||
- **Package list freshness**: the mtime of apt's update stamps. The agent
|
- **Package list freshness**: the mtime of apt's success stamps,
|
||||||
**doesn't run `apt update`** itself; it relies on the stock `apt-daily.timer`.
|
`update-success-stamp` (Ubuntu, and `kernelwatch-apt-update.service`) and
|
||||||
If that's disabled, the host shows `update` with "package lists N days old".
|
`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
|
The agent sends raw facts and the server decides what they mean, so you can
|
||||||
change the status rules without touching the agents.
|
change the status rules without touching the agents.
|
||||||
@@ -446,6 +446,23 @@ X
|
|||||||
os.remove(os.path.join(self.root, "var/lib/apt/periodic/update-success-stamp"))
|
os.remove(os.path.join(self.root, "var/lib/apt/periodic/update-success-stamp"))
|
||||||
self.assertIsNone(self.report()["apt_lists_age_seconds"])
|
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):
|
def test_send_without_config_fails_cleanly(self):
|
||||||
proc = self.run_agent()
|
proc = self.run_agent()
|
||||||
self.assertNotEqual(proc.returncode, 0)
|
self.assertNotEqual(proc.returncode, 0)
|
||||||
|
|||||||
Reference in new issue
Block a user