今天無心中看到這樣一個服務:monit,跑在linux/unix平臺,可用於監控本地資源,觸發告警和動做。
php
安裝
nginx
yum install monit -y
配置apache
[root@lvs122101master monit.d]# cat /etc/monit.conf |grep -v '^#' set daemon 600 set mailserver 192.168.11.27 set alert shanks@null.com with reminder on 1 cycle include /etc/monit.d/*
[root@lvs122101master monit.d]# cat /etc/monit.d/system check device gen with path /dev/mapper/vg_livedvd-lv_root if space usage > 50% then alert
啓動bash
/etc/init.d/monit start
查看日誌app
[root@lvs122101master log]# tail -f monit [CST Nov 26 10:39:10] info : Monit started [CST Nov 26 10:39:10] error : 'gen' space usage 76.6% matches resource limit [space usage>50.0%] [CST Nov 26 10:40:11] info : Monit has not changed [CST Nov 26 10:40:11] error : 'gen' space usage 76.6% matches resource limit [space usage>50.0%] [CST Nov 26 10:40:59] info : monit daemon with pid [21487] killed [CST Nov 26 10:40:59] info : Monit stopped [CST Nov 26 10:41:03] info : Monit started [CST Nov 26 10:41:03] error : 'gen' space usage 76.6% matches resource limit [space usage>50.0%] [CST Nov 26 10:41:08] error : Sendmail: error receiving data from the mailserver '192.168.80.27' -- Resource temporarily unavailable [CST Nov 26 10:41:08] error : Aborting event
告警郵件格式tcp
Resource limit matched Service gen Date: Tue, 26 Nov 2013 10:40:11 +0800 Action: alert Host: lvs122101master Description: 'gen' space usage 76.6% matches resource limit [space usage>50.0%] Your faithful employee, monit
監控自定義服務ide
# 監控nginx # # 須要提供進程pid文件信息 check process nginx with pidfile /var/run/nginx.pid # 進程啓動命令行,注:必須是命令全路徑 start program = "/etc/init.d/nginx start" # 進程關閉命令行 stop program = "/etc/init.d/nginx stop" # nginx進程狀態測試,監測到nginx連不上了,則自動重啓 if failed host www.example.com port 80 protocol http then restart # 屢次重啓失敗將再也不嘗試重啓,這種就是系統出現嚴重錯誤的狀況 if 3 restarts within 5 cycles then timeout # 可選,設置分組信息 group server # 可選的ssl端口的監控,若是有的話 # if failed port 443 type tcpssl protocol http # with timeout 15 seconds # then restart # # 監控apache # check process apache with pidfile /var/run/apache2.pid start program = "/etc/init.d/apache2 start" stop program = "/etc/init.d/apache2 stop" # apache吃cpu和內存比較厲害,額外添加一些關於這方面的監控設置 if cpu > 50% for 2 cycles then alert if cpu > 70% for 5 cycles then restart if totalmem > 1500 MB for 10 cycles then restart if children > 250 then restart if loadavg(5min) greater than 10 for 20 cycles then stop if failed host www.example.com port 8080 protocol http then restart if 3 restarts within 5 cycles then timeout group server # 可選,依賴於nginx depends on nginx # # 監控spawn-fcgi進程(其實就是fast-cgi進程) # check process spawn-fcgi with pidfile /var/run/spawn-fcgi.pid # spawn-fcgi必定要帶-P參數纔會生成pid文件,默認是沒有的 start program = "/usr/bin/spawn-fcgi -a 127.0.0.1 -p 8081 -C 10 -u userxxx -g groupxxx -P /var/run/spawn-fcgi.pid -f /usr/bin/php-cgi" stop program = "/usr/bin/killall /usr/bin/php-cgi" # fast-cgi走的不是http協議,monit的protocol參數也沒有cgi對應的設置,這裏去掉protocol http便可。 if failed host 127.0.0.1 port 8081 then restart if 3 restarts within 5 cycles then timeout group server depends on nginx