Let an agent report a chosen hostname
CI / tests (Python 3.10) (pull_request) Successful in 22s
CI / tests (Python 3.11) (pull_request) Successful in 22s
CI / tests (Python 3.12) (pull_request) Successful in 22s
CI / tests (Python 3.13) (pull_request) Successful in 21s
CI / lint (pull_request) Successful in 3s

REPORT_HOSTNAME in agent.conf replaces hostname -f in the report, for hosts
registered under a label that differs from their real hostname (otherwise the
dashboard notes a mismatch forever). install.sh --report-hostname NAME sets it,
--default-report-hostname clears it, and re-running with --server (register,
rotate) keeps it. Agent 0.4.1.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
patrickandClaude Sonnet 5.5 committed 2026-10-29 22:14:41 -04:00
1 parent 3e62d086d1
commit 10b1d6abb8
6 files changed
+57 -6

No files matched your search

+6 -2
View File
@@ -78,8 +78,12 @@ How the agent works it out:
the row gets a note: "registered as web01 but reports itself as db01". This the row gets a note: "registered as web01 but reports itself as db01". This
catches a token copied to the wrong machine. It never changes the status. A catches a token copied to the wrong machine. It never changes the status. A
host you registered under a custom label (`<ssh target> <name>` in host you registered under a custom label (`<ssh target> <name>` in
`agent/hosts`) will always show the note; register it under its `hostname -s` `agent/hosts`) shows the note until you tell its agent which name to report:
to clear it. `agent/deploy.sh iris -- --report-hostname iris` (or `install.sh
--report-hostname iris` on the host, which sets `REPORT_HOSTNAME` in
`agent.conf`). Upgrades and `--rotate` keep it;
`--default-report-hostname` removes it. That's the host's own say-so, so it
only silences the note for hosts you've deliberately named.
- **Available updates**: a simulated `apt-get dist-upgrade`. It is read-only, takes - **Available updates**: a simulated `apt-get dist-upgrade`. It is read-only, takes
no locks and writes no cache files. `dist-upgrade` is used so that new-ABI kernel no locks and writes no cache files. `dist-upgrade` is used so that new-ABI kernel
packages pulled in by the meta-package show up. Lines whose origin contains packages pulled in by the meta-package show up. Lines whose origin contains
+5
View File
@@ -12,3 +12,8 @@ TOKEN_FILE=/etc/kernelwatch/agent.token
# curl timeout in seconds. # curl timeout in seconds.
#TIMEOUT=30 #TIMEOUT=30
# Hostname to report instead of the machine's own (`hostname -f`). Set this when
# the host is registered under a different name, so the dashboard doesn't note
# the mismatch. Usually set with: install.sh --report-hostname NAME
#REPORT_HOSTNAME=web01
+24 -1
View File
@@ -3,6 +3,7 @@
# #
# sudo ./install.sh --server https://kernelwatch.example.internal [--ca ca.pem] # sudo ./install.sh --server https://kernelwatch.example.internal [--ca ca.pem]
# [--with-needrestart] [--with-apt-update | --without-apt-update] # [--with-needrestart] [--with-apt-update | --without-apt-update]
# [--report-hostname NAME | --default-report-hostname]
# #
# --with-needrestart installs needrestart (Ubuntu has it already) so the # --with-needrestart installs needrestart (Ubuntu has it already) so the
# dashboard can show services needing a restart, and sets it to only list # dashboard can show services needing a restart, and sets it to only list
@@ -12,6 +13,11 @@
# hourly report, so new kernel and security updates show up within the hour. # hourly report, so new kernel and security updates show up within the hour.
# --without-apt-update turns that off again. Without either, it stays as it was. # --without-apt-update turns that off again. Without either, it stays as it was.
# #
# --report-hostname makes the agent report NAME as its hostname instead of its
# own, for a host registered under a different name (the dashboard notes a
# mismatch). --default-report-hostname goes back to the machine's own. Without
# either, it stays as it was.
#
# The host token (from `kernelwatch add-host <name>` on the server) is read # The host token (from `kernelwatch add-host <name>` on the server) is read
# from --token-file, the KERNELWATCH_TOKEN environment variable, or a prompt, # from --token-file, the KERNELWATCH_TOKEN environment variable, or a prompt,
# so it never lands in shell history. Re-running without --server or a token # so it never lands in shell history. Re-running without --server or a token
@@ -24,6 +30,7 @@ ca=
token_file= token_file=
with_needrestart=0 with_needrestart=0
apt_update= apt_update=
report_hostname=keep
while (($#)); do while (($#)); do
case $1 in case $1 in
@@ -33,7 +40,12 @@ while (($#)); do
--with-needrestart) with_needrestart=1; shift ;; --with-needrestart) with_needrestart=1; shift ;;
--with-apt-update) apt_update=on; shift ;; --with-apt-update) apt_update=on; shift ;;
--without-apt-update) apt_update=off; shift ;; --without-apt-update) apt_update=off; shift ;;
-h | --help) sed -n '2,19s/^# \{0,1\}//p' "$0"; exit 0 ;; --report-hostname)
report_hostname=${2:?--report-hostname needs a name}; shift 2
[[ $report_hostname =~ ^[A-Za-z0-9][A-Za-z0-9._-]{0,62}$ ]] ||
{ echo "invalid --report-hostname '$report_hostname'" >&2; exit 2; } ;;
--default-report-hostname) report_hostname=off; shift ;;
-h | --help) sed -n '2,25s/^# \{0,1\}//p' "$0"; exit 0 ;;
*) echo "unknown argument: $1" >&2; exit 2 ;; *) echo "unknown argument: $1" >&2; exit 2 ;;
esac esac
done done
@@ -78,6 +90,9 @@ if [[ -n $ca ]]; then
install -m 0644 "$ca" /etc/kernelwatch/ca.pem install -m 0644 "$ca" /etc/kernelwatch/ca.pem
fi fi
# Writing the config below replaces it, so remember a REPORT_HOSTNAME to keep.
prev_report_hostname=$(sed -n 's/^REPORT_HOSTNAME=//p' /etc/kernelwatch/agent.conf 2>/dev/null | tail -n1 || true)
if [[ -n $server ]]; then if [[ -n $server ]]; then
{ {
echo "SERVER_URL=$server" echo "SERVER_URL=$server"
@@ -90,6 +105,14 @@ elif [[ ! -e /etc/kernelwatch/agent.conf ]]; then
exit 1 exit 1
fi fi
case $report_hostname in
keep) report_hostname=$prev_report_hostname ;;
off) report_hostname= ;;
esac
sed -i '/^REPORT_HOSTNAME=/d' /etc/kernelwatch/agent.conf
[[ -z $report_hostname ]] || echo "REPORT_HOSTNAME=$report_hostname" >>/etc/kernelwatch/agent.conf
chmod 0644 /etc/kernelwatch/agent.conf
token= token=
if [[ -n $token_file ]]; then if [[ -n $token_file ]]; then
token=$(tr -d '[:space:]' <"$token_file") token=$(tr -d '[:space:]' <"$token_file")
+5 -2
View File
@@ -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.4.0 AGENT_VERSION=0.4.1
# 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:-}
@@ -68,6 +68,8 @@ SERVER_URL=
TOKEN_FILE=$ROOT/etc/kernelwatch/agent.token TOKEN_FILE=$ROOT/etc/kernelwatch/agent.token
CA_FILE= CA_FILE=
TIMEOUT=30 TIMEOUT=30
# Name to report instead of `hostname -f`, for a host registered under another name.
REPORT_HOSTNAME=
load_config() { load_config() {
[[ -r $CONF ]] || return 0 [[ -r $CONF ]] || return 0
@@ -84,6 +86,7 @@ load_config() {
TOKEN_FILE) TOKEN_FILE=$value ;; TOKEN_FILE) TOKEN_FILE=$value ;;
CA_FILE) CA_FILE=$value ;; CA_FILE) CA_FILE=$value ;;
TIMEOUT) TIMEOUT=$value ;; TIMEOUT) TIMEOUT=$value ;;
REPORT_HOSTNAME) REPORT_HOSTNAME=$value ;;
*) echo "kernelwatch-agent: ignoring unknown config key '$key'" >&2 ;; *) echo "kernelwatch-agent: ignoring unknown config key '$key'" >&2 ;;
esac esac
done <"$CONF" done <"$CONF"
@@ -248,7 +251,7 @@ build_report() {
printf '{' printf '{'
printf '"agent_version":%s,' "$(json_str "$AGENT_VERSION")" printf '"agent_version":%s,' "$(json_str "$AGENT_VERSION")"
printf '"reported_hostname":%s,' "$(json_str "$(hostname -f 2>/dev/null || hostname)")" printf '"reported_hostname":%s,' "$(json_str "${REPORT_HOSTNAME:-$(hostname -f 2>/dev/null || hostname)}")"
printf '"os_id":%s,' "$(json_str "$(os_release_field ID)")" printf '"os_id":%s,' "$(json_str "$(os_release_field ID)")"
printf '"os_version_id":%s,' "$(json_str "$(os_release_field VERSION_ID)")" printf '"os_version_id":%s,' "$(json_str "$(os_release_field VERSION_ID)")"
printf '"os_pretty_name":%s,' "$(json_str "$(os_release_field PRETTY_NAME)")" printf '"os_pretty_name":%s,' "$(json_str "$(os_release_field PRETTY_NAME)")"
+1 -1
View File
@@ -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.4.0" AGENT_VERSION = "0.4.1"
+16
View File
@@ -139,6 +139,22 @@ class AgentReportTest(AgentCase):
self.write_livepatch("LIVEPATCH-STATE: applied\nLIVEPATCH-FIXES: 2\n") self.write_livepatch("LIVEPATCH-STATE: applied\nLIVEPATCH-FIXES: 2\n")
self.assertEqual(sorted(set(self.report()) - set(FIELDS)), []) self.assertEqual(sorted(set(self.report()) - set(FIELDS)), [])
def write_conf(self, text):
with open(os.path.join(self.root, "etc/kernelwatch/agent.conf"), "w") as f:
f.write(text)
def test_report_hostname_overrides_the_machines_own(self):
self.write_conf("SERVER_URL=http://x\nREPORT_HOSTNAME=iris\n")
self.assertEqual(self.report()["reported_hostname"], "iris")
def test_blank_report_hostname_uses_the_machines_own(self):
self.write_conf("REPORT_HOSTNAME=\n")
self.assertEqual(self.report()["reported_hostname"], "web01.example")
def test_report_hostname_is_json_escaped(self):
self.write_conf('REPORT_HOSTNAME=a"b\\c\n')
self.assertEqual(self.report()["reported_hostname"], 'a"b\\c')
def test_no_livepatch_file_reports_nulls(self): def test_no_livepatch_file_reports_nulls(self):
r = self.report() r = self.report()
for key in ("state", "check_state", "supported", "upgrade_required", "fix_count"): for key in ("state", "check_state", "supported", "upgrade_required", "fix_count"):