Monit-開源服務器監控工具
Monit
是一個用於管理和監控Unix
系統的小型開源工具. Monit
進行自動維護和修理, 而且能夠在錯誤狀況下執行有意義的因果做用.web
比zabbix
輕量.flask
全局配置 - Web狀態頁面
monit-5.25
默認監聽2812
- 對
web
狀態頁面的訪問是經過SSL
加密的 - 使用
admin/monit
做爲用戶名/口令登陸 - 只容許經過
localhost
、myhost.mydomain.ro
和在局域網內部192.168.0.0/16
訪問 Monit
使用pem
格式的SSL
證書
生成一個自簽名證書
cd /etc/pki/tls/certs/ # 會自動在/etc/ssl/certs/下面複製一份monit.pem # 默認權限是0400, 若是不是就手動修改 ./make-dummy-cert monit.pem
httpd配置
編輯vi /etc/monitrc
, 修改相應的內容爲:bash
set httpd port 2812 and # 只接受來自本地主機的鏈接(only accept connection from localhost) use address 10.10.10.141 # 容許本地主機鏈接到服務器和(allow localhost to connect to the server and) allow localhost # 和指定網段(192.168.0.0/16), 或者全部ip均可以訪問 allow 0.0.0.0/0.0.0.0 # 須要用戶'admin',密碼爲'monit'(require user 'admin' with password 'monit') allow admin:monit # 啓用SSL/TLS並設置服務器證書的路徑(enable SSL/TLS and set path to server certificate) with ssl { pemfile: /etc/ssl/certs/monit.pem }
或者服務器
set httpd port 2812 and # 只接受來自本地主機的鏈接(only accept connection from localhost) use address 10.10.10.141 # 容許本地主機鏈接到服務器和(allow localhost to connect to the server and) allow localhost # 和指定網段(192.168.0.0/16), 或者全部ip均可以訪問 allow 192.168.0.0/16 # 配置域名 allow myhost.mydomain.ro # 須要用戶'admin',密碼爲'monit'(require user 'admin' with password 'monit') allow admin:monit # 啓用SSL/TLS並設置服務器證書的路徑(enable SSL/TLS and set path to server certificate) with ssl { pemfile: /etc/ssl/certs/monit.pem }
全局通知 - 郵件通知
咱們至少須要一個可用的SMTP
服務器來讓Monit
發送郵件.網絡
- 郵件服務器的機器名: smtp.exmail.qq.com
- Monit使用的發件人: monit@monit.ro
- 郵件的收件人: test@monit.ro
- 郵件服務器使用的SMTP端口: 587(默認是25, 根據本身的SMTP服務器肯定)
編輯vi /etc/monitrc
, 將相應的內容修改成:dom
set mailserver smtp.exmail.qq.com port 465 set mail-format { from: monit@monit.ro subject: $SERVICE $EVENT at $DATE on $HOST message: Monit $ACTION $SERVICE $EVENT at $DATE on $HOST : $DESCRIPTION. Yours sincerely, Monit } set alert test@qq.com
全局配置 - Monit守護進程
能夠設置爲:tcp
- 在120秒後進行第一次檢測
- 每2分鐘檢測一次服務
- 使用syslog來記錄日誌
編輯vi /etc/monitrc
, 將相應的內容修改成:工具
set daemon 60 with start delay 60 set logfile syslog facility log_daemon
必須定義idfile
, Monit
守護進程的一個獨一無二的ID文件; 以及eventqueue
, 當monit的郵件由於SMTP或者網絡故障發不出去, 郵件會暫存在這裏; 以及確保/var/monit
路徑是存在的. 而後使用下邊的配置就能夠了:ui
set idfile /var/monit/id set eventqueue basedir /var/monit
默認路徑爲$HOME/.monit.id
加密
驗證全局配置
語法檢測, 檢測/etc/monitrc
和/etc/monit.d
的配置語法是否正確:
$ monit -t New Monit id: 8b7015f050672ebfd066d9e161cdf3ef Stored in '/root/.monit.id' Control file syntax OK
若是報錯, 請檢查配置文件.
啓動服務, 並設置開機自啓:
systemctl start monit systemctl enable monit
服務監控
端口監控
在/etc/monit.d/
下新增配置文件monitor
, 內容以下:
# 匹配進程名 check process flask MATCHING gunicorn # 配置服務啓動和重啓命令 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
使用腳本監控
在/etc/monit.d/
下新增配置文件monitor
, 內容以下:
check program monitor with path "/bin/bash /etc/monit.d/service/service" with timeout 60 seconds # IF STATUS operator value THEN action if status == 1 then exec "/bin/bash /etc/monit.d/service/service restart views"