1.介紹php
monit監控和管理進程、程序、文件、目錄和Unix系統的文件的工具。能夠進行自動維護和修理,在錯誤的狀況下執行有意義的因果關係的行動。好比,某個進程沒有運行啓動它;沒有響應重啓它;佔用太多資源中止該進程等等。也能夠用來監控文件、目錄或系統文件的變化,如時間戳的改變,檢驗和的改變或大小的改變mysql
2.安裝:(需先裝epel源)nginx
yum install monit -y sql
3.配置apache
3.1 monit 的配置文件monit.conf服務器
set daemon 30 監控檢查的時間間隔ssh
set logfile syslog 默認日誌爲syslog,還能夠設置爲自定義路徑下的文件工具
set pidfile /var/run/monit.pid 設置pid文件的位置 spa
set idfile /var/.monit.id 設置監控項目惟一ID的文件位置 默認在$HOME/.monit.idrest
set statefile /var/.monit.state 默認存儲每一個週期內monitorinig 狀態的文件
set mailserver mail.bar.baz 主的郵件服務器
backup.bar.baz port 10025 備的郵件服務器
set mail-format { 設置郵件告警的格式
from: monit@$HOST
subject: monit alert -- $EVENT $SERVICE
message: $EVENT Service $SERVICE
Date: $DATE
Action: $ACTION
Host: $HOST
Description: $DESCRIPTION
Your faithful employee,
Monit
}
set alert sysadm@foo.bar 設置接收告警的郵箱
以上是全局的配置,具體的services監控語法後面會介紹
3.2 進程監控
在 /etc/monit.d/下建立 nginx ,,並重啓monit。內容以下
check process nginx with pidfile /usr/local/xywy/nginx/run/nginx.pid start program = "/usr/local/xywy/nginx/sbin/nginx" with timeout 30 seconds stop program = "/usr/local/xywy/nginx/sbin/nginx -s stop"
3.3 監控文件
在/etc/monit.d/下建立monit_test ,並重啓monit,內容以下
check file monit.conf path /etc/monit.conf group system if changed sha1 checksum then exec "/usr/local/bin/monit -c /etc/monit.conf reload"
3.4 監控文件設備
check device VAR_LOG with path /var/log if space usage > 85% then alert check filesystem tmpfs with path /var if space usage > 80% then alert
下面是配置綜合案例
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 port 22 protocol ssh then restart if 5 restarts within 5 cycles then timeout check process mysql with pidfile /var/run/mysqld/mysqld.pid group database start program = "/etc/init.d/mysqld start" stop program = "/etc/init.d/mysqld stop" if failed host 127.0.0.1 port 3306 then restart if 5 restarts within 5 cycles then timeout check process nginx with pidfile /var/run/nginx.pid start program = "/etc/init.d/nginx start" stop program = "/etc/init.d/nginx stop" if failed host www.vpsee.com port 80 protocol http then restart check process php_cgi with pidfile /var/run/php_cgi.pid start program = "/etc/init.d/php_cgi start" stop program = "/etc/init.d/php_cgi stop" if failed host 127.0.0.1 port 9000 then restart if 5 restarts within 5 cycles then timeout check process apache with pidfile /var/run/httpd.pid group www start program = "/etc/init.d/httpd start" stop program = "/etc/init.d/httpd stop" if failed host www.vpsee.com port 8080 protocol http then restart if cpu is greater than 80% for 2 cycles then alert if cpu > 80% for 5 cycles then restart if totalmem > 512 MB for 5 cycles then restart if children > 200 then restart if loadavg(5min) greater than 10 for 8 cycles then stop if 3 restarts within 5 cycles then timeout
3.5 monit命令使用
-c 指定配置文件
-d 後臺運行
-l 指定logfile
-p 指定pid file
-s 指定statefile
<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">