天天看點

Linux 程序監控工具 monit

如果你已經使用Cacti Nagios 以及zabbix 等一系列監控工具來監控你的項目,但仍然達不到你想要的程序挂掉可以自動拉起并且郵件報警的功能,那麼請往下看,推薦一款及其好用的程序監控軟體Monit;

Monit是一款功能非常豐富的程序、檔案、目錄和裝置的監測軟體,适用于Linux/Unix平台。 它可以自動修複那些已經停止運作的程式,特使适合處理那些由于多種原因導緻的軟體錯誤,同時Monit 包含一個内嵌的 HTTP(S) Web 界面,你可以使用浏覽器友善地檢視 Monit 所監視的伺服器;

Monit官網:http://mmonit.com/monit/      

CentOS可以直接:

# yum install monit -y      

注:CentOS 可能需要安裝epel源才能有這個軟體包;

這裡就順便講一下怎麼安裝epel源:
## RHEL/CentOS 5 32-Bit ##
# wget http://download.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
# rpm -ivh epel-release-5-4.noarch.rpm

## RHEL/CentOS 5 64-Bit ##
# wget http://download.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
# rpm -ivh epel-release-5-4.noarch.rpm

RHEL/CentOS 6 32-64 Bit

## RHEL/CentOS 6 32-Bit ##
# wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
# rpm -ivh epel-release-6-8.noarch.rpm

## RHEL/CentOS 6 64-Bit ##
# wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# rpm -ivh epel-release-6-8.noarch.rpm



RHEL/CentOS 7 64 Bit
## RHEL/CentOS 7 64-Bit ##
# wget http://dl.fedoraproject.org/pub/epel/beta/7/x86_64/epel-release-7-0.2.noarch.rpm
# rpm -ivh epel-release-7-0.2.noarch.rpm

# ls -1 /etc/yum.repos.d/epel*
/etc/yum.repos.d/epel.repo
/etc/yum.repos.d/epel-testing.repo

# yum repolist
# yum --enablerepo=epel info monit
# yum --enablerepo=epel install monit      

Debian可以直接:

apt-get install monit -y      

接下來看一下yum 裝完軟體包之後有哪些檔案:

# rpm -ql monit
/etc/logrotate.d/monit
/etc/monit.conf
/etc/monit.d
/etc/monit.d/logging
/etc/rc.d/init.d/monit
/usr/bin/monit
/usr/share/doc/monit-5.1.1
/usr/share/doc/monit-5.1.1/CHANGES.txt
/usr/share/doc/monit-5.1.1/COPYING
/usr/share/doc/monit-5.1.1/LICENSE
/usr/share/doc/monit-5.1.1/PLATFORMS
/usr/share/doc/monit-5.1.1/README
/usr/share/doc/monit-5.1.1/README.DEVELOPER
/usr/share/doc/monit-5.1.1/README.SSL
/usr/share/man/man1/monit.1.gz
/var/log/monit      

monit文檔:

https://mmonit.com/monit/documentation/      

檢視預設配置檔案:

# vim etc/monit.conf
set daemon  120           # 預設每隔兩分鐘檢測一次
set logfile syslog facility log_daemon	#預設monit日志為syslog
set idfile /var/.monit.id  #設定Monit監控項目的唯一id檔案位置,預設在$HOME/.monit.id
set statefile /var/.monit.state     #預設存儲每個周期内monitorinig 狀态的檔案
set mailserver mail.bar.baz,               # 預設的主郵件伺服器

mailserver 支援格式為:
SET MAILSERVER <hostname|ip-address [PORT number] [USERNAME string] [PASSWORD string] [using SSLAUTO|SSLV2|SSLV3|TLSV1|TLSV11|TLSV12] [CERTMD5 checksum]>, ...
                [with TIMEOUT X SECONDS]
                [using HOSTNAME hostname]
例如:
set mailserver smtp.gmail.com port 587 username "[email protected]" password "password" using tlsv1 with timeout 30 seconds


set eventqueue			#預設當郵件伺服器不可用時使用eventqueue存儲報警事件
     basedir /var/monit  
     slots 100    		#限制隊列大小

set mail-format {
      from: [email protected]
  	  reply-to: [email protected]
   	  subject: $SERVICE $EVENT at $DATE
      message: Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION.
            Yours sincerely,
            monit
 }


set alert [email protected]                       # receive all alerts


set httpd port 2812 and     # 引用monit自己封裝的web server來監控monit的監控項目狀态;
     use address localhost  # only accept connection from localhost
     allow localhost        # 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

include /etc/monit.d/*      

看完這些是不是已經對配置檔案有所了解了呢,小編為剛剛接觸的同學準備了調優後的一個執行個體配置檔案如下:

# vim etc/monit.conf
set daemon  15           # check services at 2-minute intervals
set logfile /var/log/monit.log                      
set idfile /var/.monit.id
set statefile /var/.monit.state
set mailserver mail.wz.com
		username "[email protected]" password "cacti"
        	with timeout 15 seconds
set eventqueue
     basedir /var/monit  # set the base directory where events will be stored
     slots 100           # optionally limit the queue size
set mail-format {
        from: [email protected]
        subject: Monit Alert -- Host $HOST 's $SERVICE $DESCRIPTION
        message:
        Date:            $DATE
        Action:          $ACTION
        Host:            $HOST
        Description:
        Host $HOST 's $SERVICE $DESCRIPTION
        $EVENT Service $SERVICE
        $DESCRIPTION
}
set alert [email protected]                       # receive all alerts
set httpd port 11111 and
    allow localhost
    allow 192.168.3.0/24
    allow username:password
include /etc/monit.d/*      

這裡需要注意的是,上面配置的郵件服務server需要用到使用者名密碼認證,能力強的同學可以内網搭建一個郵件系統,覺得麻煩的同學可以用騰訊郵箱或者126...balabala

最後一項include /etc/monit.d/* 是定義監控項目配置檔案放置的位置;我們需要監控哪些項目其配置檔案就可以放在這裡;

那小編又為同學們準備了一個執行個體:

# vim /etc/monit.d/perl-fcgi.conf
check process perl-fcgi with pidfile /opt/logs/perl-fcgi.pid
        start program = "/etc/init.d/perl-fcgi start"
        stop program = "/etc/init.d/perl-fcgi stop"
        if 5 restarts within 5 cycles then timeout


# vim /etc/monit.d/nginx.conf
check process nginx with pidfile /opt/logs/nginx.pid
        start program = "/etc/init.d/nginx start"
        stop program = "/etc/init.d/nginx stop"
        if failed port 80 protocol http then restart
        if 5 restarts within 5 cycles then timeout

# vim /etc/monit.d/mysql.conf
check process mysqld with pidfile /data/mariadb/mariadb.pid
        start program = "/etc/init.d/mysqld start"
        stop program = "/etc/init.d/mysqld stop"
        #if failed port 3306 protocol mysql then restart
        if failed unixsocket /tmp/mysql.sock then restart
        if 5 restarts within 5 cycles then timeout      

配置檔案有了,接下來檢測一下文法對不對呢:

# monit -t 
Control file syntax OK      

啟動monit:

/etc/init.d/monit start
Starting monit: monit: generated unique Monit id 5d83a4c163d9bc0218b77f1aae3cdf2d and stored to '/var/.monit.id'
                                                           [  OK  ]      

注意:

如果你的系統裡有防火牆,這裡需要添加防火牆規則;

monit的配置檔案中有一個自帶的web server用來監控monit裡面的監控項目的狀态,有個預設端口2812,如果沒修改的話就添加2812端口的規則,如果修改了就添加你修改後的端口防火牆規則;

monit預設2812端口規則:
# vim /etc/sysconfig/iptables
-A INPUT -p tcp  --dport 2812 -j ACCEPT      
monit修改後端口11111規則:
# vim /etc/sysconfig/iptables
-A INPUT -p tcp  --dport 11111 -j ACCEPT      

記得重新開機iptables:

/etc/init.d/iptables restart      

接下來在浏覽器裡測試通路monit的狀态:

http://192.168.3.5:11111/ 
輸入配置檔案中的賬号和密碼:username password      
Linux 程式監控工具 monit

盡情enjoy吧!

這裡附上幾張郵件報警的截圖:

測試nginx關閉之後是否可以自動拉起呢?

/etc/init.d/nginx stop      
Linux 程式監控工具 monit

馬上就是見證奇迹的時刻!

看,快看!

Linux 程式監控工具 monit