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 

 附件已上傳,在下面。

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