ウェブサイト検索

Monit - Linux システムを管理および監視するツール


Monit は、プロセスファイルディレクトリチェックサム権限ファイルシステム、およびApacheNginxなどのサービスMySQLFTPSSHSMTPなどUNIX/Linuxベースのシステムまた、システム管理者に優れた役立つ監視機能を提供します。

monit にはユーザーフレンドリーな Web インターフェイスがあり、ネイティブ HTTP(S) Web サーバーまたはコマンド ライン インターフェイスを使用してシステム ステータスを直接表示し、プロセスをセットアップできます。つまり、monit Web インターフェイスにアクセスして表示するには、ApacheNginx などの Web サーバーがシステムにインストールされている必要があります。

モントにできること

Monit には、プロセスが実行中でない場合に開始し、応答しない場合にプロセスを再起動し、リソースを大量に使用している場合にプロセスを停止する機能があります。さらに、Monit を使用してファイルディレクトリファイルシステムの変更チェックサムの変更などを監視することもできます。ファイル サイズの変更またはタイムスタンプの変更です。

Monit を使用すると、リモート ホストのTCP/IP ポート、サーバー プロトコル、およびpingを監視できます。 Monit は独自のログ ファイルを保持し、重大なエラー状態と回復ステータスについて警告します。

この記事は、RHEL ベースおよび Debian ベースの Linux ディストリビューションでのMonit のインストールと構成に関する簡単なガイドを説明するために書かれています。

ステップ 1: Linux に Monit をインストールする

デフォルトでは、Monit 監視プログラムはデフォルトのシステムベースリポジトリからは利用できません。RHEL に monit パッケージをインストールするには、サードパーティの epel リポジトリを追加して有効にする必要があります。 - ベースのディストリビューション(CentOSRocky LinuxAlmaLinuxなど)。

--------- On RHEL 9 based Systems --------- 
dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm 

--------- On RHEL 8 based Systems --------- 
dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

--------- On RHEL 7 based Systems ---------
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

epel リポジトリを追加したら、次の yum コマンドを実行してMonit パッケージをインストールします。

yum install monit
OR
dnf install monit  [On Fedora Linux]

Ubuntu/Debian/Linux Mint の場合、ユーザーは次のように apt コマンドを使用して簡単にインストールできます。

sudo apt install monit

ステップ 2: Linux での Monit の構成

Monit の設定は非常に簡単で、実際、設定ファイルは非常に読みやすく、ユーザーが理解しやすいように作成されています。実行中のサービスを 2 分ごとに監視し、ログを「/var/log/monit」に保存するように設計されています。

Monit には、Web サーバーを使用してポート 2812 で実行される Web インターフェイスがあります。 Web インターフェースを有効にするには、monit 構成ファイルを変更する必要があります。

monit のメイン設定ファイルは、(RedHat/CentOS/Fedora) および /etc/monit/monitrc/etc/monit.conf にあります。 (Ubuntu/Debian/Linux Mint) 用のファイル。

選択したエディタを使用してこのファイルを開きます。

vi /etc/monitrc
Or
sudo nano /etc/monit/monitrc

次に、次のセクションのコメントを解除し、サーバーの IP アドレスまたはドメイン名を追加し、誰でも接続して monit ユーザーとパスワードを変更できるようにします。または、デフォルトのものを使用することもできます。

set httpd port 2812 and
     use address 0.0.0.0  # only accept connections from localhost
     allow 0.0.0.0/0        # 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

構成したら、monit サービスを開始、有効化、検証して、新しい構成設定をリロードする必要があります。

systemctl start monit
systemctl enable monit
systemctl status monit

これで、次の URL に移動して monit Web インターフェイスにアクセスできるようになります。

http://localhost:2812
OR
http://ip-address:2812
Or
http://example.com:2812

次に、ユーザー名に「admin」、パスワードに「monit」を入力します。以下のような画面が表示されるはずです。

ステップ 3: Linux サービスを監視に追加する

monit Web インターフェイスが正しく設定されたら、監視するプログラムを (RedHat/CentOS/Fedora<) の下の /etc/monitrc に追加します。) と下部にある (Ubuntu/Debian/Linux Mint) の /etc/monit/monitrc ファイル。

以下に、monit の便利な構成例をいくつか示します。これは、サービスがどのように実行されているか、プロファイルがどこに保存されているか、サービスの開始と停止の方法などを確認するのに非常に役立ちます。

Monit で Apache を監視する

check process httpd with pidfile /var/run/httpd.pid
group apache
start program = "/usr/bin/systemctl httpd start"
stop program = "/usr/bin/systemctl httpd stop"
if failed host 127.0.0.1 port 80
protocol http then restart
if 5 restarts within 5 cycles then timeout

Monit で Apache2 を監視する

check process apache with pidfile /run/apache2.pid
start program = "/usr/bin/systemctl apache2 start" with timeout 60 seconds
stop program  = "/usr/bin/systemctl apache2 stop"

Monit で Nginx を監視する

check process nginx with pidfile /var/run/nginx.pid
start program = "/usr/bin/systemctl nginx start"
stop program = "/usr/bin/systemctl nginx stop"

Monit で MySQL を監視する

check process mysqld with pidfile /var/run/mysqld/mysqld.pid
group mysql
start program = "/usr/bin/systemctl mysqld start"
stop program = "/usr/bin/systemctl mysqld stop"
if failed host 127.0.0.1 port 3306 then restart
if 5 restarts within 5 cycles then timeout

Monit で SSH を監視する

check process sshd with pidfile /var/run/sshd.pid
start program "/usr/bin/systemctl sshd start"
stop program "/usr/bin/systemctl sshd stop"
if failed host 127.0.0.1 port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout

すべてのプログラムを監視用に構成したら、monit 構文にエラーがないか確認します。エラーが見つかった場合は修正すれば、何が問題だったのかを特定するのはそれほど難しくありません。 「制御ファイルの構文は OK」のようなメッセージが表示されるか、エラーが表示されない場合は、先に進むことができます。

monit -t
Or
sudo monit -t

考えられるエラーをすべて修正した後、次のコマンドを入力して monit サービスを開始できます。

systemctl monit restart
OR
sudo systemctl monit restart

これは、監視用のすべての Linux サービスを追加した後の monit の様子です。