如何在CentOS 7系统Vesta CP面板配置Monit进程?

  • A+

之前陈沩亮博客有分享过CentOS 6 安装和配置Monit的教程 ▼

但是,在CentOS 7 配置Monit监控程序,和 CentOS 6 有些区别,是不完全一样的。

如果你的Linux主机使用CentOS 7系统,在Vesta CP面板安装了 Monit 后,可以跟着本教程进行配置Monit监控程序。

Monit监控进程的服务

以下是Monit监控进程的服务配置内容(删除掉较少用的部分监控服务):

  • clamd
  • crond
  • dovecot
  • exim
  • httpd
  • memcached
  • MySQL
  • nginx
  • spamassassin
  • sshd
  • vesta-nginx
  • vesta-php
  • vsftpd
  • 监控空间 和 索引节点 on
  • 监控系统资源(RAM,交换,CPU,负载)

Monit监控配置

CentOS 6 & 7 的 Monit配置文件名称是不同的:

  • CentOS 6 的 Monit配置文件名称是 “monit.conf”
  • CentOS 7 的 Monit配置文件名称是 “monitrc”

用 SFTP 软件进入你的Linux服务器后,编辑Monit配置文件 ▼

/etc/monitrc

将以下的配置内容,添加到这个 “monitrc” 文件中 ▼

##
## 陈沩亮博客示例monit配置文件说明:
## 1. 域名以 www.etufo.org 为例。
## 2. 后面带xxx的均是举例用的名字,需要根据自己的需要修改。
##
################################################## #############################
## Monit control file
################################################## #############################
#
# 检查周期,默认为2分钟,对于网站来说有点长,可以根据需要自行调节,这改成30秒。
set daemon 30

include /etc/monit.d/*

# 日志文件
set logfile /var/log/monit.log

#
# 邮件通知服务器
#
#set mailserver mail.example.com
set mailserver localhost with timeout 30 seconds

#
# 通知邮件的格式设置,下面是默认格式供参考
#
## Monit by default uses the following alert mail format:
##
## --8<--
## From: monit@$HOST # sender
## Subject: monit alert -- $EVENT $SERVICE # subject
##
## $EVENT Service $SERVICE #
## #
## Date: $DATE #
## Action: $ACTION #
## Host: $HOST # body
## Description: $DESCRIPTION #
## #
## Your faithful employee, #
## monit #
## --8<--
##
## You can override the alert message format or its parts such as subject
## or sender using the MAIL-FORMAT statement. Macros such as $DATE, etc.
## are expanded on runtime. For example to override the sender:
#
# 简单的,这只改了一下发送人,有需要可以自己修改其它内容。
set mail-format { from: admin@xxxx }

# 设置邮件通知接收者。建议发到gmail,方便邮件过滤。
set alert xxx@xxxx

set httpd port 2812 and #设置http监控页面的端口
use address www.etufo.org # http监控页面的IP或域名
##allow localhost # 允许本地访问
##allow 203.82.90.239 # 允许指定IP访问
allow user:password # 访问用户名密码

################################################## #############################
## Services
################################################## #############################
#
# 系统整体运行状况监控,默认的就可以,可以自己去微调
#
# 系统名称,可以是IP或域名
#check system www.etufo.org
# if loadavg (1min) > 4 then alert
# if loadavg (5min) > 2 then alert
# if memory usage > 75% then alert
# if cpu usage (user) > 70% then alert
# if cpu usage (system) > 30% then alert
# if cpu usage (wait) > 20% then alert


# 可选的ssl端口的监控,如果有的话
# if failed port 443 type tcpssl protocol http
# with timeout 15 seconds
# then restart

# 监控mariadb
check process mysql with pidfile /var/run/mariadb/mariadb.pid
start program = "/usr/bin/systemctl start mariadb"
stop program = "/usr/bin/systemctl stop mariadb"
if failed host 127.0.0.1 port 3306 for 2 cycles then restart
if 2 restarts within 4 cycles then timeout

# 监控httpd
check process httpd with pidfile /var/run/httpd/httpd.pid
start program = "/usr/bin/systemctl start httpd"
stop program = "/usr/bin/killall -9 httpd"
if children > 120 for 2 cycles then restart
if failed host localhost port 8080 protocol http for 2 cycles then restart
if 4 restarts within 10 cycles then timeout

# 监控nginx
check process nginx with pidfile /var/run/nginx.pid
start program = "/usr/bin/systemctl start nginx"
stop program = "/usr/bin/systemctl stop nginx"
if failed host localhost port 80 protocol http for 2 cycles then restart
if 4 restarts within 10 cycles then timeout

# 监控sshd
check process sshd with pidfile /var/run/sshd.pid
start program = "/usr/bin/systemctl start sshd"
stop program = "/usr/bin/systemctl stop sshd"
if failed host localhost port 22 protocol ssh for 2 cycles then restart
if 4 restarts within 10 cycles then timeout

# 监控vesta-nginx
check process vesta-nginx with pidfile /var/run/vesta-nginx.pid
start program = "/usr/bin/systemctl start vesta"
stop program = "/usr/bin/systemctl stop vesta"
if failed host localhost port 8083 protocol https for 2 cycles then restart
if 4 restarts within 10 cycles then timeout

# 监控vesta-php
check process vesta-php with pidfile /var/run/vesta-php.pid
start program = "/usr/bin/systemctl start vesta"
stop program = "/usr/bin/systemctl stop vesta"
if failed host localhost port 8083 protocol https for 2 cycles then restart
if 4 restarts within 10 cycles then timeout

# 监控vsftpd
check process vsftpd with match vsftpd
start program = "/usr/bin/systemctl start vsftpd"
stop program = "/usr/bin/systemctl stop vsftpd"
if failed host localhost port 21 protocol ftp for 2 cycles then restart
if 4 restarts within 10 cycles then timeout

#监控crond
check process crond with pidfile /var/run/crond.pid
start program = "/usr/bin/systemctl start crond"
stop program = "/usr/bin/systemctl stop crond"
if 5 restarts within 5 cycles then timeout

#监控dovecot
check process dovecot with pidfile /var/run/dovecot/master.pid
start program = "/usr/bin/systemctl start dovecot"
stop program = "/usr/bin/systemctl stop dovecot"
if failed host localhost port 143 protocol imap for 2 cycles then restart
if 4 restarts within 10 cycles then timeout

#监控exim
check process exim with pidfile /var/run/exim.pid
start program "/usr/bin/systemctl start exim"
stop program "/usr/bin/systemctl stop exim"
if failed host localhost port 25 protocol smtp for 2 cycles then restart
if 4 restarts within 10 cycles then timeout

如果修改过MONIT配置内容,需要重新启动 Monit 服务才能生效 ▼

systemctl restart monit

2018年5月26日更新:

  • 推荐使用以上的配置内容 ▲
  • 不推荐使用以下SSH 命令,生成 Monit 监控服务的文件。
cd /etc/monit.d/
git clone https://github.com/infinitnet/vesta-centos7-monit.git ./ && rm -f README.md
myip=1.2.3.4
sed -i 's/host localhost/host '$myip'/g' /etc/monit.d/*.conf
systemctl restart monit
  • 用你的共享IP替换1.2.3.4

因为可能会出现错误,找不到git bash command ▼

还可能会出现输入此命令后 ▼

sed -i 's/host localhost/host '$myip'/g' /etc/monit.d/*.conf

出现以下错误提示 ▼

sed: can't read /etc/monit.d/*.conf: No such file or directory

Monit日志查看错误

SSH查看monit.log 日志文件命令 ▼

tailf /var/log/monit.log
  • SSH查看monit.log文件,只能显示最近10行日志。

建议SFTP登录 /var/log/monit.log 以完全查看之前 monit.log 日志文件的状态。

邮件发送错误

如果Monit邮件无法发送,会出现以下错误提示 ▼

error : Cannot open a connection to the mailserver localhost:25 -- Operation now in progress
error : Mail: Delivery failed -- no mail server is available

解决邮件发送错误

陈沩亮建议使用Gmail SMTP ▼

set mailserver smtp.gmail.com port 587
username "user@domain.com"
password "password"
using tlsv1
with timeout 30 seconds

解决方法是替换 Monit 配置文件里的 set mailserver,详情请查看这篇教程 ▼

filesystem statistic错误

如果在 monit.log 日志文件中,发现以下错误问题 ▼

filesystem statistic error: cannot read /proc/diskstats -- No such file or directory
  • 可能因为 Linux 默认最小化安装 VestaCP面板,不存在 /proc/diskstats 磁盘状态监控文件。

解决filesystem statistic错误

第 1 步:SSH 进入 /etc/monit.d 目录 ▼

cd /etc/monit.d

第 2 步:删除 Monit 监控服务文件  “root-space.conf” ▼

rm -rf root-space.conf

第 3 步:SSH 重启 Monit监控 ▼

systemctl restart monit.service

Monit命令(CentOS 7专用)

查看 Monit 启动状态 ▼

systemctl status monit.service

启动 Monit 服务 ▼

systemctl start monit.service

关闭 Monit 服务 ▼

systemctl stop monit.service

重新启动 Monit 服务 ▼

systemctl restart monit

开机启动 Monit 服务 ▼

systemctl enable monit.service

开机关闭 Monit 服务 ▼

systemctl disable monit.service

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: