Monit監控工具使用簡介
Monit是一款功能非常豐富的進程、文件、目錄和設備的監測軟件,適用于Linux/Unix平臺。它可以自動修復那些已經停止運作的程序,特別適合處理那些由于多種原因導致的軟件錯誤、監控系統關鍵的進程和資源。同時Monit 包含一個內嵌的 HTTP(S) Web 界面,你可以使用瀏覽器方便地查看 Monit 所監視的服務器。此外,M/Monit可以把多臺安裝Monit的服務器集中起來一起管理。 功能
你可以用monit來監控進程,尤其對監控守護進程很有用,比如:在系統啟動時間啟動的/etc/init.d;比如:sendmail,ssh,apache,mysql等
1)你可以用Monit來監控files,directories,文件系統,monit可以監控這些項目的改變,比如:時間戳,校驗和改變,文件大小改變,這樣比較安全,比如:你改變了文件的內容,那么它的md5或者sha1校驗碼就會改變。
2)monit可以監控到各種服務器的網絡鏈接,本地或者遠程,TCP還是UDP,Unix DomainSockets 都支持
3)monit可以用來在某些時候測試程序或者腳本,你可以測試程序的返回值,并以此為依據,進行一些必要的操作,比如:執行某一個動作或者發送一個警報
4)Monit可以用來監控一般的系統資源,比如CPU使用,內存,以及負載均值(Load Acerage)
{
LoadAverage是CPU的Load,它所包含的信息不是CPU的使用率狀況,而是在一段時間內CPU正在處理以及等待CPU處理的進程數之和的統計信息,也就是CPU使用隊列的長度的統計信息}
配置
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和內存比較厲害,額外添加一些關于這方面的監控設置
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對應的設置,一定要去掉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.5/man/man1/monit.1 查看man手冊。也可以查看官方wiki頁面上的幫助文檔。
總結
Monit使用C語言編寫而成,處理效率非常的高,占用資源非常少(幾乎不占用資源),配置參數十分的簡單,只使用幾個if … then…語句就可以完成監控任務。尤其適用于對某些進程進行守護。例如:在檢測到http服務不正常時,自動重啟apache或nginx 。但監控功能上相對于nagios略顯簡單。
</span>