【问题标题】:Server Port Status Check Monit Configuration服务器端口状态检查监视器配置
【发布时间】:2018-02-05 06:43:06
【问题描述】:

我有两台服务器,其中运行着多个服务,我希望通过 monit 检查某些端口(相对于该服务)是否正在侦听。配置比较简单,monit检查端口状态,失败时monit会启动一个Nmap bash脚本,并将状态发送到文件中。另外,在同一个函数中,monit 会在 5 个周期后发送警报。

问题是在 monit 发送连接失败警报之后,然后在接下来的 5 分钟内 monit 再次发送连接成功警报。当我检查 Nmap 日志脚本时,没有端口失败(过滤或关闭)/端口状态始终处于打开状态。当 monit 发送失败警报时,我还使用 Nmap(无脚本)手动检查,但结果始终相同:端口状态为打开:

为什么 monit 在端口打开时总是发送失败警报,为什么在接下来的 5 分钟间隔内我看到连接成功?我将 set daemon 更改为 30,然后警报间隔变为 1.5 分钟,最后将 daemon 恢复为 300,但现在警报间隔始终为 1.5 分钟。

这是我对第一台服务器的 /etc/monitrc 配置(另一台服务器配置脚本完全相同)

set daemon  300              # check services at 300 seconds (5 minutes) intervals

check host somehost with address somehost.com
        start program = "/opt/monit/scripts/checkport.sh start"
        stop program = "/opt/monit/scripts/checkport.sh stop"
        if failed port 80 then restart
        if failed port 843 then restart
        if failed port 2121 then restart
        if failed port 8080 then restart
        if failed port 80 for 5 cycles then alert
        if failed port 843 for 5 cycles then alert
        if failed port 2121 for 5 cycles then alert
        if failed port 8080 for 5 cycles then alert
        alert username@example.com with reminder on 5 cycles 

这是我的 /opt/monit/checkport.sh 脚本,这是 example 的略微修改版本

#!/bin/bash

case $1 in
     start)
            nmap -p 80,843,2121,8080 -P0 somehost.com -oG-| awk 'NR>=6 && NR<=9 {print $1 "\t" $2}' | cat >> /opt/monit/log/checkedport | date >> /opt/monit/log/checkedport & echo $! > /var/run/checkport.pid ;
            ;;
     stop)
       pkill -F /var/run/checkport.pid ;;
     *)
       echo "usage: checkport {start|stop}" ;;
    esac
    exit 0 

【问题讨论】:

  • Monit 总是在下次检查失败后(每 300 秒)发送成功警报消息,通知问题已解决。每 5 个周期发出一次警报,间隔为 30 秒,即 2 分 30 秒。 “alert ...提醒”这一行是每5个周期发送一条消息以记住您失败的状态,否则它只发送一次消息(一次失败,一次成功)。对于失败,我不知道,也许尝试看看是否有一个端口失败,并定义检查类型(http、https、mysql等)。如果您不希望收到成功消息,请将 alert 替换为 exec 并向您发送邮件。

标签: linux bash port monit


【解决方案1】:

我认为不需要nmap端口检查脚本。

check host myhost with address 0.0.0.0
if failed port 80 then alert
if failed port 843 then alert
alert username@example.com with reminder on 5 cycles

像这样添加您的监控端口。它足以监视。当您的端口关闭时发出警报

【讨论】:

    【解决方案2】:

    以下对我有用 [在 Monit 中重新启动服务的端口监控]

    check host {any name to identify} with address 127.0.0.1
    start program = "/bin/systemctl start {service name}"
    stop program = "/bin/systemctl stop {service name}"
    if failed port {port number} then restart
    if 5 restarts within 5 cycles then timeout
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-29
      • 1970-01-01
      • 1970-01-01
      • 2016-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多