使用Monit監控與自動重啟動系統服務

一、安裝

        sudo apt-get install monit

二、設定

        依照自己系統有安裝的程式新增對應的設定檔

        1.監控amavis,新增檔案/etc/monit/conf.d/amavis

                  檔案內容如下:            

          check process amavisd with pidfile /var/run/amavis/amavisd.pid
                group mail
                start program = "/etc/init.d/amavis start"
                stop  program = "/etc/init.d/amavis stop"
                if failed port 10024 protocol smtp then restart
                if 5 restarts within 5 cycles then timeout 

            2.控apache2,新增檔案/etc/monit/conf.d/apache2

          檔案內容如下:

           check process apache2 with pidfile /var/run/apache2.pid
                 group www
                 start program = "/etc/init.d/apache2 start"
                 stop program = "/etc/init.d/apache2 stop"
                 if failed host localhost port 80 protocol http
                 with timeout 10 seconds
                 then restart
                 if 5 restarts within 5 cycles then timeout
 

          注意:此處是監控apache預設port 80,請依自身設定修改port number

           3.控dovecot,新增檔案/etc/monit/conf.d/dovecot

          檔案內容如下:

          check process dovecot with pidfile /var/run/dovecot/master.pid
                group mail
                start program = "/etc/init.d/dovecot start"
                stop program = "/etc/init.d/dovecot stop"
                group mail
                if failed port 993 type tcpssl sslauto protocol imap for 5 cycles then restart
                if 3 restarts within 5 cycles then timeout

                 注意:此處是監控dovecot提供的imaps服務

        4.控mysql,新增檔案/etc/monit/conf.d/mysql

        檔案內容如下:

         check process mysqld with pidfile /var/run/mysqld/mysqld.pid
               group database
               start program = "/etc/init.d/mysql start"
               stop program = "/etc/init.d/mysql stop"
               if failed host localhost port 3306 protocol mysql then restart
               if 5 restarts within 5 cycles then timeout

             注意:此處監控mysql預設的port 3306,若不是請修改為自己定義的port

       5.控memcached,新增檔案/etc/monit/conf.d/memcached

       檔案內容如下:

        check process memcached with pidfile /var/run/memcached.pid
              group www
              start program = "/etc/init.d/memcached start"
              stop program = "/etc/init.d/memcached stop"
              if failed host localhost port 11211 then restart
              if 5 restarts within 5 cycles then timeout
       6.控postfix,新增檔案/etc/monit/conf.d/postfix

            檔案內容如下:

      check process postfix with pidfile /var/spool/postfix/pid/master.pid
            group mail
            start program = "/etc/init.d/postfix start"
            stop  program = "/etc/init.d/postfix stop"
            if failed port 25 protocol smtp then restart
            if 5 restarts within 5 cycles then timeout

     7.控spamassassin,新增檔案/etc/monit/conf.d/spamassassin

              檔案內容如下:
         check process spamassassin with pidfile /var/run/spamd.pid
               group mail
               start program = "/etc/init.d/spamassassin start"
               stop  program = "/etc/init.d/spamassassin stop"
               if 5 restarts within 5 cycles then timeout
      8.控sshd,新增檔案/etc/monit/conf.d/sshd

        檔案內容如下:

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

           注意:sshd的安全連線設定記得要開放127.0.0.1存取

               編輯sudo gedit /etc/hosts.allow

               加上  sshd:127.0.0.1:ALLOW

設定monit主要的設定檔/etc/monit/monitrc

             sudo gedit /etc/monit/monitrc

        裡面有英文說明,請依需求移掉註記後並修改為自身系統狀態

        例如:設定通知Email伺服器

        #
        ## Set the list of mail servers for alert delivery. Multiple servers may be
        ## specified using a comma separator. If the first mail server fails, Monit
        # will use the second mail server in the list and so on. By default Monit uses
        # port 25 - it is possible to override this with the PORT option.
        #
        # set mailserver mail.bar.baz,               # primary mailserver
        #                backup.bar.baz port 10025,  # backup mailserver on port 10025
        #                localhost                   # fallback relay
        set mailserver main.example.com,               # primary mailserver
                   second.example.com port 10025,  # backup mailserver on port 10025
                          localhost                   # fallback relay

四、設定完成後重新啟動

        sudo service monit restart

 

參考資料

http://www.exratione.com/2012/05/a-mailserver-on-ubuntu-1204-postfix-dovecot-mysql/

,

K 發表在 痞客邦 PIXNET 留言(0) 人氣()