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

+5
View File
@@ -12,3 +12,8 @@ TOKEN_FILE=/etc/kernelwatch/agent.token
# curl timeout in seconds.
#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]
# [--with-needrestart] [--with-apt-update | --without-apt-update]
# [--report-hostname NAME | --default-report-hostname]
#
# --with-needrestart installs needrestart (Ubuntu has it already) so the
# 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.
# --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
# 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
@@ -24,6 +30,7 @@ ca=
token_file=
with_needrestart=0
apt_update=
report_hostname=keep
while (($#)); do
case $1 in
@@ -33,7 +40,12 @@ while (($#)); do
--with-needrestart) with_needrestart=1; shift ;;
--with-apt-update) apt_update=on; 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 ;;
esac
done
@@ -78,6 +90,9 @@ if [[ -n $ca ]]; then
install -m 0644 "$ca" /etc/kernelwatch/ca.pem
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
{
echo "SERVER_URL=$server"
@@ -90,6 +105,14 @@ elif [[ ! -e /etc/kernelwatch/agent.conf ]]; then
exit 1
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=
if [[ -n $token_file ]]; then
token=$(tr -d '[:space:]' <"$token_file")
+5 -2
View File
@@ -6,7 +6,7 @@
# rules can change without redeploying agents.
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.
ROOT=${KERNELWATCH_ROOT:-}
@@ -68,6 +68,8 @@ SERVER_URL=
TOKEN_FILE=$ROOT/etc/kernelwatch/agent.token
CA_FILE=
TIMEOUT=30
# Name to report instead of `hostname -f`, for a host registered under another name.
REPORT_HOSTNAME=
load_config() {
[[ -r $CONF ]] || return 0
@@ -84,6 +86,7 @@ load_config() {
TOKEN_FILE) TOKEN_FILE=$value ;;
CA_FILE) CA_FILE=$value ;;
TIMEOUT) TIMEOUT=$value ;;
REPORT_HOSTNAME) REPORT_HOSTNAME=$value ;;
*) echo "kernelwatch-agent: ignoring unknown config key '$key'" >&2 ;;
esac
done <"$CONF"
@@ -248,7 +251,7 @@ build_report() {
printf '{'
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_version_id":%s,' "$(json_str "$(os_release_field VERSION_ID)")"
printf '"os_pretty_name":%s,' "$(json_str "$(os_release_field PRETTY_NAME)")"