- A+
所属分类:linux技术
Monit介绍
monit监控和管理进程、程序、文件、目录和Unix系统的文件的工具。可以进行自动维护和修理,在错误的情况下执行有意义的因果关系的行动。比如,某个进程没有运行启动它;没有响应重启它;占用太多资源停止该进程等等。也可以用来监控文件、目录或系统文件的变化,如时间戳的改变,检验和的改变或大小的改变
文件准备
monit-5.28.1-linux-x64.tar.gz - 可点击链接进入官网下载
一、使用tar命令解压
tar -axvf monit-5.3.2-linux-x86.tar.gz
二、拷贝配置文件
cp /usr/local/monit/conf/monitrc /etc
三、修改默认配置
1. Monit默认仅支持localhost访问,文件中找到以下内容,将localhost修改为0.0.0.0并注释掉allow localhost这一行
set httpd port 2812 and use address localhost
修改后配置内容如下
2. 配置监控脚本目录,取消include注释
四、创建脚本目录
mkdir /etc/monit.d
五、定义监控脚本
1.监控PostgreSQL数据库示例:
vim /etc/monit.d/PostgreSQL
内容如下:
check process PostgreSQL with pidfile /var/lib/pgsql/9.3/data/postmaster.pid group database start program = "systemctl start postgresql-9.3" stop program = "systemctl stop postgresql-9.3" restart program = "systemctl restart postgresql-9.3" if failed host 127.0.0.1 port 5432 then restart if 5 restarts within 5 cycles then timeout
2.监控服务示例:
vim /etc/monit.d/TongShanService
内容如下:
check process TongShanService MATCHING /opt/projects/TongShan/services/TongShan.Services group service start program = "systemctl start tongshan.service" stop program = "systemctl stop tongshan.service" restart program = "systemctl restart tongshan.service" if failed host 127.0.0.1 port 8889 then restart if 5 restarts within 5 cycles then timeout
六、启动monit
/usr/local/monit/bin/monit
其他常用命令:
/usr/local/monit/bin/monit -t #检查monit语法 /usr/local/monit/bin/monit reload #重新载入配置 /usr/local/monit/bin/monit start all #启动所有服务 /usr/local/monit/bin/monit stop all #停止所有服务 /usr/local/monit/bin/monit restart all #重启所有服务 /usr/local/monit/bin/monit monitor all #启动对所有服务的监视 /usr/local/monit/bin/monit unmonitor all #禁用对所有服务的监视 /usr/local/monit/bin/monit quit #推出监控守护进程 /usr/local/monit/bin/monit validate #检查所有服务,如果未运行则启动
七、最终效果
打开浏览器输入地址:localhost:2812 输入用户名密码
点击Process可查看详细监控配置信息(页面底部支持一键启动、停止、重启服务):