monit监控puma/nginx/redis/mysql/sidekiq,并发送钉钉通知

安装Monit


yum install -y monit httpd

服务配置


set daemon  5
set log syslog
set httpd port 2812 and        #http端口
    use address 192.168.1.32   
    allow 0.0.0.0/0.0.0.0
    allow admin:admin          #登陆权限
include /etc/monit.d/*         #引用配置文件

配置监控项


  1. 如果启/停命令是纯脚本,需要指定绝对路径
  2. 配置完启/停服务后,当服务进程异常时会自动进行重启

Puma

  1. 监控CPU和内存的使用并发送到dingding(monit有邮件发送相关配置,本文用ruby脚本发送到dingding做通知)
check process puma with pidfile /home/www/app/shared/tmp/pids/puma.pid
  start = "/bin/sh -c 'cd /home/www/app/current && /root/.rbenv/bin/rbenv exec /root/.rbenv/versions/2.4.2/bin/bundle exec puma -C /home/www/app/shared/puma.rb --daemon'"
  stop = "/bin/sh -c 'cd /home/www/app/current && cd /home/www/app/current && /root/.rbenv/bin/rbenv exec  /root/.rbenv/versions/2.4.2/bin/bundle exec pumactl -S /home/www/app/shared/tmp/pids/puma.state -F /home/www/app/shared/puma.rb stop'"
  if cpu > 60% for 2 cycles then exec "/root/.rbenv/versions/2.4.2/bin/ruby /tmp/alert_to_dingding.rb 'puma占用CPU > 60 %'"
  if totalmem > 2048.0 MB for 2 cycles then exec "/root/.rbenv/versions/2.4.2/bin/ruby /tmp/alert_to_dingding.rb 'puma占用内存 > 2G'"

Nginx

  1. 监控80端口是否响应
check process nginx with pidfile /var/run/nginx.pid
  start program = "/sbin/service nginx start"
  stop program = "/sbin/service nginx stop"
  if failed host localhost port 80 protocol http  with timeout 5 seconds then exec "/root/.rbenv/versions/2.4.2/bin/ruby /tmp/alert_to_dingding.rb 'nginx 80 端口 5秒内无法响应'"

mysql

check process mysql with pidfile /var/run/mysqld/mysqld.pid
  start program = "/sbin/service mysqld start"
  stop program = "/sbin/service mysqld stop"

redis

check process redis with pidfile /var/run/redis/redis.pid
  start program = "/sbin/service redis start"
  stop program = "/sbin/service redis stop"

sidekiq

check process sidekiq with pidfile /home/www/app/shared/tmp/pids/sidekiq-0.pid
  start = "/bin/sh -c 'cd /home/www/app/current && /root/.rbenv/bin/rbenv exec /root/.rbenv/versions/2.4.2/bin/bundle exec sidekiq --index 0 --pidfile /home/www/app/shared/tmp/pids/sidekiq-0.pid --environment production --logfile /home/www/app/shared/log/sidekiq.log --daemon'"
  stop = "/bin/sh -c 'cd /home/www/app/current && /root/.rbenv/bin/rbenv exec  /root/.rbenv/versions/2.4.2/bin/bundle exec sidekiqctl stop /home/www/app/shared/tmp/pids/sidekiq-0.pid 10'"

最终效果


WX20180808-172741

ruby dingding通知脚本


#!/root/.rbenv/versions/2.4.2/bin/ruby

cmd = "curl 'https://oapi.dingtalk.com/robot/send?access_token=YOUR_TOKEN' -H 'Content-Type: application/json' -d ' {\"msgtype\": \"text\", \"text\": { \"content\": \"" + ARGV[0] + "\"}}'"
system(cmd)