前言

Monit是一款功能非常丰富的进程、文件、目录和设备的监测软件,适用于Linux/Unix平台。它可以监控服务器进程状态、HTTP/TCP状态码、服务器资源变化、文件系统变动等等,根据这些变化,可以设定邮件报警、重启进程或服务、自动修复那些已经停止运作的程序,特别适合处理那些由于多种原因导致的软件错误、监控系统关键的进程和资源。同时Monit 包含一个内嵌的 HTTP(S) Web 界面,你可以使用浏览器方便地查看 Monit 所监视的服务器。此外,M/Monit可以把多台安装Monit的服务器集中起来一起管理。

下面是引用官方的解释:

Monit is a small Open Source utility for managing and monitoring Unix systems. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations.

官网:https://mmonit.com/monit
文档:https://mmonit.com/monit/documentation/monit.html
最新版本:https://mmonit.com/monit/dist/binary/5.26.0/monit-5.26.0-linux-x64.tar.gz

安装

# 安装epel
[root@localhost software]# yum -y install epel-release   

# 确认一下是否有monit包
[root@localhost software]# yum list| grep monit  
....
monit.x86_64                             5.26.0-1.el7                  epel     
monitor-edid.x86_64                      3.2-1.el7                     epel     
monitorix.noarch          
...
  
# 安装monit 
[root@localhost software]# yum -y install monit 

# 检验安装
[root@localhost software]# monit -V 
This is Monit version 5.26.0
Built with ssl, with ipv6, with compression, with pam and with large files
Copyright (C) 2001-2019 Tildeslash Ltd. All Rights Reserved.

# 开启启动&启动服务
[root@localhost software]# systemctl start monit
[root@localhost software]# systemctl enable monit
Created symlink from /etc/systemd/system/multi-user.target.wants/monit.service to /usr/lib/systemd/system/monit.service.
[root@localhost software]# monit
Starting Monit 5.26.0 daemon with http interface at [localhost]:2812

常用命令

monit -t # 配置文件检测
monit # 启动monit daemon
monit -c /var/monit/monitrc # 启动monit daemon时指定配置文件
monit reload # 当更新了配置文件需要重载
monit status # 查看所有服务状态
monit status nginx # 查看nginx服务状态
monit stop all # 停止所有服务
monit stop nginx # 停止nginx服务
monit start all # 启动所有服务
monit start nginx # 启动nginx服务
monit -V # 查看版本

配置

/etc/monitrc # 主配置文件
/etc/monit.d/ # 单独配置各项服务

为了文件和密码安全性,monitrc必须具有读写权限不超过0700(u=xrw,g=,o=)。

主配置文件

# 默认配置
[root@localhost software]# grep -v "^#" /etc/monitrc
set daemon  30              # check services at 30 seconds intervals
set log syslog



set httpd port 2812 and
    use address localhost  # only accept connection from localhost (drop if you use M/Monit)
    allow localhost       # allow localhost to connect to the server and
    allow admin:monit      # require user 'admin' with password 'monit'
    #with ssl {            # enable SSL/TLS and set path to server certificate
    #    pemfile: /etc/ssl/certs/monit.pem
    #}

include /etc/monit.d/*


#修改后的
[root@localhost software]# grep -v "^#" /etc/monitrc
set daemon  30              # 每 30 秒检查被监控服务的状态
set log syslog

# 邮箱设置
set mailserver xxx@xxx
username "xxx" password "xxx"
# using ssl
set alert xxx@xxx
set alert xxx@xxx #可以设置多个

set mail-format {
from: xxx@xxx
subject: [$SERVICE] $EVENT
message:
[$SERVICE] $EVENT

Date: $DATE
Action: $ACTION
Host: $HOST
Description: $DESCRIPTION

Your faithful employee,
Monit }
    
set httpd port 2812 and         # 监听端口
    use address 192.168.3.100  # 监听地址
    allow 192.168.3.100        # 这个一定要加,解决 Error receiving data -- Connection reset by peer 报错。
    allow 192.168.3.0/24       # 允许192.168.3.0/24段访问
    allow localhost       # allow localhost to connect to the server and
    #allow 0.0.0.0/0.0.0.0  # 允许任何地址访问
    allow admin:monit      # 访问web账密
    #with ssl {            # enable SSL/TLS and set path to server certificate
    #    pemfile: /etc/ssl/certs/monit.pem
    #}

include /etc/monit.d/*  # 监控服务配置文件目录

监控项目配置

监控服务器配置我们统一放在/etc/monit.d/*

Nginx

check process nginx with pidfile  /usr/local/nginx/logs/nginx.pid
        start program = "/usr/local/nginx/sbin/nginx"
        stop program = "/usr/local/nginx/sbin/nginx -s stop"

php-fpm

check process php-fpm with pidfile /usr/local/php/var/run/php-fpm.pid
    start program = "/etc/init.d/php-fpm start"
    stop program  = "/etc/init.d/php-fpm stop"
    if failed unixsocket /tmp/php-cgi.sock then restart
    if 3 restarts within 5 cycles then timeout

配置文件写好之后,我们执行monit -t检测一下语法,没问题的话monit reload就可以使其生效了。

[root@localhost monit.d]# monit reload
Reinitializing monit daemon
[root@localhost monit.d]# monit status
Monit 5.26.0 uptime: 2h 59m

Process 'php-fpm'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  pid                          10509
  parent pid                   1
  uid                          0
  effective uid                0
  gid                          0
  uptime                       1d 15h 10m
  threads                      1
  children                     2
  cpu                          0.0%
  cpu total                    0.0%
  memory                       0.9% [4.5 MB]
  memory total                 3.2% [15.7 MB]
  security attribute           -
  disk read                    0 B/s [4 kB total]
  disk write                   0 B/s [4 kB total]
  unix socket response time    0.026 ms to /tmp/php-cgi.sock type TCP protocol DEFAULT
  data collected               Fri, 15 May 2020 13:45:32

Process 'nginx'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  pid                          14722
  parent pid                   1
  uid                          0
  effective uid                0
  gid                          0
  uptime                       33m
  threads                      1
  children                     1
  cpu                          0.0%
  cpu total                    0.0%
  memory                       0.6% [3.0 MB]
  memory total                 5.7% [27.7 MB]
  security attribute           -
  disk write                   0 B/s [4 kB total]
  data collected               Fri, 15 May 2020 13:45:32

System 'localhost.localdomain'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  load average                 [0.00] [0.01] [0.05]
  cpu                          0.0%us 0.2%sy 0.0%wa
  memory usage                 269.7 MB [55.4%]
  swap usage                   138 MB [9.0%]
  uptime                       6d 22h 0m
  boot time                    Fri, 08 May 2020 15:44:44
  data collected               Fri, 15 May 2020 13:45:32

你也可以通过问吧web页面http://IP:2812查看状态。

文章目录