標籤: 警報

  • Linux 上使用 Monit 做監控

    Linux 上使用 Monit 做監控

    Monit 是一個不錯用的監控軟體,可以實作異常警報或是重啟等動作。網路上有滿多教學的,但是可能是名字的關係,google 時常常找到其他東西。這邊記錄一下。

    有幾個注意點:

    1. M/Monit 是另一款延伸的應用程式,需要付費但有更多功能
    2. 常用的功能:
      • monitrc 設定 stmp 和 alert 。提供警報通道。
      • 設定監控閾值和動作(例如要重啟/開/關或是 alert)
    3. 也有一些延伸功能,例如添加網頁存取,可以透過網頁查看。
    4. 最有需要的應該是 loadavg 和 mem, cpu 的警報。
    5. 有個缺點是無法做 server down/up 的警報。
    6. 重啟指令:monit reload
      • 如果 mail 設定成功,會收到一封重啟通知。
      • monitrc 設定中有 log 位置設定,debian 中預設 /var/log/monit.log 有任何問題可以先看 log

    目前我使用大多作為警報收信,應該是還沒遇到不穩定的系統需要實作自動重啟,算是一種成就??

    這裡貼上一個設定範本 + 說明:

    # 檢察系統效能
    check system $HOST
    
    # 主要設定中 daemon 參數設定 60s,2 cyvles 表示 120s
        if loadavg (5min) > 2 for 2 cycles then alert
        if loadavg (15min) > 2 for 2 cycles then alert
    
    # 有些 server 有 2 顆的 cpu 數字會需要設定超過 100%
        if cpu usage > 190% for 2 cycles then alert
        if memory usage > 85% for 1 cycles then alert
        if swap usage > 30% then alert
    
    # 監控網路行為,要注意 interface 在 server 中是哪個?
    check network public with interface ens4
        if failed link then alert
        if changed link then alert
        if saturation > 90% then alert
        if download > 10 MB/s for 10 cycles then alert
    
    # for 5 times within 30 cycles 意思是: 在 30 個循環裡面出現 5 次(滿足條件)的狀況下
        if upload > 20 MB/s for 5 times within 30 cycles then alert
    
    # 監控硬碟空間的行為, data_directory 是辨識用名稱, PATH 後面的路徑是要監控的路徑
    check filesystem data_directory PATH /data/
        if write rate > 50 MB/s for 5 cycles then alert
        if read rate > 50 MB/s for 5 cycles then alert
    # 因為通常 space 不太會降回去,所以設定的 cycles 數字比較大,相對要特別注意警告
        if space usage > 80% for 60 cycles then alert
    
    check filesystem root_directory PATH /
        if write rate > 50 MB/s for 5 cycles then alert
        if read rate > 50 MB/s for 5 cycles then alert
        if space usage > 80% for 60 cycles then alert

    附上實作顯示網頁的畫面截圖:

    同場加映找到的資源,以後可以少找一些吧。

    基於 centos 環境介紹和教學:

    基於 debian/ubuntu 環境這是介紹和教學(滿詳細的):

    這是延伸的監控服務,要付費的,但是功能更強大: