1.安装monit
sudo apt-get install monit
2.启动和关闭monit
sudo service monit start
sudo service monit stop
3.monit相关配置(/etc/monit/monitrc)
set daemon 120 #每隔2分钟检查一次服务
set logfile /var/log/monit.log #monit日志写入位置
set idfile /var/lib/monit/id
set statefile /var/lib/monit/state
配置monit WEB访问
set httpd port 2812 and #2812表示访问端口号
use address localhost #仅接受来自本地主机的连接
allow localhost
allow admin:monit #web登录的用户名和密码
配置这个的时候,在阿里控制台开通2812端口,但是还是不能访问,好像是阿里为了安全把有些端口给禁止了,最后用Apache进行了转发
由于monit使用的是httpd,打开apache的 proxy_http.load模块
执行命令:ln -s /etc/apache2/mods-available/proxy_http.load /etc/apache2/mods-enabled (如果/etc/apache2/mods-enabled文件夹下面已经存在proxy_http.load则不需要执行)
在Apache配置文件中配置,就可以访问了
ProxyPass / http://localhost:2812/
ProxyPassReverse / http://localhost:2812/
check file tomcat_error_log with path /var/log/tomcat8/catalina.out
ignore match "^monit"
if match "(warn|error)" then alert
监控Apache是否正常启动
check process apache with pidfile /var/run/apache2/apache2.pid
group www
group apache
start program = "/etc/init.d/apache2 start"
stop program = "/etc/init.d/apache2 stop"
if 4 restarts within 20 cycles then timeout
if failed host localhost port 80 with protocol http and request "/server-status" with timeout 25 seconds for 4 times within 5 cycles then restart
配置报警邮件发送
#Mail settings
set mail-format {
from: [email protected]
subject: monit alert -- $EVENT
message: $EVENT Service $SERVICE
Date: $DATE
Action: $ACTION
Host: $HOST
Description: $DESCRIPTION
Your faithful employee,
Monit
}
set mailserver smtpdm.aliyun.com port 80 username [email protected] password "you-password"
set alert test@163.com #接收邮件的邮箱
邮箱采用的是阿里云的消息推送邮箱,[email protected]是用户名 you-password是SMTP密码,下图是邮件发送的最终样式