Monit是一个开源的进程监控工具,可以监控和管理Unix系统上的进程、文件、目录、设备等。以下是使用Monit监控服务状态的详细指南:
sudo apt-get update
sudo apt-get install monit
sudo yum install monit
# 或
sudo dnf install monit
Monit的主配置文件通常位于:
- /etc/monit/monitrc
(Debian/Ubuntu)
- /etc/monit.conf
(RHEL/CentOS)
set httpd port 2812 and
allow admin:monit # 用户名和密码
以下是一个监控Nginx服务的示例配置:
check process nginx with pidfile /var/run/nginx.pid
start program = "/etc/init.d/nginx start"
stop program = "/etc/init.d/nginx stop"
if failed host 127.0.0.1 port 80 protocol http then restart
if cpu > 80% for 5 cycles then alert
if memory > 500 MB for 5 cycles then restart
if 3 restarts within 5 cycles then timeout
check process my_app with pidfile /var/run/my_app.pid
start program = "/usr/local/bin/my_app --daemon"
stop program = "/usr/bin/killall my_app"
if cpu usage > 95% for 10 cycles then restart
if memory > 512 MB then alert
check host myservice with address 127.0.0.1
if failed port 3306 protocol mysql then alert
if failed port 5432 protocol pgsql then alert
start program
: 服务启动命令stop program
: 服务停止命令if failed port
: 检查端口是否可用if changed pidfile
: 检查PID文件是否变化if cpu > (n)%
: CPU使用率监控if memory > (n) MB
: 内存使用监控if loadavg (1min) > (n)
: 系统负载监控启动Monit服务:
sudo systemctl start monit
设置开机启动:
sudo systemctl enable monit
检查配置语法:
sudo monit -t
重新加载配置:
sudo monit reload
查看监控状态:
sudo monit status
set mailserver smtp.example.com port 587
username "user@example.com" password "password"
using tlsv12
set alert admin@example.com
check system $HOST
if loadavg (1min) > 4 then alert
if memory usage > 75% then alert
if cpu usage (user) > 70% then alert
check file apache_error with path /var/log/apache/error.log
if size > 100 MB then alert
monit -t
检查语法通过以上配置,Monit可以有效地监控系统服务的状态,并在服务异常时自动恢复或发送警报。