服务器端口状态检查监控配置

时间:2017-08-28 03:37:30

标签: linux bash port monit

我有两个服务器,其中运行了多个服务,我希望monit检查某些端口(相对于该服务)是否正在监听monit。配置比较简单,monit check port status,当monit失败时会启动一个Nmap bash脚本并将状态发送给该文件。另外,在相同的功能中,monit将在5个周期后发送警报。

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

为什么monit总是在端口打开时发送故障警报,为什么在接下来的5分钟间隔内我看到连接成功了?我已将set daemon更改为30,然后警报间隔变为1.5分钟,最后将守护程序恢复为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 

2 个答案:

答案 0 :(得分:1)

以下为我工作[在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

答案 1 :(得分:0)

我认为不需要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

这样添加您的监视端口。它足以监视。当您的端口出现故障时发出警报