配置步骤
安装
# 下载二进制文件
文件目录地址:https://mmonit.com/monit/dist/binary/
# 解压下载的文件到/usr/local/monit
[root@Server-130 monit]# ls
bin conf COPYING man
# 复制配置文件
[root@Server-130 monit]# cp ./conf/monitrc /etc/
# 环境变量
编辑/etc/profile文件,添加:
“
export PATH=/usr/local/monit/bin:$PATH
”
# 生效
[root@Server-130 monit]# . /etc/profile
配置
编辑配置文件
daemon 120 # 设置monit作为守护进程运行,并且每2分钟监视一次
set logfile /var/log/monit.log # 设置日志文件的位置,如果要写入系统日志可以"set logfile syslog"
# monit内置了一个用于查看被监视服务状态的http服务器,需要在防火墙中开启2812
set httpd port 2812 and
#use address localhost # only accept connection from localhost
use address 192.168.137.130
#allow localhost # allow localhost to connect to the server and
allow 192.168.137.0/8
allow admin:monit # require user 'admin' with password 'monit'
#allow @monit # allow users of group 'monit' to connect (rw)
#allow @users readonly # allow users of group 'users' to connect readonly
# 设置发送邮件的服务器及邮箱
set mailserver smtp.sina.com port 25 USERNAME "xxx@sina.com" PASSWORD "***"
# 制定报警邮件的格式
set mail-format {
from: linlianpengit@sina.com
subject: $SERVICE $EVENT at $DATE
message: Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION.
}
# 指定邮件接收者
set alert XXX@qq.com with reminder on 3 cycles
# 检测Nginx
check process Nginx with pidfile /var/run/nginx.pid
start program = "/usr/sbin/nginx -c /etc/nginx/nginx.conf"
stop program = "/usr/sbin/nginx -s stop"
if failed host 127.0.0.1 port 80 then restart
if 5 restarts within 5 cycles then timeout
# 检测Tomcat
# tomcat进程默认是不使用pid文件的,pid文件需要显式为tomcat设置,可以打开tomcat目录下的bin目录,打开catalina.sh文件,在开头(非首行)加入:CATALINA_PID=/var/run/catalina.pid
check process Tomcat with pidfile /var/run/tomcat.pid
start program = "/usr/local/apache-tomcat-7.0.70/bin/startup.sh"
stop program = "/usr/local/apache-tomcat-7.0.70/bin/shutdown.sh"
if failed port 8080 for 5 cycles then restart
if 3 restarts within 5 cycles then timeout
测试配置文件
[root@Server-130 ~]# monit -t
Control file syntax OK
启动
[root@Server-130 ~]# monit
设置monit开机启动
在inittab中加入随系统启动的设置,使得monit进程如果停止,init进程会将其重启
[root@Server-130 ~]# echo "moni:2345:respawn:/usr/local/monit-5.10/bin/monit -Ic /etc/monitrc" >> /etc/inittab
注意
由于将monit设置成了守护进程,并且在inittab中加入了随系统启动的设置,则monit进程如果停止,init进程会将其重启,而monit又监视着其它的服务, 这意味着monit所监视的服务不能使用一般的方法来停止,因为一停止,monit又会将其启动。要停止monit所监视的服务,应该使用monit stop name这样的命令。 例如要停止tomcat:[root@Server-130 ~]# monit stop tomcat
要停止全部monit所监视的服务可以使用:[root@Server-130 ~]# monit stop all
启动全部,则是[root@Server-130 ~]# monit start all