官方網(wǎng)址:http://mmonit.com/monit/
當(dāng)前版本:5.10
源代碼包:http://mmonit.com/monit/dist/
2進(jìn)制包:http://mmonit.com/monit/dist/binary/
Monit是1款功能非常豐富的進(jìn)程、文件、目錄和裝備的監(jiān)測軟件,適用于Linux/Unix平臺。它可以自動修復(fù)那些已停止運(yùn)作的程序,特別合適處理那些由于多種緣由致使的軟件毛病、監(jiān)控系統(tǒng)關(guān)鍵的進(jìn)程和資源。同時Monit 包括1個內(nèi)嵌的 HTTP(S) Web 界面,你可使用閱讀器方便地查看 Monit 所監(jiān)視的服務(wù)器。另外,M/Monit可以把多臺安裝Monit的服務(wù)器集中起來1起管理。
注:監(jiān)控管理Python進(jìn)程,經(jīng)常使用的是supervisor(百度使用,自己定制)。
注:監(jiān)控管理Ruby實現(xiàn):god(小米)
注:M/Monit是需要收費(fèi)的,可以避免費(fèi)使用30天,網(wǎng)址:http://mmonit.com/。
注:M/Monit用戶文檔http://mmonit.com/documentation/mmonit_manual.pdf
注:M/Monit確當(dāng)前版本為3.3,要求5.2以后的版本作為代理。
你可以用monit來監(jiān)控進(jìn)程,特別對監(jiān)控守護(hù)進(jìn)程很有用,比如:在系統(tǒng)啟動時間啟動的/etc/init.d;比如:sendmail,ssh,apache,mysql等
1)你可以用Monit來監(jiān)控files,directories,文件系統(tǒng),monit可以監(jiān)控這些項目的改變,比如:時間戳,校驗和改變,文件大小改變,這樣比較安全,比如:你改變了文件的內(nèi)容,那末它的md5或sha1校驗碼就會改變。
2)monit可以監(jiān)控到各種服務(wù)器的網(wǎng)絡(luò)鏈接,本地或遠(yuǎn)程,TCP還是UDP,Unix DomainSockets 都支持
3)monit可以用來在某些時候測試程序或腳本,你可以測試程序的返回值,并以此為根據(jù),進(jìn)行1些必要的操作,比如:履行某1個動作或發(fā)送1個警報
4)Monit可以用來監(jiān)控1般的系統(tǒng)資源,比如CPU使用,內(nèi)存,和負(fù)載均值(Load Acerage)
{
LoadAverage是CPU的Load,它所包括的信息不是CPU的使用率狀態(tài),而是在1段時間內(nèi)CPU正在處理和等待CPU處理的進(jìn)程數(shù)之和的統(tǒng)計信息,也就是CPU使用隊列的長度的統(tǒng)計信息}
Debian或Ubuntu上安裝monit非常方便,履行下面的命令:
sudoapt-get install monit
其它*nix平臺上
1)直接下載對應(yīng)平臺的2進(jìn)制文件,可以直接使用。或
2)直接從源碼安裝,安裝命令以下:
./configure
make
make install
安裝完成后,Monit的默許配置文件為/etc/monit/monitrc。
Monit默許2分鐘(120秒)去檢查下服務(wù)并且把檢查結(jié)果寫入log文件中,log文件默許放在/var/log/monit.log中,這些內(nèi)容都可以在配置文件中修改。
把需要監(jiān)控的進(jìn)程等信息添加到Monit的配置文件中,Monit配置可以參考下面的示例文件monitrc。
###############################################################################
## Monit control file
###############################################################################
#
# 檢查周期,默許為2分鐘,可以根據(jù)需要自行調(diào)理,這里把它改成30秒。
set daemon 30
# 日志文件
set logfile /var/log/monit.log
#
# 郵件通知服務(wù)器
#set mailserver mail.example.com
set mailserver localhost
# 通知郵件的格式設(shè)置,下面是默許格式供參考
set mail-format { from:webmaster@example.com }
# 設(shè)置郵件通知接收者。建議發(fā)到gmail,方便郵件過濾。
set alert userxxx@gmail.com
set httpd port 2812 and # 設(shè)置http監(jiān)控頁面的端口
use address www.example.com #http監(jiān)控頁面的IP或域名
allow localhost # 允許本地訪問
allow 58.68.78.0/24 # 允許此IP段訪問
##allow 0.0.0.0/0.0.0.0 # 允許任何IP段,不建議這樣干
allow userxxx:passwordxxx # 訪問用戶名密碼
# 系統(tǒng)整體運(yùn)行狀態(tài)監(jiān)控,默許的就能夠,可以自己去微調(diào)
#
# 系統(tǒng)名稱,可以是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
#
# 監(jiān)控nginx
#
# 需要提供進(jìn)程pid文件信息
check process nginx with pidfile/var/run/nginx.pid
#進(jìn)程啟動命令行,注:必須是命令全路徑
start program = "/etc/init.d/nginx start"
#進(jìn)程關(guān)閉命令行
stop program ="/etc/init.d/nginx stop"
#nginx進(jìn)程狀態(tài)測試,監(jiān)測到nginx連不上了,則自動重啟
if failed host www.example.com port 80 protocol http then restart
#屢次重啟失敗將不再嘗試重啟,這類就是系統(tǒng)出現(xiàn)嚴(yán)重毛病的情況
if 3 restartswithin 5 cycles then timeout
# 如果程序使用cpu和內(nèi)存比較利害,額外添加1些關(guān)于這方面的監(jiān)控設(shè)置
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
#可選,設(shè)置分組信息
group server
include /etc/monit.d/* # 可以將其他配置放到這個目錄里包括進(jìn)來
注:官方配置示例網(wǎng)址http://mmonit.com/wiki/Monit/ConfigurationExamples
在修改完monitrc配置文件后,我們需要履行下面的命令檢查monitrc的語法是不是正確:
# monit -t -c /etc/monitrc
Control file syntax OK
注意事項:
1)start和stop的program參數(shù)里的命令必須是全路徑,否則monit不能正常啟動,比如killall應(yīng)當(dāng)是/usr/bin/killall。
2)對spawn-fcgi,很多人會用它來管理PHP的fast-cgi進(jìn)程,但spawn-fcgi本身也是有可能掛掉的,所以還是需要用monit來監(jiān)控spawn-fcgi。spawn-fcgi必須帶-P參數(shù)才會有pid文件,而且fast-cgi走的不是http協(xié)議,monit的protocol參數(shù)也沒有cgi對應(yīng)的設(shè)置,1定要去掉protocol http這項設(shè)置才管用。
3)進(jìn)程屢次重啟失敗monit將不再嘗試重啟,收到這樣的通知郵件表明系統(tǒng)出現(xiàn)了嚴(yán)重的問題,要引發(fā)足夠的重視,需要趕快人工處理。
啟動monit監(jiān)控履行下面命令:
# monit -c /etc/monitrc
其中:-c選項也能夠不加,不加monit默許會從~/monitrc、/etc/monitrc兩個位置去找配置文件。其他相干參數(shù)可通過monit -h查看。啟動完成后,可以通過http://IP:2812(端口可以在配置文件中進(jìn)行更改)查看具體監(jiān)控信息(默許用戶名和密碼是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)
注:詳細(xì)幫忙文件可以通過less monit⑸.5/man/man1/monit.1 查看man手冊。也能夠查看官方wiki頁面上的幫助文檔。
Monit使用C語言編寫而成,處理效力非常的高,占用資源非常少(幾近不占用資源),配置參數(shù)10分的簡單,只使用幾個if … then…語句就能夠完成監(jiān)控任務(wù)。特別適用于對某些進(jìn)程進(jìn)行守護(hù)。例如:在檢測到http服務(wù)不正常時,自動重啟apache或nginx 。但監(jiān)控功能上相對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