librenms / librenms-agent

LibreNMS Agent & Scripts
GNU General Public License v2.0
118 stars 188 forks source link

ntp-server.sh does not perform version check correctly #376

Closed aekinskjaldi closed 2 years ago

aekinskjaldi commented 3 years ago

ntpd --version from Centos 7 repo gives output to stderr instead of stdout, and thus variable VER remains empty even if version is exactly as mentioned in condition, i.e. 4.2.6p5, and instead of ntpdc command script tries to execute ntpq Moreover, ntpdc does not resolve localhost correctly and gives error

ntpdc: write to localhost failed: Operation not permitted

I rewrote condition this way to make it work

VER=$($BIN_NTPD --version 2>&1 | head -n 1)
if [[ "$VER" == *"4.2.6p5"* ]]; then
  USECMD=$(echo "$BIN_NTPDC" -c iostats 127.0.0.1)
else
  USECMD=$(echo "$BIN_NTPQ" -c iostats 127.0.0.1)
fi
VVelox commented 2 years ago

Looks like it is working properly here for grabbing that version on CentOS 7 under either bash or sh(but both of those working the same is hardly surprising).

[root@elk0]0|/root>sh
sh-4.2# VER=$(ntpd --version 2>&1 | head -n 1)
sh-4.2# echo $VER
ntpd 4.2.6p5
sh-4.2# exit
[root@elk0]0|/root>bash
[root@elk0 ~]# VER=$(ntpd --version 2>&1 | head -n 1)
[root@elk0 ~]# echo $VER
ntpd 4.2.6p5
[root@elk0 ~]# ntpd --version > /dev/null 
ntpd 4.2.6p5
[root@elk0 ~]# 
VVelox commented 2 years ago

Derp. Sorry. Disregard what I said. I see what you mean now.

Thanks. And aye, you are correct.

VVelox commented 2 years ago

Tested and looks good. Thanks!

VVelox commented 2 years ago

Made all POSIX happy and merged. Thanks!