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:monit@192.168.1.10: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 sysadm@foo.bar # 接受所有警報
# set alert your-name@your.domain 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 security@foo.bar 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/*