使用 monit 检查 TCP 端口是否关闭

使用 monit 检查 TCP 端口是否关闭

我想检查主机上定义的端口是否关闭,例如8080主机上的端口www.example.com。我尝试过类似的事情:

check host www.example.com with address www.example.com
    if not failed 8080 protocol http then alert

但我认为我不能使用这种语法,因为不是关键字不允许存在:

/etc/monit/conf.d/myhost:29: Error: syntax error 'failed'

有没有办法可以在不编写外部脚本的情况下做到这一点?

答案1

使用if succeeded

# Verify that port 22, 80 and 443 is open and that port 8000 is closed
check host myhost with address localhost
  if failed port 22 then alert
  if failed port 80 then alert
  if failed port 443 then alert
  if succeeded port 8000 then alert

答案2

以下 Monit 检查将尝试使用 TCP 连接到端口 8080 上的服务器 www.example.com。

if failed host www.example.com port 8080 then alert

如果连接操作失败,Monit 会生成错误并执行操作,在这种情况下它会发送警报。

以上内容在监控日志中显示如下:

error : socket_create: Could not create socket -- Operation now in progress error : 'portCheckTest' failed, cannot open a connection to INET[www.example.com:8080] via TCP

相关内容