a tomato

トマトが大好きです

monitを使ったサービス簡易監視の設定

本格的な監視をするならzabbixですが、スモールサービスで簡易な監視を行う場合はmonitがお手軽です。

実行環境

CentOS Linux release 7.5.1804 (Core)

インストール

# epel install
[root@vmhost01 ~]# yum install -y monit
Installed:
  epel-release.noarch 0:7-11                                                                                                                                                                                        

Complete!

# monit install
[root@vmhost01 ~]# sudo yum -y install monit
Installed:
  monit.x86_64 0:5.25.1-1.el7                                                                                                                                                                                       

Complete!

ディレクトリ構成

path 役割
/etc/monitrc monitの基本設定
/etc/monit.d 各種監視の設定

プロセス監視

httpdの設定を例にした場合は、以下のように設定する

[root@vmhost01 ~]#  vim /etc/monit.d/httpd
check process httpd with pidfile /var/run/httpd/httpd.pid
        start program = "/bin/systemctl start httpd"
        stop program  = "/bin/systemctl stop httpd"
        if failed
          host localhost
          port 80
          protocol http
        then restart

リソース監視

リソースを監視する設定を例にした場合は、以下のように設定する

[root@vmhost01 ~]#  vim /etc/monit.d/sysresrc
check system 127.0.0.1
## cpu/mem check
 if loadavg (15min) > 2 then alert
 if cpu usage (user) > 90% then alert
 if memory usage > 80% then alert 
## service restart when high load
 if loadavg (1min) > 2 then exec "/etc/init.d/postgresql restart" 
 if loadavg (1min) > 2 then exec "/etc/init.d/httpd restart"
 
##disk check
check device disk_/ with path /
 if space usage > 90% for 5 times within 15 cycles then alert 
 if space usage > 98% then stop

構文チェック

設定した記述が正しいかチェックを行う。

[root@vmhost01 ~]# monit -t
Control file syntax OK

起動設定

# monit起動
[root@vmhost01 ~]#  systemctl start monit

# monit自動起動設定
[root@vmhost01 ~]#  systemctl enable monit

状態確認

現在サーバがどのような状態なのか、以下のコマンド結果で確認が可能です。

[root@vmhost01 ~]# monit status
Monit 5.25.1 uptime: 1m

Process 'httpd'
  status                       Initializing
  monitoring status            Initializing
  monitoring mode              active
  on reboot                    start
  data collected               Fri, 10 Aug 2018 22:33:32

System 'vmhost01'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  load average                 [0.05] [0.05] [0.05]
  cpu                          0.3%us 0.3%sy 0.0%wa
  memory usage                 694.2 MB [37.8%]
  swap usage                   7.5 MB [0.4%]
  uptime                       13d 12h 11m
  boot time                    Sat, 28 Jul 2018 10:22:23
  data collected               Fri, 10 Aug 2018 22:33:32