check_mk_agent: Do not rely on "hostname" cmd on OpenWRT
Issue:
Built-in BusyBox shell in OpenWRT has no hostname command available by default. While it can be added with additional packages in custom images there are alternative ways to determine the machine's hostname without introducing an additional dependency.
On some default OpenWRT image the "Hostname" attribute is empty:
root@my-openwrt-box:~# hostname
-ash: hostname: not found
root@my-openwrt-box:~# cat /proc/sys/kernel/hostname
my-openwrt-box
root@my-openwrt-box:~# check_mk_agent dump
<<<check_mk>>>
Version: 2.2.0p26
AgentOS: openwrt
Hostname:
AgentDirectory: /etc/check_mk
...
Well, it doesn't really hurt much, but it's not desired behavior either.
Proposed changes:
Read the hostname from the sysctl parameter kernel.hostname (i.e. read /proc/sys/kernel/hostname) instead which is likely available on every image.
root@my-openwrt-box:~# check_mk_agent dump
<<<check_mk>>>
Version: 2.2.0p26
AgentOS: openwrt
Hostname: my-openwrt-box
AgentDirectory: /etc/check_mk
...
All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.
I have read the CLA Document and I hereby sign the CLA or my organization already has a signed CLA.
My first attempt had a conditional function in the preamble
if ! inpath hostname; then
hostname() {
cat /proc/sys/kernel/hostname
}
fi
but I couldn't find any real reasons why a system's hostname command should be preferable in this context.
If desired though, I can update my PR accordingly.
Thanks for highlighting this. In OpenWRT hostname isn't there and won't come anytime soon (https://github.com/openwrt/openwrt/issues/11765)
So, your PR is approved from my perspective (product, not dev). We are pondering as well, if we should not just use /proc/sys/kernel/hostname in all UNIX agents though. Will forward to a dev.