用monit監控系統關鍵進程

monit是一款功能強大的系統狀態、進程、文件、目錄和設備的監控軟件,用於*nix平臺, 它可以自動重啓那些已經掛掉的程序,非常適合監控系統關鍵的進程和資源,如:nginx、apache、mysql和cpu佔有率等。而監控管理Python進程,常用的是supervisor,後續會另外撰文介紹。

下面分別介紹monit的安裝、配置和啓動。

安裝

在debian或ubuntu上安裝monit非常方便,通過下面的命令

sudoapt-getinstall monit

即可,其它*nix上也很簡單,下載源碼走一遍安裝三步就OK了。

./configure
make
makeinstall

安裝後,默認的配置文件爲/etc/monit/monitrc。

配置

添加需要監控的進程等信息至monit的配置文件,monit的配置詳見下面的示例文件,爲了便於理解,關鍵的配置我都給出了中文的註釋。

##
## 飛龍日誌 示例monit配置文件,說明:
## 1. 域名以example.com爲例。
## 2. 後面帶xxx的均是舉例用的名字,需要根據自己的需要修改。
##
###############################################################################
## Monit control file
###############################################################################
#
# 檢查週期,默認爲2分鐘,對於網站來說有點長,可以根據需要自行調節,這改成30秒。
set daemon  30

# 日誌文件
set logfile /var/log/monit.log

#
# 郵件通知服務器
#
#set mailserver mail.example.com
set mailserver localhost          

#
# 通知郵件的格式設置,下面是默認格式供參考
#
## Monit by default uses the following alert mail format:
##
## --8<--
## From: monit@$HOST                         # sender
## Subject: monit alert --  $EVENT $SERVICE  # subject
##
## $EVENT Service $SERVICE                   #
##                                           #
##         Date:        $DATE                   #
##         Action:      $ACTION                 #
##         Host:        $HOST                   # body
##         Description: $DESCRIPTION            #
##                                           #
## Your faithful employee,                   #
## monit                                     #
## --8<--
##
## You can override the alert message format or its parts such as subject
## or sender using the MAIL-FORMAT statement. Macros such as $DATE, etc.
## are expanded on runtime. For example to override the sender:
#
# 簡單的,這隻改了一下發送人,有需要可以自己修改其它內容。
set mail-format { from: [email protected] }

# 設置郵件通知接收者。建議發到gmail,方便郵件過濾。
set alert [email protected]

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     # 訪問用戶名密碼

###############################################################################
## Services
###############################################################################
#
# 系統整體運行狀況監控,默認的就可以,可以自己去微調
#
# 系統名稱,可以是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 restarts within 5 cycles then timeout
    # 可選,設置分組信息
    group server

#   可選的ssl端口的監控,如果有的話
#    if failed port 443 type tcpssl protocol http
#       with timeout 15 seconds
#       then restart

#
# 監控apache
#
check process apache with pidfile /var/run/apache2.pid
    start program = "/etc/init.d/apache2 start"
    stop program  = "/etc/init.d/apache2 stop"
    # apache吃cpu和內存比較厲害,額外添加一些關於這方面的監控設置
    if cpu > 50% for 2 cycles then alert
    if cpu > 70% for 5 cycles then restart
    if totalmem > 1500 MB for 10 cycles then restart
    if children > 250 then restart
    if loadavg(5min) greater than 10 for 20 cycles then stop
    if failed host www.example.com port 8080 protocol http then restart
    if 3 restarts within 5 cycles then timeout
    group server
    # 可選,依賴於nginx
    depends on nginx

#
# 監控spawn-fcgi進程(其實就是fast-cgi進程)
#
check process spawn-fcgi with pidfile /var/run/spawn-fcgi.pid
    # spawn-fcgi一定要帶-P參數纔會生成pid文件,默認是沒有的
    start program = "/usr/bin/spawn-fcgi -a 127.0.0.1 -p 8081 -C 10 -u userxxx -g groupxxx -P /var/run/spawn-fcgi.pid -f /usr/bin/php-cgi"
    stop program = "/usr/bin/killall /usr/bin/php-cgi"
    # fast-cgi走的不是http協議,monit的protocol參數也沒有cgi對應的設置,這裏去掉protocol http即可。
    if failed host 127.0.0.1 port 8081 then restart
    if 3 restarts within 5 cycles then timeout
    group server
    depends on nginx

雖然在註釋裏有詳細說明,但是我還是要再強調說明幾點:

  1. start和stop的program參數裏的命令必須是全路徑,否則monit不能正常啓動,比如killall應該是/usr/bin/killall。

  1. 對於spawn-fcgi,很多人會用它來管理PHP的fast-cgi進程,但spawn-fcgi本身也是有可能掛掉的,所以還是需要用monit來監控spawn-fcgi。spawn-fcgi必須帶-P參數纔會有pid文件,而且fast-cgi走的不是http協議,monit的protocol參數也沒有cgi對應的設置,一定要去掉protocol http這項設置才管用。

  2. 進程多次重啓失敗monit將不再嘗試重啓,收到這樣的通知郵件表明系統出現了嚴重的問題,要引起足夠的重視,需要趕緊人工處理。

當然monit除了管理進程之外,還可以監控文件、目錄、設備等,本文不做討論,具體配置方式可以去參考monit的官方文檔

啓動、停止、重啓

標準的start、stop、restart

sudo/etc/init.d/monit start
sudo/etc/init.d/monit stop
sudo/etc/init.d/monit restart

看到正確的提示信息即可,若遇到問題可以去查看配置裏指定的日誌文件,如/var/log/monit.log。

從我的服務器這幾年的運行情況(monit發了的通知郵件)來看,nginx掛掉的事幾乎沒有,但apache或fast-cgi出問題的情況還是比較多見,趕快用上monit來管理你的服務器以提高服務器穩定性,跟502 Bad Gateway之類錯誤說拜拜吧。

上面基本都是從網上轉的啦。但安裝後如果想用gmail發郵件:配置如下

先monit -V 查看你的版本,不過如果你是新安裝的。應該都木問題的。 支持的版本是 monit version >= 4.10

然後配置如下:

set mailserver smtp.gmail.com port 587
    username "[email protected]" password "password"
    using tlsv1
    with timeout 30 seconds

  要注意的是: using 的是 tlsv1  大寫(TLSV1)。 前面是字母L後面是數字1  。


[root@aqua_node_1 upgrade]# egrep -v "^$|^#" /etc/monit.conf
set daemon  10              # check services at 1-minute intervals
set logfile /var/log/monit.log
set idfile /var/monit/id
set statefile /var/monit/state
set httpd port 2812 and
    use address localhost  # only accept connection from localhost
    allow localhost        # allow localhost to connect to the server and
    allow admin:monit      # require user 'admin' with password 'monit'
    allow @monit           # allow users of group 'monit' to connect (rw)
    allow @users readonly  # allow users of group 'users' to connect readonly
check process jetty with pidfile /xor/data0/aqua/bin/jetty.pid
    start program = "/etc/init.d/jetty start"
    stop program = "/etc/init.d/jetty stop"
    if 9 restarts within 10 cycles then timeout
    group server
include /etc/monit.d/*


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章