How to use Monit to monitor VPS services.
早就从Tianyicui的twitter上了解了monit这个服务,但是一直没用上。直到最近自己VPS上的Mysql没有自动启动,才发现需要一个自动化的工具来监视VPS上重要进程(Nginx, PHP5-FPM, MySQL)的状态。
简单来讲,借鉴Monit的文档和 Graham King 的Blog,我的Monit配置如下。
zhanxw@zhanxw:~$ cat /etc/monit/conf.d/zhanxw.monit set daemon 120 set idfile /var/.monit.id set alert me@zhanxw.com set mailserver smtp.gmail.com port 587 username "XXXX@XXXX.com" password "XXXX" using tlsv1 with timeout 30 seconds # this basically allows you to use gmail's smtp server. tlsv1 gets things going in SSL. set logfile /var/log/monit.log set httpd port 2812 use address zhanxw.com allow 141.211.10.47 allow 205.185.127.61 allow @zhanxw #allow admin:Monit check system zhanxw.com if memory usage > 75% then alert check process nginx with pidfile "/var/run/nginx.pid" start = "/etc/init.d/nginx start" stop = "/etc/init.d/nginx stop" if loadavg (1min) > 4 then alert if loadavg (5min) > 2 then alert if failed port 8080 and protocol http then restart if 2 restarts within 3 cycles then timeout alert me@zhanxw.com check process php5-fpm pidfile "/var/run/php5-fpm.pid" start = "/usr/sbin/service php5-fpm start" stop = "/usr/sbin/service php5-fpm stop" if failed host 127.0.0.1 port 9000 then restart if 2 restarts within 3 cycles then timeout alert me@zhanxw.com check process mysqld with pidfile "/var/lib/mysql/zhanxw.pid" start = "/usr/sbin/service mysql start" stop = "/usr/sbin/service mysql stop" if failed host 127.0.0.1 port 3306 then restart if 2 restarts within 3 cycles then timeout alert me@zhanxw.com check process varnish with pidfile "/var/run/varnishd.pid" start = "/usr/sbin/service varnish start" stop = "/usr/sbin/service varnish stop" if failed port 80 and protocol http then restart if 2 restarts within 3 cycles then timeout alert me@zhanxw.com
这个配置的优势在于可以把服务进程的变化发到我的gmail信箱;可以用和SSH相同的密码登录Monit管理界面;可以自动重启关键进程(Nginx, PHP5-FPM,MySQL,Varnish)。我没有监控SSHD,因为这个进程不是zhanxw.com blog的关键部分,即不和WordPress服务相关。
本文没有提到安装Monit,实际上在ubuntu系统上可以用 sudo apt-get install monit 安装。
对于有图形化要求的系统监视,上面的Blog提到了另一个Open source soluiton: Munin。有兴趣的可以移步到那里学习。