1. 程式人生 > 實用技巧 >monit 配置詳解(monitrc)

monit 配置詳解(monitrc)

monitrc是Monit的主配置檔案(控制檔案)。
monitrc的內容主要分為全域性(golbal)和服務(services)兩個部分。
預設情況下monitrc檔案在/etc/monit目錄下。

###############################################################################
## 配置內容書寫規範
###############################################################################
# 註釋以“#”開頭並延伸到行尾
# 關鍵字不區分大小寫
# 所有路徑必須完全限定路徑,以“/”開頭

###############################################################################
## Global section 全域性部分
###############################################################################
  set daemon 120
  # 設定以守護程序的方式執行Monit,每隔2分鐘檢查一次服務
  #   with start delay 240    
  # 第一次啟動時延遲檢查4分鐘(預設情況下,Monit啟動後立即進行檢查)
  set logfile /var/log/monit.log
  # 設定系統日誌記錄。如果要改為記錄到獨立的日誌檔案,請指定日誌檔案的完整路徑
  # set pidfile /var/run/monit.pid
  # 設定Monit例項的PID檔案路徑,PID檔案記錄了Monit例項程序的程序編號(PID),預設情況下,檔案放置在
  # $HOME/.monit.pid
  set idfile /var/lib/monit/id
  # 設定Monit例項的id檔案路徑。id檔案記錄了Monit例項的唯一ID(標識),id在第一次Monit啟動時生成並存儲。默# 認情況下,檔案放置在$HOME/.monit.id
  set statefile /var/lib/monit/state
  # 設定在每個週期中儲存監視狀態的Monit狀態檔案的位置。預設情況下,檔案放在$HOME/.monit.state. 如果狀態文
  # 件儲存在持久檔案系統中,Monit在重新啟動時將恢復監視狀態。如果它在臨時檔案系統上,狀態將在重新啟動時丟
  # 失。

  # set limits {
  #     programOutput:     512 B,      # 檢查程式輸出截斷限制
  #     sendExpectBuffer:  256 B,      # send/expect協議測試限制
  #     fileContentBuffer: 512 B,      # 檔案內容緩衝區限制
  #     httpContentBuffer: 1 MB,       # HTTP內容緩衝區限制
  #     networkTimeout:    5 seconds   # 網路I/O超時限制
  #     programTimeout:    300 seconds # 檢查程式超時限制
  #     stopTimeout:       30 seconds  # 服務停止超時限制
  #     startTimeout:      30 seconds  # 服務啟動超時限制
  #     restartTimeout:    30 seconds  # 服務重啟超時限制
  # }
  # 設定各種限制。以上示例顯示了預設值。

  # set ssl {
  #     verify     : enable, # 驗證SSL證書(預設禁用,但強烈建議)
  #     selfsigned : allow   # 允許自簽名SSL證書(預設情況下拒絕)
  # }
  # 設定全域性SSL選項

  # set mailserver mail.bar.baz,               # 主郵件伺服器
  #                backup.bar.baz port 10025,  # 備郵件伺服器 on port 10025
  #                localhost                   # 如果沒有郵件伺服器就將警報傳送到本地
  # 設定要傳遞警報的郵件伺服器列表。可以使用逗號分隔符指定多個伺服器。如果第一個郵件伺服器發生故障,Monit
  # 將使用列表中的第二個郵件伺服器,依此類推。預設情況下,Monit可以使用port選項覆蓋它

  set eventqueue
      basedir /var/lib/monit/events # 設定儲存事件的基本目錄
      slots 100                     # 可選擇限制佇列大小
  # 預設情況下,如果沒有可用的郵件伺服器,Monit將刪除警報事件。如果要保留警報以便以後重試傳遞,可以使
  # 用"eventqueue"語句。儲存未送達警報的基本目錄由"basedir"選項指定。您可以使用"slots"選項來限制佇列大小
  # (如果省略,佇列將受到後端檔案系統中可用空間的限制)

  # set mmonit http://monit:[email protected]:8080/collector
  #     # with timeout 30 seconds              # 預設超時5秒
  #     # and register without credentials     # 不註冊憑據
  # 設定與M/Monit通訊,向M/Monit傳送狀態和事件(有關M/Monit的更多資訊,請參閱http://mminit.com/). 預設情
  # 況下,Monit使用M/Monit註冊憑據,以便M/Monit可以順利地與Monit通訊,並且您不必在M/Monit中手動註冊Monit
  # 憑據。可以使用下面的註釋掉選項禁用憑證註冊。但是,如果安全是一個問題,我們建議在與M/Monit通訊時使用
  # https,並加密傳送憑據。一般不使用。

  # set mail-format {
  #   from:    monit@$HOST
  #   subject: monit alert --  $EVENT $SERVICE
  #   message: $EVENT Service $SERVICE
  #                 Date:        $DATE
  #                 Action:      $ACTION
  #                 Host:        $HOST
  #                 Description: $DESCRIPTION
  #
  #            Your faithful employee,
  #            Monit
  # }
  # 設定郵件格式,預設情況下,如果缺少郵件格式"mail-format"語,Monit將使用以上格式傳送警報。

  # set alert [email protected]                                     # 接受所有警報
  # set alert [email protected] not on { instance, action }  # 不接受篩選器{}中的的警報
  # 設定警報關聯的郵箱和篩選器,如果不設定任何篩選器則關聯郵箱接受所有警報。
  # 當Monit啟動、停止或執行使用者啟動的操作時,不要發出警報。建議使用上面第二個篩選器,以避免在一般情況下收
  # 到警報。

  # set httpd port 2812 and
  #     use address localhost  # 僅接受本地主機的連線
  #     allow localhost        # 允許從localhost訪問HTTP介面
  #     allow admin:monit      # 當管理者通過WEB訪問時,需要使用賬號密碼驗證身份"admin:monit"
  # Monit有一個嵌入式HTTP介面,可用於從WEB介面檢視所監視服務的狀態和管理服務。如果要從命令列發出Monit命
  # 令,例如“monit status”或“monit restart service”,則也需要HTTP介面。原因是Monit客戶端使用HTTP介面
  # 將這些命令傳送到正在執行的Monit守護程式。如果要為HTTP介面啟用SSL,請參閱monitwiki。

###############################################################################
## Services 服務部分
###############################################################################
# 以下是一些配置示例。

  #  check system $HOST
  #    if loadavg (1min) > 4 then alert
  #    if loadavg (5min) > 2 then alert
  #    if cpu usage > 95% for 10 cycles then alert
  #    if memory usage > 75% then alert
  #    if swap usage > 25% then alert
  # 檢查一般系統資源,如平均負載、cpu和記憶體使用情況。每個測試都指定一個資源、條件和測試失敗時要執行的操
  # 作。

  #  check file apache_bin with path /usr/local/apache/bin/httpd
  #    if failed checksum and 
  #       expect the sum 8f7f419955cefa0b33a2ba316cba3659 then unmonitor
  #    if failed permission 755 then unmonitor
  #    if failed uid root then unmonitor
  #    if failed gid root then unmonitor
  #    alert [email protected] on {
  #           checksum, permission, uid, gid, unmonitor
  #        } with the mail-format { subject: Alarm! }
  #    group server
  # 檢查檔案是否存在、校驗和、許可權、uid和gid。除了全域性部分中的警報收件人外,還可以通過指定本地警報處理程
  # 序將自定義警報傳送到其他收件人。可以使用GROUP選項對服務進行分組。可以通過重複“group name”語句指定多
  # 個組。

  #  check process apache with pidfile /usr/local/apache/logs/httpd.pid
  #    start program = "/etc/init.d/httpd start" with timeout 60 seconds
  #    stop program  = "/etc/init.d/httpd stop"
  #    if cpu > 60% for 2 cycles then alert
  #    if cpu > 80% for 5 cycles then restart
  #    if totalmem > 200.0 MB for 5 cycles then restart
  #    if children > 250 then restart
  #    if loadavg(5min) greater than 10 for 8 cycles then stop
  #    if failed host www.tildeslash.com port 80 protocol http 
  #       and request "/somefile.html"
  #    then restart
  #    if failed port 443 protocol https with timeout 15 seconds then restart
  #    if 3 restarts within 5 cycles then unmonitor
  #    depends on apache_bin
  #    group server
  # 檢查程序是否正在執行(在本例中為Apache),以及它是否響應HTTP和HTTPS請求。檢查其資源使用情況,如cpu和
  # 記憶體,以及子程序的數量。如果程序沒有執行,Monit將在預設情況下重新啟動它。如果服務經常重新啟動而問題仍
  # 然存在,可以使用"unmonitor"語句禁用監視。此服務依賴於上面定義的另一個服務(apache_bin)

  #  check filesystem datafs with path /dev/sdb1
  #    start program  = "/bin/mount /data"
  #    stop program  = "/bin/umount /data"
  #    if failed permission 660 then unmonitor
  #    if failed uid root then unmonitor
  #    if failed gid disk then unmonitor
  #    if space usage > 80% for 5 times within 15 cycles then alert
  #    if space usage > 99% then stop
  #    if inode usage > 30000 then alert
  #    if inode usage > 99% then stop
  #    group server
  # 檢查檔案系統許可權、uid、gid、空間和inode使用情況。其他服務(如資料庫)可能依賴於此資源,在檔案系統變滿
  # 和資料丟失之前,可能會向這些服務級聯自動優雅的停止。

  #  check file database with path /data/mydatabase.db
  #    if failed permission 700 then alert
  #    if failed uid data then alert
  #    if failed gid data then alert
  #    if timestamp > 15 minutes then alert
  #    if size > 100 MB then exec "/my/cleanup/script" as uid dba and gid dba
  # 檢查檔案的時間戳。在這個例子中,我們測試一個檔案是否超過15分鐘,如果它沒有被更新,我們會假設它有問
  # 題。另外,如果檔案大小超過給定的限制,請執行指令碼

  #  check directory bin with path /bin
  #    if failed permission 755 then unmonitor
  #    if failed uid 0 then unmonitor
  #    if failed gid 0 then unmonitor
  # 檢查目錄許可權、uid和gid。如果目錄不屬於uid為0且gid為0的使用者,則會觸發事件。

  #  check host myserver with address 192.168.1.1
  #    if failed ping then alert
  #    if failed port 3306 protocol mysql with timeout 15 seconds then alert
  #    if failed port 80 protocol http
  #       and request /some/path with content = "a string"
  #    then alert
  # 通過發出ping測試檢查遠端主機的可用性,並檢查來自web伺服器的響應的內容。最多傳送三個ping,並執行到埠
  # 的連線和應用程式級網路檢查

  #  check network public with interface eth0
  #    if failed link then alert
  #    if changed link then alert
  #    if saturation > 90% then alert
  #    if download > 10 MB/s then alert
  #    if total upload > 1 GB in last hour then alert
  # 檢查網路鏈路狀態(上/下)、鏈路容量變化、飽和和頻寬使用情況.

  #  check program myscript with path /usr/local/bin/myscript.sh
  #    if status != 0 then alert
  # 檢查自定義程式狀態輸出,執行指令碼"/usr/local/bin/myscript.sh",如果指令碼執行完成後系統的狀態值不等於0,
  # 則報警

###############################################################################
## Includes 附加部分
###############################################################################
# 可以使用"include"包含來自其他檔案或目錄的附加配置檔案,將部分配置獨立一個配置檔案,便於管理。
   include /etc/monit/conf.d/*
   include /etc/monit/conf-enabled/*