nagios 实时监控 iptables 状态

【功能】:实时监控Iptables,防止人为关闭后,忘了开启,或者监控规则是否有增删。
【说明】脚本来至官方,这个脚本是通过获取iptables规则条数来判断iptables是否正常。运行参数:./check_iptables.sh -T filter -r 1(filter为表名) (1为规则条数)原脚本是当检测到的规则条数大于-r参数时提示正常,否则不正常。(我改成了当等于规则条数时,提示正常。如果你的规则是动态的增加的则可以改为大于时提示正常)同时将当前用户写入/var/log/iptables/iptables.log中。
ps:当然这个脚本缺点是不能监控规则更改,不过目前我采用了另外一种方法。暂时不公开。
【设置】 
在nrpe端
1、新建iptables监控日志目录 /var/log/iptables/
2、将check_iptables.sh(在下面或下载附件)脚本授权并修改属主放入..nagios/libexec/ 目录中。
修改nrpe.cfg 添加 
  1. command[check_iptables]=/usr/local/nagios/libexec/check_iptables.sh -T filter -r n  (n为条数)
脚本调用了iptables,iptables默认只允许root调用。所以需要修改sudo
使用visodu命令添加以下语句,表示只允许nagios用户不用密码使用该条命令(包括参数)。使用其它iptables参数是不行的,可以自己测试。最大程度保障安全。
  1. nagios ALLNOPASSWD: /sbin/iptables -n -t filter -L 
重启nrpe进程。
(如果遇到nagios没有权限调用iptables 则参考http://xikder.blog.51cto.com/1423200/785618处理方法) 
在nagios端
service中添加
  1. define service { 
  2.         use                  web-service 
  3.         host_name             XX 
  4.         service_description   iptables_status 
  5.         check_command         check_nrpe!check_iptables 
  6.         } 
检测配置并重启nagios。
【脚本】check_iptables.sh
  1. #!/bin/bash 
  2. # Developers: Rhommel Lamas 
  3. # Purpose: Nagios Plugin for Iptables Rules load check 
  4. # Version 0.5 
  5. # ---------------------------------------- License ----------------------------------------------------- 
  6. #  
  7. # This program is free software: you can redistribute it and/or modify 
  8. #   it under the terms of the GNU General Public License as published by 
  9. #   the Free Software Foundation, either version 3 of the License, or 
  10. #   (at your option) any later version. 
  11. #  This program is distributed in the hope that it will be useful, 
  12. #   but WITHOUT ANY WARRANTY; without even the implied warranty of 
  13. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  14. #   GNU General Public License for more details. 
  15. #   You should have received a copy of the GNU General Public License 
  16. #   along with this program.  If not, see <http://www.gnu.org/licenses/>.  
  17. # ---------------------------------------- Documentation ------------------------------------------------- 
  18. # Documentation about iptables: ~:# man iptables 
  19. # This scripts is intended to be used to check if your iptables rules are set correctly load at any time, 
  20. #   I didn't find a better way to check if a server has your rules loaded so I check the number of  
  21. #   configured rules and if they are less than they should be Nagios will send an alert using it  
  22. #   notify service. 
  23. #  
  24. # ----------------------------------------------------------------------------------------------------- 
  25. #  Plugin Description 
  26. # ----------------------------------------------------------------------------------------------------- 
  27. # This Plugin handled 2 States 
  28. #   OK - The number of Iprules equal o more than the minimun that we setup on the -r variable 
  29. #   CRITICAL - The number of IPrules are less than the minimun required. 
  30. #   UNKNOWN - It could be something about validation on the parameters 
  31. #  
  32. # This plugin also send and log every check to the file $LOG so if the plugins goes critical we can se who 
  33. # disable the iptables comparing the time with the auth file. 
  34. #---------------------------------------------------------------------------------------------------------- 
  35. #   Initialization 
  36. #---------------------------------------------------------------------------------------------------------- 
  37. PARAM1=$1 
  38. TABLE=$2 
  39. MINRULES=$3 
  40. PARAM4=$4 
  41. LOG=/var/log/iptables/iptables.log 
  42. CHKIPTBLS=`sudo /sbin/iptables -n -t filter -L |wc -l` 
  43.  
  44. # Parameter Validation 
  45. ## 
  46.  
  47. if [ "$PARAM1" != "-T" -o "$TABLE" == "" -o "$MINRULES" != "-r" -o "$PARAM4" == "" ]; then 
  48.         echo "Usage: $0 -T <table> -r <min rules>
  49.         echo "" 
  50.         exit 3 
  51.                 # Nagios exit code 3 = status UNKNOWN = orange 
  52.  
  53.  
  54. if [ "$PARAM1" == "-h" ]; then 
  55.         echo "" 
  56.         echo "      -h = Display's this Help" 
  57.         echo "      -T = Table to check"  
  58.         echo "               Available Tables:" 
  59.         echo "                  nat" 
  60.         echo "                  mangle" 
  61.         echo "                  filter"      
  62.         echo "      -r = Minimun quantity of rules" 
  63.         echo "" 
  64.         # Nagios exit code 3 = status UNKNOWN = orange 
  65.                 exit 3 
  66.    fi 
  67. fi 
  68.  
  69. ## 
  70. #   DO NOT MODIFY ANYTHING BELOW THIS 
  71. ## 
  72.  
  73. $CHKIPTBLS >/dev/null 2>/dev/null 
  74.  
  75. if [ "$CHKIPTBLS" == 0 ]; then 
  76.     TOTRULES=$CHKIPTBLS 
  77. else 
  78.     TOTRULES=$[$CHKIPTBLS-8] 
  79. fi 
  80.  
  81.  
  82. if [ "$TOTRULES" == "$PARAM4" ]; then 
  83.                     echo "OK - Iptables are OK The Table $TABLE and Chain $CHAIN has $TOTRULES rules configured" 
  84.                     # Nagios exit code 0 = status OK = green 
  85.                     exit 0 
  86. else 
  87.                     echo " CRITICAL - Iptables are CRITICAL The Table $TABLE and Chain $CHAIN has $TOTRULES rules configured" 
  88.                     for i in `w  -h | cut -f1 -d" " | sort | uniq` 
  89.                     do 
  90.                              
  91.                         echo "`date '+%d/%m/%Y - %H:%M:%S'` - CRITICAL - $i is logged in and there are only $TOTRULES loaded" >> $LOG 
  92.                     done 
  93.                     # Nagios exit code 2 = status CRITICAL = red 
  94.                     exit 2                 
  95. fi 

 附件已上传,在下面。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章