LoginSignup
0
0

monitを使ってサーバー監視

Posted at

monitとは

本記事では簡易監視ツールのmonitについてご紹介します。monitはオープンソースのツールで一定周期で監視し、設定したイベント検知すると自動でコマンドを実行してくれる便利なツールです。

関連リンク
モニト

#monit設定

インストール

まずmonitをインストールしましょう。epelリポジトリからyumで簡単にインストールすることができます。

yum install epel-release
 yum install monit

#監視基本設定
インストールが無事に成功したら以下の設定ファイルからmonitの設定を行っていきます。※バージョンやディストリビューションによって設定ファイル名は異なるのでご注意ください。

# vi /etc/monitrc

監視の周期を確認します。秒単位で設定が可能です。あまり周期が短いと負荷がかかるためデフォルトの30秒で設定しています。

 set daemon  30              # check services at 30 seconds intervals

monitはsyslogでの出力が可能です。特定のログファイルに出力したい場合は、ここにフルパスを記載します。後ほど記載しますが、デフォルトで/var/log/monit.logにもログは出力されます。

set log syslog

#監視内容設定
Apacheを監視するように設定する

# vi /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 port 80 with timeout 10 seconds then restart   #80番ポートが10秒タイムアウトしたら再起動する

if failed port 443 with timeout 10 seconds then restart  #443番ポートが10秒タイムアウトしたら再起動する

if 10 restarts within 10 cycles then unmonitor  #10回中10回再起動したら監視を止める

#設定確認
以下コマンドで設定の文法チェックを行ってくれます。

# monit -t
> Control file syntax OK と出ることを確認

monitを起動し、自動起動設定を行います。

# systemctl start monit

# systemctl enable monit

monitが起動したことと、自動起動されるようになったことを確認する

# systemctl status monit

# systemctl is-enabled monit

これにてmonitのインストールは完了です。

0
0
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
0
0