实时监控Iptables,防止人为关闭后,忘了开启,或者监控规则是否有增删。

在客户端(被监控端):

# cd /usr/local/nagios/etc

# vim nrpe.cfg 增加如下内容:

command[check_iptables]=/usr/local/nagios/libexec/check_iptables.sh

# cd /usr/local/nagios/libexec

# touch check_iptables.sh

# chmod 755 check_iptables.sh

# chown nagios:nagios check_iptables.sh

# vim check_iptables.sh ;增加如下内容:

---------------------------------

#!/bin/bash
local_iptables_md5="975fe1cb63de080b470a1073bebb0f56"  //首先获取iptables 开启状态下的MD5值
check_iptables_md5=`sudo /sbin/iptables -n -t filter -L|md5sum|awk '{print $1}'`
if [ $local_iptables_md5 == $check_iptables_md5 ]; then
        echo "OK - Iptables is OK"
        exit 0
else
        echo "CRITICAL - Iptables is CRITICAL"
        exit 2
fi

---------------------------------

脚本说明:先获取iptables的值,然后对比现在的值,如果一样,说明iptables状态正常,否则报异常;


# visudo  增加如下内容:

nagios ALL= NOPASSWD: /sbin/iptables -n -t filter -L

注:脚本调用了iptables命令,iptables默认只允许root调用,所有需要修改sudo。以上语句,表示只允许nagios用户不用密码使用该条命令。


在服务端(监控端):

在监控配置文件里新增如下内容:

define service {
        use                  web-service
        host_name             ip address
        service_description   iptables_status
        check_command           check_nrpe!check_iptables
        }

最后检测配置并重启nagios。


可能遇到的问题:

Status Information:

CRITICAL - Iptables are CRITICAL The Table filter and Chain has 0 rules configured

解决办法:

# visudo

找到 “Defaults    requiretty”字样,然后更改为:

Defaults:nagios    !requiretty

保存退出,即可!