尝试使用monit来监视CentOS7中的进程,一选项

开头

我新租了一台Sakura VPS,于是我创建了一个包含PHP7、PHP-FPM和Nginx的环境,并尝试使用Monit进行进程监视。

环境

操作系统:CentOS Linux 发行版 7.4

假设

邮件服务器:smtp.gmail.com
警报邮件发送者:alert@example.gmail.com
警报邮件发送者密码:passw0rd
警报邮件接收者:xxxxx@example.gmail.com

安装

# yum --enablerepo=epel install monit

创建和编辑配置文件

复制默认的设置文件

# cd /etc
# cp monitrc monitrc.org

编辑/etc/monitrc

set daemon 30
  with start delay 60

set log /var/log/monit.log

set mailserver smtp.gmail.com
  port 587
  username "xxxxx@example.gmail.com"
  password "passw0rd"
  using TLSV12
  with timeout 30 seconds

set eventqueue
    basedir /var/monit  # set the base directory where events will be stored
    slots 100           # optionally limit the queue size

set mail-format {
  from:    Monit <alert@example.gmail.com>
  subject: monit alert --  $EVENT $SERVICE
  message: $EVENT Service $SERVICE
                Date:        $DATE
                Action:      $ACTION
                Host:        $HOST
                Description: $DESCRIPTION

           Your faithful employee,
           Monit
}

set alert xxxxx@example.gmail.com

如果想要更改日志文件的输出位置,有时候需要检查是否在/etc/monit.d/logging中设置了配置,请进行删除等操作。

创建监视文件

本次将创建nginx和sshd的配置文件。

check process sshd with pidfile /var/run/sshd.pid
start program = "/bin/systemctl start sshd"
stop program = "/bin/systemctl stop sshd"
if 5 restarts within 5 cycles then unmonitor
check process nginx with pidfile /var/run/nginx.pid
start program = "/bin/systemctl start nginx"
stop program = "/bin/systemctl stop nginx"
if 5 restarts within 5 cycles then unmonitor
if 5 restarts within 5 cycles then unmonitor
(if x restarts within y cycles then unmonitor)
y回のサイクルのなかでx回再起動したら監視対象から外す

启动Monit

# monit -t #設定ファイルのcheck
# systemctl start monit
# systemctl enable monit

确认

试着关闭nginx。

# systemctl stop nginx
# systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since 月 2018-04-23 18:05:27 JST; 1s ago
     Docs: http://nginx.org/en/docs/
  Process: 18725 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
  Process: 18715 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 18716 (code=exited, status=0/SUCCESS)

查看日志

[JST Apr 23 18:03:51] error    : 'nginx' process is not running
[JST Apr 23 18:03:51] info     : 'nginx' trying to restart
[JST Apr 23 18:03:51] info     : 'nginx' start: '/bin/systemctl start nginx'

好像正在重新启动。

请确认nginx的状态。

# systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since 月 2018-04-23 18:05:52 JST; 1min 25s ago
     Docs: http://nginx.org/en/docs/
  Process: 18725 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
  Process: 18731 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 18732 (nginx)
   CGroup: /system.slice/nginx.service
           ├─18732 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           └─18733 nginx: worker process¥

Active已经变成了”active”。
启动已确认。

参考:

尝试在CentOS7上安装monit
使用Amazon Linux轻松进行进程监视,尝试使用monit!

bannerAds