MonitでApacheとMySQLを復旧させる最低限の設定です。

(文中のドメイン名やメールアドレスは読み替えて下さい)

0. 環境

[CentOS] 6.4 (64bit版カーネル)
[Monit] 5.25.1
[Apache] 2.2.15
[MySQL] 5.1.73

1. セットアップ

  • インストール

    # yum install monit
    
  • 自動起動を設定

    # chkconfig monit on
    

2. 設定

2-1. Apacheの監視設定

  • httpd.conf を開いて、

    # vim /etc/monit.d/httpd.conf
    
  • 下記を記載。

    check process httpd with pidfile /var/run/httpd/httpd.pid
    start program = "/etc/init.d/httpd start"
    stop program = "/etc/init.d/httpd stop"
        
    if 5 restarts within 5 cycles then timeout
    
    if failed host example.net port 443
        protocol HTTP request /home then alert
    group httpd
    

2-2. MySQLの監視設定

  • 次に、mysqld.conf を開いて、

    # vim /etc/monit.d/mysqld.conf
    
  • 下記を記載。

    check process mysqld with pidfile /var/run/mysqld/mysqld.pid
    start program = "/etc/init.d/mysqld start"
    stop program = "/etc/init.d/mysqld stop"
        
    if 5 restarts within 5 cycles then timeout
    
  • シンタックスをチェック。

    # monit -t
    

2-3. Monitの設定

  • バックアップをとってから、/etc/monit.conf を編集。

    # diff /etc/monit.conf /etc/monit.conf.org
    
  • 編集箇所は下記にdiff形式で表示。

    • (1)アラートを送信するメールサーバーを指定。

    • (2)送信元メールアドレスを設定。

    • (3)送信から除外したいMonitのイベントを指定。

      • 後述の「3. プロセス監視」でactionイベントが発生するため除外する。
    85d84
    < set mailserver localhost
    135c134
    < set mail-format { from: monit@example.net }
    ---
    > # set mail-format { from: monit@foo.bar }
    148d146
    < set alert test@gmail.com not on {action}
    
  • Monitを再起動。

    # service monit restart
    

3. 監視停止を回避

  • Monitが unmonitered状態になってしまうことを防ぐため、念のため下記をcronに設定。

    # crontab -l
    */5 * * * * /usr/bin/monit monitor all