nagios 流量監控和警示的shell指令碼

來源:互聯網
上載者:User

標籤:nagios指令碼 異常流量警示

上下文:我們平台的監控系統用的是cacti+nagios。之前沒有加流量異常警示設定,cacti上到是有外掛程式可以時間警示,但是無法使用我們自己的簡訊介面(nagios用的購買的簡訊介面),所以想自己寫一個指令碼配合nagios實現基本的流量異常警示。

指令碼思路:/proc/net/dev取出當前流經網卡的(接收和發送)的kb總數量,在活動訊號間隔時間時間後再次讀取這兩個值,
相減既是間隔時間段內的增量,再根據此增量做計算,算出間隔時間內的平均流量,和基準流量作比較,觸發nagios警示事件

局限性:我們只檢測外網卡的流量,且預設em1網卡為外網卡(需根據各位實際情況自行調整)

#!/bin/bash   #預設第一塊網卡為外網卡,檢查當前系統是以eth0還是em1作為網卡一,後面會引用網卡名   interface=`ip a|grep -e "eth0" -e "em1"|awk ‘{print $NF}‘|tail -1`      #定義儲存結果的函數,以便在任何異常/正常退出前都能儲存最新的記錄   function tmp_store {       chmod 777 /tmp/receive /tmp/transfer &> /dev/null        #以防以root使用者驗證指令碼時以root建立這兩個檔案,等nagios以nagios使用者呼叫指令碼的時候無法寫入新的記錄值,導致檢測結果不準       cat /proc/net/dev|grep "$interface"|awk ‘{print $1}‘|cut -d ":" -f 2 > /tmp/receive       cat /proc/net/dev|grep "$interface"|awk ‘{print $9}‘ > /tmp/transfer   }   #將當時流量統計記錄於tmp,然後由nagios呼叫指令碼,定時(5分鐘,遇異常隔5分鐘再檢測)採集新的統計值,   做計算統計出時間段內流量的平均值   RX_bytes_last=`cat /tmp/receive`   TX_bytes_last=`cat /tmp/transfer`   RX_bytes=`cat /proc/net/dev|grep "$interface"|awk ‘{print $1}‘|cut -d ":" -f 2`   TX_bytes=`cat /proc/net/dev|grep "$interface"|awk ‘{print $9}‘`   speed_RX=`echo "scale=0;($RX_bytes - $RX_bytes_last)*8/1024/1024/300"|bc`     #300即5分鐘,nagios 每五分鐘檢測一次流量,統計出來的 單位為Mb   speed_TX=`echo "scale=0;($TX_bytes - $TX_bytes_last)*8/1024/1024/300"|bc`       if   [ $speed_RX -gt 5  -a $speed_TX -gt 5 ];then #此處的5,所有對比值,是根據cacti統計出來的基準值        echo "speed_RX=$speed_RX, speed_TX=$speed_TX. both great than normal"        tmp_store;        exit 2   elif [ $speed_RX -gt 5 -o $speed_TX -gt 5 ];then        if [ $speed_RX -gt 5 ];then           echo "receive is $speed_RX Mbps, great than 5Mbps"           tmp_store;           exit 2        else           echo "transfer is $speed_TX Mbps, great than 5Mbps"           tmp_store;           exit 2        fi   else         echo "OK speed_RX=$speed_RX Mbps, speed_TX=$speed_TX Mbps"        tmp_store;        exit 0   fi


nagios 流量監控和警示的shell指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.