Monit 是專門用來監視與管理系統中行程、檔案、目錄與裝置的工具。當你指定的服務當機或無回應,它會直接砍掉該行程再重啟,並發出郵件通知。它還內建 HTTP(S) Web 介面,用瀏覽器就能查看被監看的服務狀態。
官網:https://www.mmonit.com
下載:https://mmonit.com/monit/dist/monit-5.1.1.tar.gz
shell > tar xzvf monit-5.1.1.tar.gz
shell > cd monit-5.*
shell > ./configure
shell > make
shell > make install
shell > rm /etc/monitrc
shell > cp monitrc /etc/
接著編輯 /etc/inittab,讓 init 負責守護 monit 行程:
shell > vi /etc/inittab
mo:2345:respawn:/usr/local/bin/monit -Ic /etc/monitrc
安裝到這裡就完成了。
set daemon 120 # 檢查間隔,單位秒
set logfile syslog facility log_daemon # 以 syslog 記錄
set logfile /var/logs/monit.log # 日誌路徑
set idfile /var/.monit.id # PID 檔位置
set mailserver 192.168.0.21 # 郵件伺服器 IP
set mail-format { from: monit@test.com } # 寄件帳號
set alert phoneNumber@139.com # 通知信箱(139 有到件提醒)
set httpd port 2812 and
use address 192.168.0.21 # 監聽 IP,方便 http 存取
allow admin:pass # 帳號與密碼
check device system with path /dev/mapper/VolGroup00-LogVol00
if space usage > 85% for 5 times within 15 cycles then alert
if space usage > 95% then stop
if inode usage > 85% then alert
if inode usage > 95% then stop
check process sshd with pidfile /var/run/sshd.pid
start program "/etc/init.d/sshd start"
stop program "/etc/init.d/sshd stop"
if failed host 127.0.0.1 port 22 then restart
if 5 restarts within 5 cycles then timeout
check process cron with pidfile /var/run/crond.pid
group system
把這些規則寫進設定後重載,Monit 就能同時肩負磁碟、行程與埠的守門工作,異常時還會主動寄信通知。