Linux

Monit CentOS7 Apache, MySQL

 

プロセスの死活や状態監視をして、ミドルウェアがダウンしたら自動で再起動するようにするといった要件に対してMonitを利用することで簡単に実装出来ます。

シェルスクリプトを用意しなくて良いので楽ですね。

 

インストール

# yum install monit

 

# rm -f /etc/monitrc

 

通知設定

# vi /etc/monitrc


# 監視周期
set daemon  30

##ログの出力
set logfile syslog facility log_daemon
set logfile /var/log/monit.log
##アラートメール送信元
set mailserver localhost
##アラートメールの内容
set mail-format {
    from: monit@えふきゅーでぃえぬ
    subject: $SERVICE $EVENT at $DATE
    message: Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION.
            Yours sincerely,
            monit
}

##送信先
set alert root

set httpd port 2812 and
use address localhost # only accept connection from localhost
#allow クライアントのIPアドレス # allow localhost to connect to the server and
allow admin:monit # require user 'admin' with password 'monit'
#allow @monit # allow users of group 'monit' to connect (rw)
#allow @users readonly # allow users of group 'users' to connect readonly


# ファイルの読み込み
include /etc/monit.d/*

 

# chmod 700 /etc/monitrc

 

 

MySQL

# vi /etc/monit.d/mysqld.conf

check process mysqld with pidfile /var/run/mysqld/mysqld.pid
   start program = "/usr/bin/systemctl start mysqld"
   stop program = "/usr/bin/systemctl stop mysqld"
   if failed unixsocket /var/lib/mysql/mysql.sock then restart
   if 5 restarts within 5 cycles then timeout

 

Apache

# vi /etc/monit.d/httpd.conf


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 5 restarts within 5 cycles then timeout

 

 

構文チェック

# monit -t
Control file syntax OK

 

起動、自動起動設定

# systemctl enable monit
# systemctl start monit

 

 

 

 

状態確認

# monit summary


Monit 5.25.1 uptime: 0m
lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqqqk
x Service Name                    x Status                     x Type          x
tqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqnqqqqqqqqqqqqqqqqqqqqqqqqqqqqnqqqqqqqqqqqqqqqu
x localhost.localdomain           x OK                         x System        x
tqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqnqqqqqqqqqqqqqqqqqqqqqqqqqqqqnqqqqqqqqqqqqqqqu
x mysqld                          x OK                         x Process       x
tqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqnqqqqqqqqqqqqqqqqqqqqqqqqqqqqnqqqqqqqqqqqqqqqu
x httpd                           x OK                         x Process       x
mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqvqqqqqqqqqqqqqqqqqqqqqqqqqqqqvqqqqqqqqqqqqqqqj

 

 

 

Cronに登録

# vi /etc/crontab


SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=''

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

##monitがさぼらないように保険
*/50 * * * * /usr/bin/monit monitor all

たまに監視がはずれる時があるので、こうしておくと具合が良いのです。

 

 

ステータス確認

# monit status



Monit 5.25.1 uptime: 1m

Process 'mysqld'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  pid                          1615
  parent pid                   1
  uid                          27
  effective uid                27
  gid                          27
  uptime                       34m
  threads                      31
  children                     0
  cpu                          0.0%
  cpu total                    0.0%
  memory                       6.2% [1.9 GB]
  memory total                 6.2% [1.9 GB]
  security attribute           (null)
  disk read                    0 B/s [27.8 MB total]
  disk write                   0 B/s [12.7 MB total]
  unix socket response time    0.023 ms to /var/lib/mysql.sock type TCP protocol DEFAULT
  data collected               Wed, 18 Jul 2018 17:04:44

Process 'httpd'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  pid                          1281
  parent pid                   1
  uid                          0
  effective uid                0
  gid                          0
  uptime                       34m
  threads                      1
  children                     5
  cpu                          0.0%
  cpu total                    0.0%
  memory                       0.1% [22.5 MB]
  memory total                 0.2% [78.2 MB]
  security attribute           (null)
  disk read                    0 B/s [22.3 MB total]
  disk write                   0 B/s [52 kB total]
  data collected               Wed, 18 Jul 2018 17:04:44

System 'localhost.localdomain'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  load average                 [0.04] [0.04] [0.10]
  cpu                          0.3%us 0.1%sy 0.0%wa
  memory usage                 3.7 GB [11.9%]
  swap usage                   0 B [0.0%]
  uptime                       35m
  boot time                    Wed, 18 Jul 2018 16:29:48
  data collected               Wed, 18 Jul 2018 17:04:44

 

 

Amazonおすすめ

iPad 9世代 2021年最新作

iPad 9世代出たから買い替え。安いぞ!🐱 初めてならiPad。Kindleを外で見るならiPad mini。ほとんどの人には通常のiPadをおすすめします><

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)