####什麼是monitlinux
Monit是一個跨平臺的用來監控Unix/linux系統(好比Linux、BSD、OSX、Solaris)的工具。Monit特別易於安裝,並且很是輕量級(只有500KB大小),而且不依賴任何第三方程序、插件或者庫。nginx
####安裝 yum install monitweb
####如何配置 使用yum安裝默認配置文件在:
/etc/monitrc # 全局參數配置文件
/etc/monit.d/ # 在這個目錄下新增每一個待監控服務的配置mongodb
####先說說全局參數配置文件/etc/monitrc,個人配置以下:shell
#設置週期,每60秒自動檢測一次
set daemon 60
#設置報警郵件發送格式
set mailserver smtp.exmail.qq.com port 25 USERNAME "test@outlook.com" PASSWORD "test"
set mail-format {
from: test@outlook.com
subject: monit alert -- $EVENT $SERVICE
message: $EVENT Service $SERVICE
Date: $DATE
Action: $ACTION
Host: $HOST
Description: $DESCRIPTION
}
#設置報警郵件發給誰,默認只會發送一次報警。
#with reminder on 3 cycles表示若是服務一直處於失敗,則基於週期最多發送3次報警
set alert dev@outlook.com with reminder on 3 cycles
#Monit Web界面相關的訪問配置,如不使用則不須要配置(web管理界面須要額外的M/Monit項目)
set httpd port 2812
allow app:app
set eventqueue basedir /var/monit slots 1000
#包含全部須要監控服務的子配置項,這裏使用了文件名通配符
include /etc/monit.d/*.monitrc.conf
複製代碼
###下面列舉兩種最多見的服務監控方式,端口監控和進程號變動監控bash
####監控mongodb配置示例(利用端口監控)
在/etc/monit.d/下新增配置文件mongo.monitrc.conf,內容以下:服務器
#匹配進程名
CHECK PROCESS mongo MATCHING mongo
#配置服務啓動和重啓命令
start program = "/usr/bin/sudo service mongod start"
restart program = "/usr/bin/sudo service mongod restart"
#若是端口27017沒法訪問則認爲服務失敗,發報警郵件並重啓服務
if failed port 27017 type tcp then alert
if failed port 27017 type tcp then restart
#若是在三個週期內重啓了3次,則再也不監控
if 3 restarts within 3 cycles then unmonitor
複製代碼
####監控rabbitmq配置示例(利用進程ID變動監控) 在/etc/monit.d/下新增配置文件rabbitmq.monitrc.conf,內容以下:app
#匹配進程名
check PROCESS rabbitmq with MATCHING rabbitmq
#配置服務啓動和重啓命令
start program = "/etc/init.d/rabbitmq-server start"
restart program = "/etc/init.d/rabbitmq-server restart"
#若是進程號發生變化則認爲服務失敗,發報警郵件並重啓服務
if changed pid then alert
if changed pid then restart
#若是在三個週期內重啓了3次,則再也不監控
if 3 restarts within 3 cycles then unmonitor
複製代碼
####monit相關命令
monit # 啓動monit daemon
monit reload # 當更新了配置文件須要重載
monit status # 查看全部服務狀態
monit status nginx # 查看nginx服務狀態
monit stop all # 中止全部服務
monit stop mongo # 中止mongo服務
monit start all # 啓動全部服務
monit start mongo # 啓動mongo服務tcp
####圖形化管理界面M/Monit
能夠基於圖形化界面管理多個monit host,可是這個是要收費的,能夠免費試用30天工具