gamiyama3110 / gamiyama.github.io

技術ブログ(の予定)【動かん】
0 stars 0 forks source link

dockerでmonitの検証環境構築 #4

Open gamiyama3110 opened 4 years ago

gamiyama3110 commented 4 years ago

環境作成

systemctlを使えるように --privileged /sbin/init を指定

docker run -d --privileged --name monit-work -it centos:7 /sbin/init
docker exec -it monit-work /bin/bash
yum -y update
yum -y install epel-release
yum -y install monit

起動

systemctl start monit

# 確認
[root@fb0cc3775ea4 /]# systemctl status monit
● monit.service - Pro-active monitoring utility for unix systems
   Loaded: loaded (/usr/lib/systemd/system/monit.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-06-03 11:39:44 UTC; 8s ago
...

[root@fb0cc3775ea4 /]# tail -f /var/log/monit.log
[UTC Jun  3 11:39:44] info     :  New Monit id: 292c1f7c8758211287c81b92c38f0579
 Stored in '/root/.monit.id'
[UTC Jun  3 11:39:44] info     : Starting Monit 5.26.0 daemon with http interface at [localhost]:2812
[UTC Jun  3 11:39:44] info     : 'fb0cc3775ea4' Monit 5.26.0 started

テスト用アパッチインストール

yum -y install httpd
systemctl start httpd

[root@fb0cc3775ea4 /]# curl localhost
[root@fb0cc3775ea4 /]# tail -f /var/log/httpd/access_log
127.0.0.1 - - [03/Jun/2020:11:53:28 +0000] "HEAD / HTTP/1.1" 403 - "-" "curl/7.29.0"

設定

[root@fb0cc3775ea4 /]# grep daemon /etc/monitrc
## Start Monit in the background (run as a daemon):
set daemon  6              # check services at 30 seconds intervals

cat << EOF > /etc/monit.d/myservice
check process httpd with pidfile /var/run/httpd/httpd.pid
  every 2 cycle
  start program "/bin/systemctl start httpd"
  stop program "/bin/systemctl stop httpd"
EOF

# 構文チェック
[root@fb0cc3775ea4 /]# monit -t
Control file syntax OK

# リロード
monit reload

[root@fb0cc3775ea4 /]# monit status
Monit 5.26.0 uptime: 31m

Process 'httpd'
  status                       Does not exist
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  data collected               Wed, 03 Jun 2020 12:10:45

httpdで検証

[root@fb0cc3775ea4 /]# systemctl stop httpd
[root@fb0cc3775ea4 /]# tail -f /var/log/monit.log
[UTC Jun  3 12:15:45] error    : 'httpd' process is not running
[UTC Jun  3 12:15:45] info     : 'httpd' trying to restart
[UTC Jun  3 12:15:45] info     : 'httpd' start: '/bin/systemctl start httpd'

再起動してくれた。

外部サービスを監視

check host backend with address backend.hogehoge.com
  if failed url http://backend.hogehoge.com/
  then restart
# hosts変えて繋がらなくする
[root@fb0cc3775ea4 /]# tail -f /var/log/monit.log
[UTC Jun  3 12:42:14] error    : 'backend' failed protocol test [HTTP] at [backend.hogehoge.com]:80/ [TCP/IP] -- HTTP error: Server returned status 403
[UTC Jun  3 12:42:14] info     : 'backend' trying to restart
# hosts戻す
[UTC Jun  3 12:42:20] info     : 'backend' connection succeeded to [backend.hogehoge.com]:80/ [TCP/IP]
gamiyama3110 commented 4 years ago

urlのチェックはステータスまで見てない? 500系でも再起動してくれる?

gamiyama3110 commented 4 years ago

メールまでセットアップしとけばよかった alertの検証もしてみたい

gamiyama3110 commented 4 years ago

ま、追々。

gamiyama3110 commented 4 years ago

記事にしてないや