LoginSignup
1
5

More than 5 years have passed since last update.

[さくらVPS]CentOS7 のプロセス監視にmonitを使ってみる

Last updated at Posted at 2018-04-23

はじめに

さくらVPSを新しく借りたのでphp7 + php-fpm + nginxの環境をつくった
のでmonitでプロセス監視をしてみます。

環境

OS: CentOS Linux release 7.4

前提

メールサーバー: smtp.gmail.com
アラートメールの送り主: alert@example.gmail.com
アラートメールの送り主のパスワード: passw0rd
アラートメールの送り先: xxxxx@example.gmail.com

install

# 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の設定ファイルを作成します。

/etc/monit.d/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
/etc/monit.d/nginx
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)

logを確認する

/etc/monit.log
[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'

restartしているみたいです。

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を使ってカジュアルにプロセス監視する!

1
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
5