若是你已經使用Cacti Nagios 以及zabbix 等一系列監控工具來監控你的項目,但仍然達不到你想要的進程掛掉能夠自動拉起而且郵件報警的功能,那麼請往下看,推薦一款及其好用的進程監控軟件Monit;mysql
Monit是一款功能很是豐富的進程、文件、目錄和設備的監測軟件,適用於Linux/Unix平臺。 它能夠自動修復那些已經中止運做的程序,特使適合處理那些因爲多種緣由致使的軟件錯誤,同時Monit 包含一個內嵌的 HTTP(S) Web 界面,你可使用瀏覽器方便地查看 Monit 所監視的服務器;ios
Monit官網:http://mmonit.com/monit/
CentOS能夠直接:nginx
# yum install monit -y
注:CentOS 可能須要安裝epel源纔能有這個軟件包;web
這裏就順便講一下怎麼安裝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能夠直接:sql
apt-get install monit -y
接下來看一下yum 裝完軟件包以後有哪些文件:vim
# 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 "user@domain.com" password "password" using tlsv1 with timeout 30 seconds set eventqueue #默認當郵件服務器不可用時使用eventqueue存儲報警事件 basedir /var/monit slots 100 #限制隊列大小 set mail-format { from: monit@foo.bar reply-to: support@domain.com subject: $SERVICE $EVENT at $DATE message: Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION. Yours sincerely, monit } set alert sysadm@foo.bar # 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/*
看完這些是否是已經對配置文件有所瞭解了呢,小編爲剛剛接觸的同窗準備了調優後的一個實例配置文件以下:bash
# 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 "cacti@wz.com" 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: cacti@wz.com 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 cacti@wz.com # 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
盡情enjoy吧!
這裏附上幾張郵件報警的截圖:
測試nginx關閉以後是否能夠自動拉起呢?
/etc/init.d/nginx stop
立刻就是見證奇蹟的時刻!
看,快看!
由郵件時間看出,檢測時間而且拉起進程很是快,10秒以內就檢測到而後進程就又被從新拉起!
是否是很是好用呢?同窗們快用起來!