Monit 官網(wǎng):https://mmonit.com/monit/
一、Monit 介紹
Monit是用于管理和監(jiān)視Unix系統(tǒng)上的進(jìn)程,程序,文件,目錄和文件系統(tǒng)的實(shí)用程序。Monit會(huì)進(jìn)行自動(dòng)維護(hù)和修復(fù),并且可以在錯(cuò)誤情況下執(zhí)行有意義的因果操作。例如,Monit可以在不運(yùn)行的情況下啟動(dòng)進(jìn)程,在不響應(yīng)的情況下重啟進(jìn)程,在使用過(guò)多資源的情況下停止進(jìn)程。您可以使用Monit監(jiān)視文件,目錄和文件系統(tǒng)的更改,例如時(shí)間戳更改,校驗(yàn)和更改或大小更改。
Monit通過(guò)基于自由格式,面向令牌的語(yǔ)法的易于配置的控制文件進(jìn)行控制。Monit記錄到syslog或自己的日志文件中,并通過(guò)可自定義的警報(bào)消息通知您有關(guān)錯(cuò)誤情況的信息。Monit可以執(zhí)行各種TCP / IP網(wǎng)絡(luò)檢查,協(xié)議檢查,并且可以使用SSL進(jìn)行此類(lèi)檢查。Monit提供HTTP(S)接口,您可以使用瀏覽器訪(fǎng)問(wèn)Monit程序。
二、Monit 特色
- 超輕量, 穩(wěn)定, 高可用
- 依賴(lài)少, 安裝配置方便, 盡量減少運(yùn)維及學(xué)習(xí)成本(即使沒(méi)有任何 Monit 基礎(chǔ)的人, 都能輕易的讀懂大部分監(jiān)控文件)
- 非侵入式, 被監(jiān)控的程序可以不用知道監(jiān)控程序的存在(如果使用 Supervisor 監(jiān)控, 則服務(wù)必須從 Supervisor 啟動(dòng))
- 基本功能完備(進(jìn)程 、文件 、Fifo、 文件系統(tǒng)、目錄、遠(yuǎn)程主機(jī)、系統(tǒng)、程序 、網(wǎng)絡(luò)
9 種類(lèi)型監(jiān)控, 郵件報(bào)警, 支持用戶(hù)自定義 shell 擴(kuò)展)
三、Monit 和 Supervisor 的對(duì)比
進(jìn)程守護(hù)
-
supervisor:
優(yōu)點(diǎn):采用托管的方式守護(hù),一旦進(jìn)程異常能馬上檢查出來(lái),可以指定目錄,對(duì)啟動(dòng)命令本身修改不大缺點(diǎn):只能啟動(dòng)前臺(tái)進(jìn)程,一旦supervisor發(fā)生重啟會(huì)再次啟動(dòng)進(jìn)程,導(dǎo)致進(jìn)程再次被啟動(dòng)(不知道有沒(méi)有辦法避免,)
-
monit:
優(yōu)點(diǎn):有基于pid文件和正則匹配的檢查進(jìn)程,不會(huì)導(dǎo)致進(jìn)程重復(fù)啟動(dòng)缺點(diǎn):通過(guò)定時(shí)檢查的方式檢查進(jìn)程,存在一定延時(shí),另外啟動(dòng)命令必須要包含完整的路徑
提醒
- supervisor:需要使用第三方插件才能實(shí)現(xiàn)郵件提醒功能,本身并不支持郵件提醒
- monit:原生支持郵件提醒功能
磁盤(pán)監(jiān)控
- supervisor:本身只是為了守護(hù)進(jìn)程,所以需要第三方擴(kuò)展才可以監(jiān)控磁盤(pán)
- monit:原生支持磁盤(pán)監(jiān)控
輕量級(jí)占用少
- supervisor:基于Python開(kāi)發(fā),輕量級(jí)
- monit:輕量級(jí)
其他
2款軟件都提供了網(wǎng)頁(yè)管理的功能,monit能在頁(yè)面中直接對(duì)進(jìn)程進(jìn)行重啟,停止和啟動(dòng)操作,supervisor能直接在頁(yè)面中查看軟件輸出的日志
四、Monit 使用
命令行
Usage: monit [options]+ [command]
選項(xiàng)
Monit可以識(shí)別以下選項(xiàng)。但是,建議您直接在.monitrc控制文件中設(shè)置選項(xiàng)(如果適用)
-c file 文件使用此控制文件
-d n 每n秒一次將 Monit 作為守護(hù)程序運(yùn)行。或在monitrc中使用“ set daemon”
-g name 設(shè)置用于啟動(dòng),停止,重新啟動(dòng),監(jiān)視,取消監(jiān)視,狀態(tài)和摘要操作的組名
-l logfile 將日志信息打印到此文件。或在monitrc中使用“設(shè)置日志”
-p pidfile 在守護(hù)程序模式下使用此鎖定文件?;蛟趍onitrc中使用“ set pidfile”
-s statefile 將狀態(tài)信息寫(xiě)入此文件?;蛟趍onitrc中使用“設(shè)置狀態(tài)文件”
-I 不要在后臺(tái)模式下運(yùn)行(需要從init運(yùn)行)?;蛟趍onitrc中使用“ set init”
--id 打印Monit的唯一ID
--resetid 重置Monit的唯一ID。謹(jǐn)慎使用
-B 批處理命令行模式(無(wú)表格輸出,無(wú)顏色)?;蛟趍onitrc中使用“設(shè)置終端批處理”
-t 運(yùn)行控制文件的語(yǔ)法檢查
-v 詳細(xì)模式(診斷輸出)
-vv 非常詳細(xì)的模式,與-v相同,并在錯(cuò)誤時(shí)記錄日志堆棧跟蹤
-H [filename] 如果省略文件名,則打印文件或標(biāo)準(zhǔn)輸入的MD5和SHA1散列;Monit之后將退出
-V 打印版本號(hào)和補(bǔ)丁程序級(jí)別
-h 打印幫助文本
參數(shù):
將Monit作為守護(hù)進(jìn)程運(yùn)行后,可以使用以下參數(shù)之一調(diào)用Monit。
然后,Monit將連接到Monit守護(hù)程序(默認(rèn)情況下位于TCP端口127.0.0.1:2812上),
并要求Monit守護(hù)程序執(zhí)行所請(qǐng)求的操作。換一種說(shuō)法; 調(diào)用不帶參數(shù)的monit將啟動(dòng)Monit守護(hù)程序,
而帶參數(shù)的monit則使您能夠與Monit守護(hù)進(jìn)程進(jìn)行通信。
start all 啟動(dòng)控制文件中列出的所有服務(wù),并啟用對(duì)它們的監(jiān)視。如果設(shè)置了組選項(xiàng)(-g),則僅啟動(dòng)并啟用對(duì)已命名組中服務(wù)的監(jiān)視(在這種情況下,不需要“ all”)
start <name> 啟動(dòng)命名服務(wù)并啟用對(duì)其的監(jiān)視。該名稱(chēng)是monitrc文件中的服務(wù)條目名稱(chēng)
stop all 停止控制文件中列出的所有服務(wù)并禁用其監(jiān)視。如果設(shè)置了組選項(xiàng),則僅停止和禁用對(duì)命名組中服務(wù)的監(jiān)視(在這種情況下,不需要“全部”)
stop <name> 停止命名服務(wù)并禁用其監(jiān)視。該名稱(chēng)是monitrc文件中的服務(wù)條目名稱(chēng)
restart all 停止并啟動(dòng)所有服務(wù)。如果設(shè)置了組選項(xiàng),則僅重新啟動(dòng)命名組中的服務(wù)(在這種情況下,不需要“全部”)
restart <name> 重新啟動(dòng)命名服務(wù)。該名稱(chēng)是monitrc文件中的服務(wù)條目名稱(chēng)
monitor all 啟用監(jiān)視控制文件中列出的所有服務(wù)。如果設(shè)置了組選項(xiàng),則僅開(kāi)始監(jiān)視命名組中的服務(wù)(在這種情況下,不需要“全部”)
monitor <name> 啟用監(jiān)視命名服務(wù)。該名稱(chēng)是monitrc文件中的服務(wù)條目名稱(chēng)。Monit還可以監(jiān)視該服務(wù)所依賴(lài)的所有服務(wù)
unmonitor all 禁用監(jiān)視控制文件中列出的所有服務(wù)。如果設(shè)置了組選項(xiàng),則僅禁用監(jiān)視命名組中的服務(wù)(在這種情況下,不需要“全部”)
unmonitor <name> 禁用對(duì)命名服務(wù)的監(jiān)視。該名稱(chēng)是monitrc文件中的服務(wù)條目名稱(chēng)。Monit還將禁用對(duì)依賴(lài)此服務(wù)的所有服務(wù)的監(jiān)視
reload 重新初始化正在運(yùn)行的Monit守護(hù)程序,該守護(hù)程序?qū)⒅匦伦x取其配置,關(guān)閉并重新打開(kāi)日志文件
status [name] 打印服務(wù)狀態(tài)信息
summary [name] 打印簡(jiǎn)短的狀態(tài)摘要
report [up|down|..] 報(bào)告服務(wù)狀態(tài)。腳本可以輕松解析輸出。如果沒(méi)有選項(xiàng),則打印由Monit管理的所有服務(wù)的狀態(tài)的簡(jiǎn)短概述。該選項(xiàng)up打印此狀態(tài)下所有服務(wù)的數(shù)量,同樣打印down等等
quit 終止Monit守護(hù)進(jìn)程
validate 檢查控制文件中列出的所有服務(wù)。當(dāng)Monit在守護(hù)程序模式下運(yùn)行時(shí),此操作也是默認(rèn)行為
procmatch <pattern> 允許輕松測(cè)試模式以進(jìn)行過(guò)程匹配檢查。該命令將正則表達(dá)式作為參數(shù),并顯示所有與模式匹配的正在運(yùn)行的進(jìn)程
配置文件
###############################################################################
## Monit control file
###############################################################################
##
## Comments begin with a '#' and extend through the end of the line. Keywords
## are case insensitive. All path's MUST BE FULLY QUALIFIED, starting with '/'.
##
## Below you will find examples of some frequently used statements. For
## information about the control file and a complete list of statements and
## options, please have a look in the Monit manual.
##
##
###############################################################################
## Global section
###############################################################################
##
## Start Monit in the background (run as a daemon):
#
set daemon 30 # check services at 30 seconds intervals
# with start delay 240 # optional: delay the first check by 4-minutes (by
# # default Monit check immediately after Monit start)
#
#
## Set syslog logging. If you want to log to a standalone log file instead,
## specify the full path to the log file
#
set log syslog
#
#
## Set the location of the Monit lock file which stores the process id of the
## running Monit instance. By default this file is stored in $HOME/.monit.pid
#
# set pidfile /var/run/monit.pid
#
## Set the location of the Monit id file which stores the unique id for the
## Monit instance. The id is generated and stored on first Monit start. By
## default the file is placed in $HOME/.monit.id.
#
# set idfile /var/.monit.id
#
## Set the location of the Monit state file which saves monitoring states
## on each cycle. By default the file is placed in $HOME/.monit.state. If
## the state file is stored on a persistent filesystem, Monit will recover
## the monitoring state across reboots. If it is on temporary filesystem, the
## state will be lost on reboot which may be convenient in some situations.
#
# set statefile /var/.monit.state
#
#
## Set limits for various tests. The following example shows the default values:
##
# set limits {
# programOutput: 512 B, # check program's output truncate limit
# sendExpectBuffer: 256 B, # limit for send/expect protocol test
# fileContentBuffer: 512 B, # limit for file content test
# httpContentBuffer: 1 MB, # limit for HTTP content test
# networkTimeout: 5 seconds # timeout for network I/O
# programTimeout: 300 seconds # timeout for check program
# stopTimeout: 30 seconds # timeout for service stop
# startTimeout: 30 seconds # timeout for service start
# restartTimeout: 30 seconds # timeout for service restart
# }
## Set global SSL options (just most common options showed, see manual for
## full list).
#
# set ssl {
# verify : enable, # verify SSL certificates (disabled by default but STRONGLY RECOMMENDED)
# selfsigned : allow # allow self signed SSL certificates (reject by default)
# }
#
#
## Set the list of mail servers for alert delivery. Multiple servers may be
## specified using a comma separator. If the first mail server fails, Monit
# will use the second mail server in the list and so on. By default Monit uses
# port 25 - it is possible to override this with the PORT option.
#
# set mailserver mail.bar.baz, # primary mailserver
# backup.bar.baz port 10025, # backup mailserver on port 10025
# localhost # fallback relay
#
#
## By default Monit will drop alert events if no mail servers are available.
## If you want to keep the alerts for later delivery retry, you can use the
## EVENTQUEUE statement. The base directory where undelivered alerts will be
## stored is specified by the BASEDIR option. You can limit the queue size
## by using the SLOTS option (if omitted, the queue is limited by space
## available in the back end filesystem).
#
# set eventqueue
# basedir /var/monit # set the base directory where events will be stored
# slots 100 # optionally limit the queue size
#
#
## Send status and events to M/Monit (for more information about M/Monit
## see https://mmonit.com/). By default Monit registers credentials with
## M/Monit so M/Monit can smoothly communicate back to Monit and you don't
## have to register Monit credentials manually in M/Monit. It is possible to
## disable credential registration using the commented out option below.
## Though, if safety is a concern we recommend instead using https when
## communicating with M/Monit and send credentials encrypted. The password
## should be URL encoded if it contains URL-significant characters like
## ":", "?", "@". Default timeout is 5 seconds, you can customize it by
## adding the timeout option.
#
# set mmonit http://monit:monit@192.168.1.10:8080/collector
# # with timeout 30 seconds # Default timeout is 5 seconds
# # and register without credentials # Don't register credentials
#
#
## Monit by default uses the following format for alerts if the mail-format
## statement is missing::
## --8<--
## set mail-format {
## from: Monit <monit@$HOST>
## subject: monit alert -- $EVENT $SERVICE
## message: $EVENT Service $SERVICE
## Date: $DATE
## Action: $ACTION
## Host: $HOST
## Description: $DESCRIPTION
##
## Your faithful employee,
## Monit
## }
## --8<--
##
## You can override this message format or parts of it, such as subject
## or sender using the MAIL-FORMAT statement. Macros such as $DATE, etc.
## are expanded at runtime. For example, to override the sender, use:
#
# set mail-format { from: monit@foo.bar }
#
#
## You can set alert recipients whom will receive alerts if/when a
## service defined in this file has errors. Alerts may be restricted on
## events by using a filter as in the second example below.
#
# set alert sysadm@foo.bar # receive all alerts
#
## Do not alert when Monit starts, stops or performs a user initiated action.
## This filter is recommended to avoid getting alerts for trivial cases.
#
# set alert your-name@your.domain not on { instance, action }
#
#
## Monit has an embedded HTTP interface which can be used to view status of
## services monitored and manage services from a web interface. The HTTP
## interface is also required if you want to issue Monit commands from the
## command line, such as 'monit status' or 'monit restart service' The reason
## for this is that the Monit client uses the HTTP interface to send these
## commands to a running Monit daemon. See the Monit Wiki if you want to
## enable SSL for the HTTP interface.
#
set httpd port 2812 and
use address localhost # only accept connection from localhost (drop if you use M/Monit)
allow localhost # allow localhost to connect to the server and
allow admin:monit # require user 'admin' with password 'monit'
#with ssl { # enable SSL/TLS and set path to server certificate
# pemfile: /etc/ssl/certs/monit.pem
#}
###############################################################################
## Services
###############################################################################
##
## Check general system resources such as load average, cpu and memory
## usage. Each test specifies a resource, conditions and the action to be
## performed should a test fail.
#
# check system $HOST
# if loadavg (1min) per core > 2 for 5 cycles then alert
# if loadavg (5min) per core > 1.5 for 10 cycles then alert
# if cpu usage > 95% for 10 cycles then alert
# if memory usage > 75% then alert
# if swap usage > 25% then alert
#
#
## Check if a file exists, checksum, permissions, uid and gid. In addition
## to alert recipients in the global section, customized alert can be sent to
## additional recipients by specifying a local alert handler. The service may
## be grouped using the GROUP option. More than one group can be specified by
## repeating the 'group name' statement.
#
# 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
#
#
## Check that a process is running, in this case Apache, and that it respond
## to HTTP and HTTPS requests. Check its resource usage such as cpu and memory,
## and number of children. If the process is not running, Monit will restart
## it by default. In case the service is restarted very often and the
## problem remains, it is possible to disable monitoring using the TIMEOUT
## statement. This service depends on another service (apache_bin) which
## is defined above.
#
# 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 disk read > 500 kb/s for 10 cycles then alert
# if disk write > 500 kb/s for 10 cycles then alert
# 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
#
#
## Check filesystem permissions, uid, gid, space usage, inode usage and disk I/O.
## Other services, such as databases, may depend on this resource and an automatically
## graceful stop may be cascaded to them before the filesystem will become full and data
## lost.
#
# 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
# if read rate > 1 MB/s for 5 cycles then alert
# if read rate > 500 operations/s for 5 cycles then alert
# if write rate > 1 MB/s for 5 cycles then alert
# if write rate > 500 operations/s for 5 cycles then alert
# if service time > 10 milliseconds for 3 times within 5 cycles then alert
# group server
#
#
## Check a file's timestamp. In this example, we test if a file is older
## than 15 minutes and assume something is wrong if its not updated. Also,
## if the file size exceed a given limit, execute a script
#
# 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
#
#
## Check directory permission, uid and gid. An event is triggered if the
## directory does not belong to the user with uid 0 and gid 0. In addition,
## the permissions have to match the octal description of 755 (see chmod(1)).
#
# check directory bin with path /bin
# if failed permission 755 then unmonitor
# if failed uid 0 then unmonitor
# if failed gid 0 then unmonitor
#
#
## Check a remote host availability by issuing a ping test and check the
## content of a response from a web server. Up to three pings are sent and
## connection to a port and an application level network check is performed.
#
# 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
#
#
## Check a network link status (up/down), link capacity changes, saturation
## and bandwidth usage.
#
# 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 uploaded > 1 GB in last hour then alert
#
#
## Check custom program status output.
#
# check program myscript with path /usr/local/bin/myscript.sh
# if status != 0 then alert
#
#
###############################################################################
## Includes
###############################################################################
##
## It is possible to include additional configuration parts from other files or
## directories.
#
# include /etc/monit.d/*
#
參考資料
http://www.itdecent.cn/p/248d6456e83f
http://www.itdecent.cn/p/b4a7b209f11a
https://www.cnblogs.com/52fhy/p/6412547.html