官方網址:http://mmonit.com/monit/
當前版本:5.10
源代碼包:http://mmonit.com/monit/dist/
2進制包:http://mmonit.com/monit/dist/binary/
Monit是1款功能非常豐富的進程、文件、目錄和裝備的監測軟件,適用于Linux/Unix平臺。它可以自動修復那些已停止運作的程序,特別合適處理那些由于多種緣由致使的軟件毛病、監控系統關鍵的進程和資源。同時Monit 包括1個內嵌的 HTTP(S) Web 界面,你可使用閱讀器方便地查看 Monit 所監視的服務器。另外,M/Monit可以把多臺安裝Monit的服務器集中起來1起管理。
注:監控管理Python進程,經常使用的是supervisor(百度使用,自己定制)。
注:監控管理Ruby實現:god(小米)
注:M/Monit是需要收費的,可以避免費使用30天,網址:http://mmonit.com/。
注:M/Monit用戶文檔http://mmonit.com/documentation/mmonit_manual.pdf
注:M/Monit確當前版本為3.3,要求5.2以后的版本作為代理。
你可以用monit來監控進程,特別對監控守護進程很有用,比如:在系統啟動時間啟動的/etc/init.d;比如:sendmail,ssh,apache,mysql等
1)你可以用Monit來監控files,directories,文件系統,monit可以監控這些項目的改變,比如:時間戳,校驗和改變,文件大小改變,這樣比較安全,比如:你改變了文件的內容,那末它的md5或sha1校驗碼就會改變。
2)monit可以監控到各種服務器的網絡鏈接,本地或遠程,TCP還是UDP,Unix DomainSockets 都支持
3)monit可以用來在某些時候測試程序或腳本,你可以測試程序的返回值,并以此為根據,進行1些必要的操作,比如:履行某1個動作或發送1個警報
4)Monit可以用來監控1般的系統資源,比如CPU使用,內存,和負載均值(Load Acerage)
{
LoadAverage是CPU的Load,它所包括的信息不是CPU的使用率狀態,而是在1段時間內CPU正在處理和等待CPU處理的進程數之和的統計信息,也就是CPU使用隊列的長度的統計信息}
Debian或Ubuntu上安裝monit非常方便,履行下面的命令:
sudoapt-get install monit
其它*nix平臺上
1)直接下載對應平臺的2進制文件,可以直接使用。或
2)直接從源碼安裝,安裝命令以下:
./configure
make
make install
安裝完成后,Monit的默許配置文件為/etc/monit/monitrc。
Monit默許2分鐘(120秒)去檢查下服務并且把檢查結果寫入log文件中,log文件默許放在/var/log/monit.log中,這些內容都可以在配置文件中修改。
把需要監控的進程等信息添加到Monit的配置文件中,Monit配置可以參考下面的示例文件monitrc。
###############################################################################
## Monit control file
###############################################################################
#
# 檢查周期,默許為2分鐘,可以根據需要自行調理,這里把它改成30秒。
set daemon 30
# 日志文件
set logfile /var/log/monit.log
#
# 郵件通知服務器
#set mailserver mail.example.com
set mailserver localhost
# 通知郵件的格式設置,下面是默許格式供參考
set mail-format { from:webmaster@example.com }
# 設置郵件通知接收者。建議發到gmail,方便郵件過濾。
set alert userxxx@gmail.com
set httpd port 2812 and # 設置http監控頁面的端口
use address www.example.com #http監控頁面的IP或域名
allow localhost # 允許本地訪問
allow 58.68.78.0/24 # 允許此IP段訪問
##allow 0.0.0.0/0.0.0.0 # 允許任何IP段,不建議這樣干
allow userxxx:passwordxxx # 訪問用戶名密碼
# 系統整體運行狀態監控,默許的就能夠,可以自己去微調
#
# 系統名稱,可以是IP或域名
check system www.example.com
if loadavg (1min) > 4 then alert
if loadavg (5min) > 2 then alert
if memory usage > 75% then alert
if cpu usage (user) > 70% then alert
if cpu usage (system) > 30% then alert
if cpu usage(wait) > 20% then alert
#
# 監控nginx
#
# 需要提供進程pid文件信息
check process nginx with pidfile/var/run/nginx.pid
#進程啟動命令行,注:必須是命令全路徑
start program = "/etc/init.d/nginx start"
#進程關閉命令行
stop program ="/etc/init.d/nginx stop"
#nginx進程狀態測試,監測到nginx連不上了,則自動重啟
if failed host www.example.com port 80 protocol http then restart
#屢次重啟失敗將不再嘗試重啟,這類就是系統出現嚴重毛病的情況
if 3 restartswithin 5 cycles then timeout
# 如果程序使用cpu和內存比較利害,額外添加1些關于這方面的監控設置
if cpu > 50% for 2 cycles then alert
if cpu > 70% for 5 cycles then restart
if totalmem > 1500 MB for 10 cycles thenrestart
if children > 250 then restart
if loadavg(5min) greater than 10 for 20cycles then stop
if failed host www.example.com port 8080protocol http then restart
if 3 restarts within 5 cycles then timeout
#可選,設置分組信息
group server
include /etc/monit.d/* # 可以將其他配置放到這個目錄里包括進來
注:官方配置示例網址http://mmonit.com/wiki/Monit/ConfigurationExamples
在修改完monitrc配置文件后,我們需要履行下面的命令檢查monitrc的語法是不是正確:
# monit -t -c /etc/monitrc
Control file syntax OK
注意事項:
1)start和stop的program參數里的命令必須是全路徑,否則monit不能正常啟動,比如killall應當是/usr/bin/killall。
2)對spawn-fcgi,很多人會用它來管理PHP的fast-cgi進程,但spawn-fcgi本身也是有可能掛掉的,所以還是需要用monit來監控spawn-fcgi。spawn-fcgi必須帶-P參數才會有pid文件,而且fast-cgi走的不是http協議,monit的protocol參數也沒有cgi對應的設置,1定要去掉protocol http這項設置才管用。
3)進程屢次重啟失敗monit將不再嘗試重啟,收到這樣的通知郵件表明系統出現了嚴重的問題,要引發足夠的重視,需要趕快人工處理。
啟動monit監控履行下面命令:
# monit -c /etc/monitrc
其中:-c選項也能夠不加,不加monit默許會從~/monitrc、/etc/monitrc兩個位置去找配置文件。其他相干參數可通過monit -h查看。啟動完成后,可以通過http://IP:2812(端口可以在配置文件中進行更改)查看具體監控信息(默許用戶名和密碼是admin/monit)。
注意:如果使用防火墻,記得把2812端口加入到防火墻配置中。
Monit的使用方法以下:
# monit -h
Usage: monit [options] {arguments}
Options are as follows:
-cfile Use this control file
-dn Run as a daemon once per nseconds
-gname Set group name for start,stop, restart, monitor and unmonitor
-llogfile Print log information to thisfile
-ppidfile Use this lock file in daemonmode
-sstatefile Set the file monit shouldwrite state information to
-I Do not run in background (needed for run from init)
-t Run syntax check for the control file
-v Verbose mode, work noisy (diagnostic output)
-H[filename] Print SHA1 and MD5 hashes of the file or of stdin if the
filename is omited; monit willexit afterwards
-V Print version number and patchlevel
-h Print this text
Optional action arguments for non-daemonmode are as follows:
start all - Start all services
start name - Only start the named service
stopall - Stop all services
stopname - Only stop the named service
restart all - Stop and start all services
restart name - Only restart the named service
monitorall - Enable monitoring of allservices
monitor name - Only enable monitoring of the named service
unmonitor all - Disable monitoring of all services
unmonitor name - Only disable monitoring ofthe named service
reload - Reinitialize monit
status - Print full status information for each service
summary - Print short status information for each service
quit - Kill monit daemon process
validate - Check all services and start if not running
(Action arguments operate on servicesdefined in the control file)
注:詳細幫忙文件可以通過less monit⑸.5/man/man1/monit.1 查看man手冊。也能夠查看官方wiki頁面上的幫助文檔。
Monit使用C語言編寫而成,處理效力非常的高,占用資源非常少(幾近不占用資源),配置參數10分的簡單,只使用幾個if … then…語句就能夠完成監控任務。特別適用于對某些進程進行守護。例如:在檢測到http服務不正常時,自動重啟apache或nginx 。但監控功能上相對nagios略顯簡單。
http://mmonit.com/documentation/mmonit_manual.pdf
http://mmonit.com/monit/documentation/monit.pdf
http://mmonit.com/monit/documentation/monit.html
http://slides.com/tildeslash/monit#/
http://mmonit.com/wiki/Monit/FAQ