使用monit监控本机服务

【安装】
yum install monit

【配置告警】
修改/etc/monit.conf
set daemon 60

set httpd port 1966
#设置允许访问服务的账号密码
allow monitor:kaka2123
#允许访问的地址,下面是所有地址
allow 0.0.0.0/0.0.0.0

#设置告警邮件使用的服务器,下面用的是qq邮箱,采用的是ssl连接
set mailserver smtp.exmail.qq.com port 465 USERNAME "[email protected]"  PASSWORD "123456" using sslv3

#邮件格式,$Host如果没有设置默认是localhost
set mail-format {
  from: [email protected]
  Subject: monit alert -- $Host $EVENT $SERVICE
  message: $EVENT Service $SERVICE
          Date:        $DATE
          Action:      $ACTION
          Host:        $HOST
          Description: $DESCRIPTION
  Your faithful employee,
  monit
}

#告警邮件的接收人
set alert [email protected] 
set alert [email protected]

set eventqueue
basedir /var/monit

#默认会读取/etc/monit.d下的所有监控服务配置文件
# Include all files from /etc/monit.d/


【配置服务器】
在/etc/monit.d目录下新增以下文件
1、mysql.conf
check process mysqld with pidfile /var/run/mysqld/mysqld.pid 
  if failed port 3306 then alert
  ##下面的语句会在有问题时尝试重启服务,如果重启5次后仍然是不则会报警
  #start program = "/etc/init.d/mysqld start" with timeout 10 seconds
  #stop program  = "/etc/init.d/mysqld stop"
  #if failed port 3306 protocol mysql then alert
  #  with timeout 10 seconds 
  #  then restart
  #if 3 restarts within 5 cycles then alert 


2、nginx.conf
check process nginx with pidfile /var/run/nginx.pid 
  start program = "/etc/init.d/nginx start" with timeout 10 seconds
  stop program  = "/etc/init.d/nginx stop"
  if failed host heylinux.com port 80 protocol http
    with timeout 10 seconds
    then restart
  if 3 restarts within 5 cycles then timeout


3、php.conf
check process php-fpm with pidfile /opt/php/var/run/php-fpm.pid
  start program = "/etc/init.d/php56.fpm start" with timeout 10 seconds
  stop program  = "/etc/init.d/php56.fpm stop"
  if cpu > 60% for 5 cycles then restart
  if loadavg(5min) greater than 4 for 5 cycles then restart
  if 3 restarts within 5 cycles then timeout


4、redis.conf
check process redis with pidfile /var/run/redis/redis.pid 
  if failed port 6379 then alert


5、ssh.conf
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


6、oss.conf
if loadavg (1min) > 10 then alert
if loadavg (5min) > 6 then alert
if memory usage > 50% for 5 cycles then alert
if cpu usage (user) > 50% then alert
if cpu usage (system) > 50% then alert
if cpu usage (wait) > 75% then alert

##下面是监控磁盘使用量,需根据实际路径修改
check device home with path /dev/mapper/vg_9quidc-lv_home
    if space usage > 70% for 1 times within 1 cycles then alert
check device root with path /dev/mapper/vg_9quidc-lv_root
    if space usage > 70% for 1 times within 1 cycles then alert
check device sda1 with path /dev/sda1
    if space usage > 70% for 1 times within 1 cycles then alert


【重启并查看服务】
> service monit restart
> monit status

猜你喜欢

转载自sensejw.iteye.com/blog/2312788