monit 配置 监控启动 nginx php mysql redis mongodb 单台服务器永不宕机

安装参考:

http://linuxjcq.blog.51cto.com/3042600/717843

启动:monit -Ic /etc/monitrc &
crontab -e
*/600 * * * * /usr/local/bin/monit -Ic /etc/monitrc &
conf 配置:
—————nginx php mysql redis mongodb 配置 完美OK——————–
# nginx
check process nginx with pidfile /usr/local/nginx/logs/nginx.pid
start program = “/usr/local/nginx/sbin/nginx”
stop program = “/usr/bin/killall nginx”
if failed host 127.0.0.1 port 80 then restart
if cpu is greater than 40% for 2 cycles then alert
if cpu > 60% for 5 cycles then restart
if 10 restarts within 10 cycles then timeout
# php5-fpm
check process php-fpm with pidfile /var/run/php-fpm.pid
start program = “/usr/local/php5.3/sbin/php-fpm”
stop program = “/usr/bin/killall php-fpm”
if failed port 9000 then restart
if cpu is greater than 40% for 2 cycles then alert
if cpu > 60% for 5 cycles then restart
if 10 restarts within 10 cycles then timeout# php5-fpm binary.
check file php5-fpm-tcp_bin with path /usr/local/php5.3/sbin/php-fpm
if 10 restarts within 10 cycles then timeout
if failed checksum then unmonitor
if failed permission 755 then unmonitor
if failed uid root then unmonitor
if failed gid root then unmonitor
alert root@localhost# Test the init scripts.
check file php5-fpm-tcp_init with path /usr/local/php5.3/sbin/php-fpm
if failed checksum then unmonitor
if failed permission 755 then unmonitor
if failed uid root then unmonitor
if failed gid root then unmonitor
alert root@localhost
# mysql
check process mysql with pidfile /var/lib/mysql/localhost.localdomain.pid
group database
start program = “/sbin/service mysql start”
stop program = “/sbin/service mysql stop”
if failed port 8306 then restart
if 3 restarts within 10 cycles then timeout
# redis
check process redis with pidfile /var/run/redis.pid
start program = “/usr/local/redis/redis-server /usr/local/redis/redis.conf”
stop program = “/usr/bin/killall redis-server”
if failed host 127.0.0.1 port 6379 then restart
if cpu is greater than 40% for 2 cycles then alert
if cpu > 60% for 5 cycles then restart
if 10 restarts within 10 cycles then timeout
# mongodb
check process mongod with pidfile /usr/local/mongodb/data/mongod.lock
start program = “/usr/local/mongodb/bin/mongod –dbpath=/usr/local/mongodb/data/”
stop program = “/usr/bin/killall mongod”
if failed port 27017 protocol http and request “/” with timeout 10 seconds then restart
if 10 restarts within 10 cycles then timeout
—————-end——————-
/usr/local/mmonit/bin/mmonit -c /usr/local/mmonit/conf/server.xml -t
/usr/local/mmonit/bin/mmonit -c /usr/local/mmonit/conf/server.xml start
monit  -t
killall monit
monit -Ic /etc/monitrc &
telinit q
ps aux |grep monit
kill -1 1
ps aux |grep redis-server
killall redis-server
ll /var/run/redis.pid
/usr/local/redis/redis-server /usr/local/redis/redis.conf &
ps aux |grep php-fpm
killall php-fpm
/usr/local/php5.3/sbin/php-fpm
ps aux |grep mongod
killall mongod
/usr/local/mongodb/bin/mongod –dbpath=/usr/local/mongodb/data/ &
set mmonit http://monit:monit@142.4.48.50:8080/collector
set nginx port 80 and
    use address 142.4.48.50
    allow localhost
    allow 142.4.48.50
  location /monit/ {
        rewrite ^/monit/(.*) /$1 break;
        proxy_ignore_client_abort on;
        proxy_pass   http://192.168.1.10:2812; 
        proxy_redirect  http://192.168.1.10:2812/monit; 
    }

# include /etc/monit.d/*
# mysql
check process mysqld with pidfile /var/lib/mysql/localhost.localdomain.pid
  group database
  start program = “/etc/init.d/mysql start”
  stop program = “/etc/init.d/mysql stop”
  if failed host 127.0.0.1 port 8306 then restart
  if 5 restarts within 5 cycles then timeout
# nginx
check process nginx with pidfile /usr/local/nginx/logs/nginx.pid
  start program = “/usr/local/nginx/sbin/nginx”
  stop program = “/usr/bin/killall nginx”
  if failed host 127.0.0.1 port 80 then restart
  if cpu is greater than 40% for 2 cycles then alert
  if cpu > 60% for 5 cycles then restart
  if 10 restarts within 10 cycles then timeout
# redis
check process redis with pidfile /var/run/redis.pid
  start program = “/usr/local/redis/redis-server /usr/local/redis/redis.conf”
  stop program = “/usr/bin/killall redis-server”
  if 10 restarts within 10 cycles then timeout
check file dump.rdb with path /usr/local/redis/dump.rdb
  if size > 100 MB then alert
# php5-fpm
check process php-fpm with pidfile /var/run/php-fpm.pid
  start program = “/usr/local/php5.3/sbin/php-fpm”
  stop program = “/usr/bin/killall php-fpm”
  if failed host 127.0.0.1 port 9000 then restart
  if cpu is greater than 40% for 2 cycles then alert
  if cpu > 60% for 5 cycles then restart
  if 10 restarts within 10 cycles then timeout
# php5-fpm binary.
check file php5-fpm-tcp_bin with path /usr/local/php5.3/sbin/php-fpm
  if 10 restarts within 10 cycles then timeout
  if failed checksum then unmonitor
  if failed permission 755 then unmonitor
  if failed uid root then unmonitor
  if failed gid root then unmonitor
  alert root@localhost
# Test the init scripts.
check file php5-fpm-tcp_init with path /usr/local/php5.3/sbin/php-fpm
  if failed checksum then unmonitor
  if failed permission 755 then unmonitor
  if failed uid root then unmonitor
  if failed gid root then unmonitor
  alert root@localhost
# mysql
check process mysqld with pidfile /var/lib/mysql/localhost.localdomain.pid
  group database
  start program = “/sbin/service mysql start”
  stop program = “/sbin/service mysql stop”
  if failed host 127.0.0.1 port 8306 then restart
  if 2 restarts within 6 cycles then timeout
# mongodb
check process mongod with pidfile /usr/local/mongodb/data/mongod.lock
  start program = “/usr/local/mongodb/bin/mongod –dbpath=/usr/local/mongodb/data/”
  stop program = “/usr/bin/killall mongod”
  if failed port 27017 protocol http and request “/” with timeout 10 seconds then restart
  if 10 restarts within 10 cycles then timeout
#mongodb
check process mongod with pidfile “/usr/local/mongodb/data/mongod.lock”
  start program = “/usr/local/mongodb/bin/mongod –dbpath=/usr/local/mongodb/data/”
  stop program = “/usr/bin/killall  mongod”
  if failed port 27017 protocol http and request “/” with timeout 10 seconds then restart
  if 5 restarts within 5 cycles then timeout
# tomcat
check process tomcat with pidfile /var/run/tomcat/tomcat.pid
  start program = “/etc/init.d/tomcat start”
  as uid solr gid solr
  stop program = “/etc/init.d/tomcat stop”
  as uid solr gid solr
  if failed port 8080 then alert
  if failed port 8080 for 5 cycles then restart
set mmonit http://monit:monit@142.4.48.50:8080/collector
set httpd port 12112 and
    use address 142.4.48.50 //monit主机的IP
    allow localhost
    allow 142.4.48.50 //mmonit主机的IP