1

我有以下服务定义:

define service{
    use                     my-service      ; Name of service template to use
    host_name               dra
    service_description     https://www.example.com
    check_command           check_http!-I my.ip.address --ssl -H www.example.com
    notifications_enabled   1
    retry_check_interval    2
    normal_check_interval   5
    contact_groups          myadmins
}

服务检查一直失败

Name or service not known
HTTP CRITICAL - Unable to open TCP socket

但是,如果我从命令行运行 http_check,我会得到 200 OK 结果:

/usr/lib/nagios/plugins/check_http -I my.ip.address --ssl -H www.example.com -v

.....
HTTP OK: HTTP/1.1 200 OK - 9176 bytes in 0.074 second response time |time=0.073543s;;;0.000000 size=9176B;;;0

另请注意,有问题的 URL 在浏览器中可以正常工作,证书有效等。我还对一堆其他站点使用完全相同的服务定义,它们都可以正常工作。我唯一能想到的是这个远程主机在 DigitalOcean 上运行,并且分配了一个“浮动 IP”。我尝试用my.ip.address分配给主机的浮动 IP 或“标准”IP 替换上面(以及在 nagios 配置文件的主机定义中),这没有区别。

nagios运行相同的命令时如何可能会失败,但手动运行时会成功?

4

1 回答 1

0

我的问题的答案是:不要使用check_http,使用

  1. 使用check_https_hostname, 和
  2. 确保该host_name节是实际的主机名
  3. 这需要匹配同一 cfg 文件host_name中所有service和定义中的节。host

所以:

define service{
    use                     my-service         ; Name of service template to use
    host_name               www.example.com
    service_description     https://www.example.com
    check_command           check_https_hostname
    notifications_enabled   1
    retry_check_interval    2
    normal_check_interval   5
    contact_groups          myadmins
}

原因如下:通过查看我的安装文件中的check_http和的定义,就很清楚了。check_https_hostname/etc/nagios-plugins/config/http.cfg

# 'check_http' command definition
define command{
        command_name    check_http
        command_line    /usr/lib/nagios/plugins/check_http -H '$HOSTADDRESS$' -I '$HOSTADDRESS$' '$ARG1$'
        }

# 'check_https_hostname' command definition
define command{
        command_name    check_https_hostname
        command_line    /usr/lib/nagios/plugins/check_http --ssl -H '$HOSTNAME$' -I '$HOSTADDRESS$' '$ARG1$'
        }

您会注意到-H-I中的参数check_http得到相同的值$HOSTADDRESS$,而在check_https_hostname它们分别得到$HOSTNAME$$HOSTADDRESS$

我构建原始命令的事实check_http!-I my.ip.address --ssl -H www.example.com并不重要。最后,该/usr/lib/nagios/plugins/check_http命令得到了两个值 for-I和两个 for -H,第二对被忽略了。

这确实打破了对 Cloudflare 的“感谢”,因为 Cloudflare 动态分配给我的 www.example.com 的 IP 地址与我在主机定义中指定的实际主机 IP 地址不同。

最后,我想提一下,帮助我解决这个问题的是设置

debug_level=-1
debug_verbosity=1

在我的/etc/nagios3/nagios.cfg文件中,然后查看/var/log/nagios3/nagios.debug.

此外,请查看 中check_http命令的所有不同变体/etc/nagios-plugins/config/http.cfg。有一些非常有用的。

于 2020-05-15T02:48:56.280 回答