自定义编写的 Nagios 插件总是 returns 不正确的值,但在命令行上有效

Custom written Nagios plugin always returns incorrect value but works on command line

我写了一个插件来检查两个主机是否同时在线,如果是,return 一个关键。当我在命令行本地 运行 此命令时,逻辑工作正常并且回显语句全部匹配,具体取决于检查的主机状态(例如 "CRITICAL - Both testbed controllers online" 或 "OK - $VM1 is the only testbed controller online." 等)。问题是,当我通过 ./check_nrpe -H <NRPEHost> -c "controller_check" 运行 这些命令时(相同的值也显示在 Nagios 网页上)无论主机的状态如何,它总是 returns 相同的值: "CRITICAL - Both testbed controllers currently offline" 回显 $VM1 和 $VM2 的实际值显示初始 if 检查 always 被设置为 0.

该脚本的工作原理是先从 ping -c 1 -W 1 $HOSTNAME if 创建二进制值,然后使用这些值创建实际的 alert/exit 值。这是为主机在线状态创建二进制值的 if 语句:

if ping -c 1 -W 1 $VM1HOSTNAME; then
  VM1=1
else
  VM1=0
fi

if ping -c 1 -W 1 $VM2HOSTNAME; then
  VM2=1
else
  VM2=0
fi

以及创建 NRPE 的实际逻辑 return:

if [ $VM1 -ne $VM2 ]; then
  if [ $VM1 -gt $VM2 ]; then
    echo "OK - $VM1 is currently the only testbed controller online."
    exit 0
  else
    echo "OK - $VM2 is currently the only testbed controller online."
    exit 0
  fi
elif [ $VM1 -eq $VM2 ]; then
  if [ $VM1 -eq 0 ]; then
    echo "CRITICAL - Both testbed controllers currently offline"
    exit 2
  else
    echo "CRITICAL - Both testbed controllers currently online."
    exit 2
  fi
else
  echo "UNKNOWN - Unable to read output."
  exit 3
fi

我以前从未写过自己的 NRPE 插件,所以我假设我在这里做的是一些简单的错误,但我在网上看到的 NRPE 插件编写教程似乎与我写的相符。作为旁注,如果我使用 check_ping 而不是 ping -c 1 -W 1 值 returned 是正确的 但是 Nagios 网页上显示的唯一值是第一个 check_ping 命令的输出。

例如(这是正确的):

./check_nrpe -H ikor -c "check_testbed_controller_status"
PING OK - Packet loss = 0%, RTA = 0.81 ms|rta=0.811000ms;10.000000;20.000000;0.000000 pl=0%;2;5;0
PING OK - Packet loss = 0%, RTA = 0.79 ms|rta=0.787000ms;10.000000;20.000000;0.000000 pl=0%;2;5;0
CRITICAL - Both testbed controllers currently online.

但是Nagios的状态信息只显示PING OK - Packet loss = 0%, RTA = 0.79 ms而不是我想要的echo语句。

所以我想我是否可以 A) 修复使用 /usr/bin/ping 进行 if 检查导致 NRPE 始终将这些主机读取为离线的问题(if 检查总是 returns 0 ) 或 B) 使用 check_ping 但 return 只有 Nagios 的第三个标准输出行具有实际状态信息。这里有人对我有任何想法或阅读建议吗?非常感谢。

事实证明 SELinux 阻止 /usr/bin/ping 被 NRPE 守护进程执行。我没有尝试编写 SELinux 策略来允许这样做,而是使用了 Nagios 插件 check_ping 并将输出通过管道传输到 /dev/null。 NRPE 插件的最终逻辑如下所示:

if $NRPEPING -H $VM1HOSTNAME -w 10,2% -c 20,5% > /dev/null 2>&1; then
  VM1=1
else
  VM1=0
fi

if $NRPEPING -H $VM2HOSTNAME -w 10,2% -c 20,5% > /dev/null 2>&1; then
  VM2=1
else
  VM2=0
fi

这意味着 A) 我不必将 SELinux 设置为允许或允许 NRPE 守护程序执行 ping 和 B) 我在 Nagios 状态信息列中的输出正确显示回显语句,没有其他信息。