Flag hosts where unattended-upgrades has stopped succeeding #27
No files matched your search
+39
-1
@@ -6,7 +6,7 @@
|
|||||||
# rules can change without redeploying agents.
|
# rules can change without redeploying agents.
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
AGENT_VERSION=0.7.0
|
AGENT_VERSION=0.8.0
|
||||||
|
|
||||||
# Prefix for every file the agent reads. Only the test suite sets this.
|
# Prefix for every file the agent reads. Only the test suite sets this.
|
||||||
ROOT=${KERNELWATCH_ROOT:-}
|
ROOT=${KERNELWATCH_ROOT:-}
|
||||||
@@ -261,6 +261,39 @@ unfinished_packages() {
|
|||||||
sort -u || true
|
sort -u || true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# unattended-upgrades: "enabled", "disabled" or "not-installed". Enabled means the
|
||||||
|
# package is installed (not just its leftover config, dpkg state rc) and
|
||||||
|
# APT::Periodic::Unattended-Upgrade is set and not 0, which is what makes
|
||||||
|
# apt-daily-upgrade.service (apt.systemd.daily) run it.
|
||||||
|
unattended_upgrades_state() {
|
||||||
|
local abbrev value
|
||||||
|
abbrev=$(dpkg-query -W -f='${db:Status-Abbrev}' unattended-upgrades 2>/dev/null || true)
|
||||||
|
if [[ $abbrev != ii* ]]; then
|
||||||
|
printf 'not-installed'
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
value=$(apt-config dump APT::Periodic::Unattended-Upgrade 2>/dev/null |
|
||||||
|
sed -n 's/^APT::Periodic::Unattended-Upgrade "\(.*\)";$/\1/p' | tail -n1 || true)
|
||||||
|
if [[ -n $value && $value != 0 ]]; then printf 'enabled'; else printf 'disabled'; fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Seconds since the file at $1 was modified, or empty if it doesn't exist.
|
||||||
|
file_age() {
|
||||||
|
local m
|
||||||
|
m=$(stat -c %Y "$1" 2>/dev/null) || return 0
|
||||||
|
printf '%s' $(($(date +%s) - m))
|
||||||
|
}
|
||||||
|
|
||||||
|
# apt.systemd.daily touches upgrade-stamp only when unattended-upgrade exits 0.
|
||||||
|
# unattended-upgrade writes its own stamp whenever a run gets far enough to know
|
||||||
|
# what it would do, even if it then fails (e.g. on a conffile prompt). So a newer
|
||||||
|
# run stamp than success stamp means it's running but failing.
|
||||||
|
UU_SUCCESS_STAMP=$ROOT/var/lib/apt/periodic/upgrade-stamp
|
||||||
|
UU_RUN_STAMP=$ROOT/var/lib/apt/periodic/unattended-upgrades-stamp
|
||||||
|
# Packages from its allowed origins it didn't upgrade (held, blacklisted, conffile
|
||||||
|
# prompt), space-separated; removed when there are none.
|
||||||
|
UU_KEPT_FILE=$ROOT/var/lib/unattended-upgrades/kept-back
|
||||||
|
|
||||||
# Ubuntu Pro state from the pro client's status cache, which it keeps
|
# Ubuntu Pro state from the pro client's status cache, which it keeps
|
||||||
# world-readable (the token lives in private/ next to it). Prints "ATTACHED ESM"
|
# world-readable (the token lives in private/ next to it). Prints "ATTACHED ESM"
|
||||||
# (e.g. "true enabled", "false -"); nothing if there's no file or it can't be
|
# (e.g. "true enabled", "false -"); nothing if there's no file or it can't be
|
||||||
@@ -361,6 +394,11 @@ build_report() {
|
|||||||
*) printf '"ubuntu_pro_attached":null,' ;;
|
*) printf '"ubuntu_pro_attached":null,' ;;
|
||||||
esac
|
esac
|
||||||
printf '"ubuntu_esm_infra":%s,' "$(json_str_or_null "$pro_esm")"
|
printf '"ubuntu_esm_infra":%s,' "$(json_str_or_null "$pro_esm")"
|
||||||
|
printf '"unattended_upgrades":%s,' "$(json_str "$(unattended_upgrades_state)")"
|
||||||
|
printf '"unattended_upgrades_success_age_seconds":%s,' "$(json_int "$(file_age "$UU_SUCCESS_STAMP")")"
|
||||||
|
printf '"unattended_upgrades_run_age_seconds":%s,' "$(json_int "$(file_age "$UU_RUN_STAMP")")"
|
||||||
|
printf '"unattended_upgrades_kept_back":%s,' \
|
||||||
|
"$( { [[ -r $UU_KEPT_FILE ]] && tr -s ' \t' '\n' <"$UU_KEPT_FILE"; } | sort -u | json_str_array)"
|
||||||
printf '"apt_lists_age_seconds":%s,' "$(json_int "$(apt_lists_age)")"
|
printf '"apt_lists_age_seconds":%s,' "$(json_int "$(apt_lists_age)")"
|
||||||
printf '"uptime_seconds":%s' "$(json_int "$(uptime_seconds)")"
|
printf '"uptime_seconds":%s' "$(json_int "$(uptime_seconds)")"
|
||||||
printf '}\n'
|
printf '}\n'
|
||||||
|
|||||||
@@ -4,4 +4,4 @@ __version__ = "0.4.0"
|
|||||||
|
|
||||||
# Version of agent/kernelwatch-agent shipped alongside this server; older agents
|
# Version of agent/kernelwatch-agent shipped alongside this server; older agents
|
||||||
# are noted on the dashboard. tests/test_status.py checks the two stay in sync.
|
# are noted on the dashboard. tests/test_status.py checks the two stay in sync.
|
||||||
AGENT_VERSION = "0.7.0"
|
AGENT_VERSION = "0.8.0"
|
||||||
@@ -47,6 +47,10 @@ FIELDS: dict[str, str] = {
|
|||||||
"dpkg_unfinished_packages": "strlist",
|
"dpkg_unfinished_packages": "strlist",
|
||||||
"ubuntu_pro_attached": "bool",
|
"ubuntu_pro_attached": "bool",
|
||||||
"ubuntu_esm_infra": "str",
|
"ubuntu_esm_infra": "str",
|
||||||
|
"unattended_upgrades": "str",
|
||||||
|
"unattended_upgrades_success_age_seconds": "int",
|
||||||
|
"unattended_upgrades_run_age_seconds": "int",
|
||||||
|
"unattended_upgrades_kept_back": "strlist",
|
||||||
"apt_lists_age_seconds": "int",
|
"apt_lists_age_seconds": "int",
|
||||||
"uptime_seconds": "int",
|
"uptime_seconds": "int",
|
||||||
}
|
}
|
||||||
|
|||||||
+60
-1
@@ -60,10 +60,14 @@ class AgentCase(unittest.TestCase):
|
|||||||
'-v) echo "#1 SMP PREEMPT_DYNAMIC Debian 6.12.48-1";; esac\n')
|
'-v) echo "#1 SMP PREEMPT_DYNAMIC Debian 6.12.48-1";; esac\n')
|
||||||
stub(self.bin, "hostname", "echo web01.example\n")
|
stub(self.bin, "hostname", "echo web01.example\n")
|
||||||
# The full-database query (unfinished packages) prints $KERNELWATCH_ROOT/dpkg-all.
|
# The full-database query (unfinished packages) prints $KERNELWATCH_ROOT/dpkg-all.
|
||||||
stub(self.bin, "dpkg-query", 'case "$*" in *binary:Package*) cat "$KERNELWATCH_ROOT/dpkg-all" 2>/dev/null;; '
|
# The unattended-upgrades query prints $KERNELWATCH_ROOT/dpkg-uu (its status, e.g. "ii ").
|
||||||
|
stub(self.bin, "dpkg-query", 'case "$*" in *" unattended-upgrades") cat "$KERNELWATCH_ROOT/dpkg-uu" 2>/dev/null;; '
|
||||||
|
'*binary:Package*) cat "$KERNELWATCH_ROOT/dpkg-all" 2>/dev/null;; '
|
||||||
'*Version*) echo "ii 6.12.48-2";; *) cat <<\'X\'\n'
|
'*Version*) echo "ii 6.12.48-2";; *) cat <<\'X\'\n'
|
||||||
f"{DPKG_DEBIAN}X\n;; esac\n")
|
f"{DPKG_DEBIAN}X\n;; esac\n")
|
||||||
self.stub_df("/dev/sda1 999320 187364 743144 21% /boot\n")
|
self.stub_df("/dev/sda1 999320 187364 743144 21% /boot\n")
|
||||||
|
# `apt-config dump KEY` prints $KERNELWATCH_ROOT/apt-config (nothing: the key is unset).
|
||||||
|
stub(self.bin, "apt-config", 'cat "$KERNELWATCH_ROOT/apt-config" 2>/dev/null; exit 0\n')
|
||||||
stub(self.bin, "apt-get", f"cat <<'X'\n{APT_SIM_DEBIAN}X\n")
|
stub(self.bin, "apt-get", f"cat <<'X'\n{APT_SIM_DEBIAN}X\n")
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
@@ -212,6 +216,61 @@ class AgentReportTest(AgentCase):
|
|||||||
r = self.report()
|
r = self.report()
|
||||||
self.assertEqual((r["ubuntu_pro_attached"], r["ubuntu_esm_infra"]), (True, None))
|
self.assertEqual((r["ubuntu_pro_attached"], r["ubuntu_esm_infra"]), (True, None))
|
||||||
|
|
||||||
|
def write_root(self, path, text="", age=None):
|
||||||
|
path = os.path.join(self.root, path)
|
||||||
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||||
|
with open(path, "w") as f:
|
||||||
|
f.write(text)
|
||||||
|
if age is not None:
|
||||||
|
t = time.time() - age
|
||||||
|
os.utime(path, (t, t))
|
||||||
|
|
||||||
|
def uu(self, status="ii ", setting=None):
|
||||||
|
self.write_root("dpkg-uu", status)
|
||||||
|
if setting is not None:
|
||||||
|
self.write_root("apt-config", f'APT::Periodic::Unattended-Upgrade "{setting}";\n')
|
||||||
|
|
||||||
|
def test_unattended_upgrades_enabled(self):
|
||||||
|
self.uu(setting="1")
|
||||||
|
self.write_root("var/lib/apt/periodic/upgrade-stamp", age=6 * 3600)
|
||||||
|
self.write_root("var/lib/apt/periodic/unattended-upgrades-stamp", age=6 * 3600)
|
||||||
|
r = self.report()
|
||||||
|
self.assertEqual(r["unattended_upgrades"], "enabled")
|
||||||
|
self.assertAlmostEqual(r["unattended_upgrades_success_age_seconds"], 6 * 3600, delta=60)
|
||||||
|
self.assertAlmostEqual(r["unattended_upgrades_run_age_seconds"], 6 * 3600, delta=60)
|
||||||
|
self.assertEqual(r["unattended_upgrades_kept_back"], [])
|
||||||
|
|
||||||
|
def test_unattended_upgrades_interval_other_than_1_counts_as_enabled(self):
|
||||||
|
for setting in ("7", "always"):
|
||||||
|
with self.subTest(setting=setting):
|
||||||
|
self.uu(setting=setting)
|
||||||
|
self.assertEqual(self.report()["unattended_upgrades"], "enabled")
|
||||||
|
|
||||||
|
def test_unattended_upgrades_switched_off(self):
|
||||||
|
for setting in ("0", ""):
|
||||||
|
with self.subTest(setting=setting):
|
||||||
|
self.uu(setting=setting)
|
||||||
|
self.assertEqual(self.report()["unattended_upgrades"], "disabled")
|
||||||
|
os.remove(os.path.join(self.root, "apt-config"))
|
||||||
|
self.assertEqual(self.report()["unattended_upgrades"], "disabled", "setting absent")
|
||||||
|
|
||||||
|
def test_unattended_upgrades_removed_but_config_left(self):
|
||||||
|
# dpkg state rc keeps /etc/apt/apt.conf.d/20auto-upgrades, so the setting still reads 1.
|
||||||
|
self.uu(status="rc ", setting="1")
|
||||||
|
self.assertEqual(self.report()["unattended_upgrades"], "not-installed")
|
||||||
|
|
||||||
|
def test_unattended_upgrades_not_installed(self):
|
||||||
|
r = self.report()
|
||||||
|
self.assertEqual(r["unattended_upgrades"], "not-installed")
|
||||||
|
self.assertIsNone(r["unattended_upgrades_success_age_seconds"])
|
||||||
|
self.assertIsNone(r["unattended_upgrades_run_age_seconds"])
|
||||||
|
|
||||||
|
def test_unattended_upgrades_kept_back(self):
|
||||||
|
self.uu(setting="1")
|
||||||
|
self.write_root("var/lib/unattended-upgrades/kept-back", "openssl libssl3t64 linux-image-amd64")
|
||||||
|
self.assertEqual(self.report()["unattended_upgrades_kept_back"],
|
||||||
|
["libssl3t64", "linux-image-amd64", "openssl"])
|
||||||
|
|
||||||
def test_every_reported_field_is_accepted_by_the_server(self):
|
def test_every_reported_field_is_accepted_by_the_server(self):
|
||||||
from kernelwatch.report import FIELDS
|
from kernelwatch.report import FIELDS
|
||||||
self.write_livepatch("LIVEPATCH-STATE: applied\nLIVEPATCH-FIXES: 2\n")
|
self.write_livepatch("LIVEPATCH-STATE: applied\nLIVEPATCH-FIXES: 2\n")
|
||||||
|
|||||||
Reference in new issue
Block a user