monit設置服務自動重啓

1. 下載,官網:http://mmonit.com/
wget http://mmonit.com/monit/dist/monit-5.2.5.tar.gz -P /usr/local/src

   
2. 安裝

tar zxvf /usr/local/src/monit-5.2.5.tar.gz -C /usr/local/src
cd /usr/local/src/monit-5.2.5
./configure –prefix=/usr/local/monit
make
# 使用了checkinstall生成rpm包,方便安裝和卸載
checkinstall
rpm -ivh /usr/src/redhat/RPMS/x86_64/monit-5.2.5-1.x86_64.rpm


3. 配置文件

cd /usr/local/src/monit-5.2.5
cp monitrc /etc


4. 設置選項

vi /etc/monitrc
set daemon 120
set logfile syslog facility log_daemon
set idfile /usr/local/monit/monit.id
set statefile /usr/local/monit/monit.state
set mailserver localhost
set alert emailaddress
set httpd port 2812 and use address 192.168.1.10
allow localhost
allow 192.168.1.0/24
allow admin:"password"
include /etc/monit.d/*

  
選項說明:

set daemon 120

設置monit作爲守護進程運行,每2分鐘監視一次

set logfile

設置日誌文件

set idfile

設置id文件

set statefile

設置狀態文件

set mailserver

設置郵件服務器

set alert

設置發送警告的郵箱

set httpd port portnumber and use address ipaddress

設置頁面監控頁面的監聽IP和端口號

allow

允許連接的主機ip,或網段

allow username:password

設置頁面監控訪問的用戶名和密碼

include /etc/monit.d/*

可以將具體監控服務的文件放在這個目錄中,monit會自動包含這些文件


5. 監控具體服務

    monit內置了一些協議,具體的在源代碼目錄的protocols中有,支持的有:aphe_status clamav default dns dwp ftp gener gps http imap ldap2 ldap3 lmtp mehe mysql nntp NOTES ntp3 pgsql radius rdate rsy sip smtp ssh tns

如果有協議支持,就可以直接使用protocol 協議進行測試

如果沒有協議支持,也可以使用send和expect使用不支持的協議進行測試,見尾部的pdf文檔中的協議說明那一節


a. 監控nginx

vi /etc/monit.d/nginx.monit
check process nginx with pidfile /usr/local/nginx/nginx.pid
start program = "/usr/local/nginx/sbin/nginx"
stop program = "/usr/local/nginx/sbin/nginx -s stop"
if failed host 192.168.1.11 port 8011 protocol http then restart


b. 監控memcached

vi /etc/monit.d/memcached.monit
check process memcached with pidfile /tmp/linuxjcq_memcached.pid
start program = "/etc/rc.d/init.d/memcached start"
stop program = "/etc/rc.d/init.d/memcached stop"
if failed host 192.168.1.10 port 11211 protocol memcache then restart


c. 監控php-fpm

vi /etc/monit.d/php-fpm.monit
check process php-fpm with pidfile /usr/local/php/logs/php-fpm.pid
start program = "/usr/local/php/sbin/php-fpm start"
stop program = "/usr/local/php/sbin/php-fpm stop"
if cpu > 50% for 2 cycles then alert
if cpu > 70% for 5 cycles then restart
if failed host 192.168.1.10 port 9000 then restart
if 5 restarts within 5 cycles then timeout


d. 監控mysql

vi /etc/monit.d/mysqld.monit
check process mysqld with pidfile /data/mysql/mysql.pid
start program = "/etc/init.d/mysqld start"
stop program = "/etc/init.d/mysqld stop"
if failed host 192.168.1.10 port 3306 then restart
if 5 restarts within 5 cycles then timeout


6. 測試配置文件

/usr/local/monit/bin/monit -tc /etc/monitrc


7. 添加爲init進程,並啓動

vi /etc/inittab
# Run monit in standard run-levels
mo:2345:respawn:/usr/local/monit/bin/monit -Ic /etc/monitrc


啓動並驗證

telinit q
ps -ef | grep "monit" | grep -v "grep"
root 5655 1 0 07:34 ? 00:00:00 /usr/local/monit/bin/monit -Ic /etc/monitrc
# 結束進程
kill -9 5655
ps -ef | grep "monit" | grep -v "grep"
root 13905 1 0 10:16 ? 00:00:00 /usr/local/monit/bin/monit -Ic /etc/monitrc

    
    注意:在殺死進程後,重啓它後,進程的ID改變了,同時會受到一封報警郵件


8. 測試進程停止

# 拿nrpe進行測試
ps -ef | grep "nrpe" | grep -v "grep"
nagios 13995 1 0 10:18 ? 00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
# 停止進程
service nrpe stop
# 驗證進程停止,沒有nrpe進程
ps -ef | grep "nrpe" | grep -v "grep"
# 在設置的指定時間後,進程自動重啓
ps -ef | grep "nrpe" | grep -v "grep"
nagios 14398 1 0 10:22 ? 00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d


說明:注意兩個nrpe進程的id不一樣,說明重啓了


9. Web監控

可以通過訪問http://ipaddress:port來訪問監控頁面

我自己用nginx做了個代理來訪問,配置如下

vi /usr/local/nginx/conf/vhosts/monit.conf
server
{
listen 192.168.1.11:8012;
server_name 192.168.1.11;
location / {
proxy_pass http://192.168.1.10:2812;
}
}


需要輸入在monitrc文件中設置的用戶名和密碼登錄,頁面如下,可以看到相關服務的狀態信息:


    點擊Process,可以看到相關服務器的完整參數,通過start service, stop service, restart service, disable monitor可以對服務進行相關的操作,如下圖:

10. monit一些常用選項

-c file
指定配置文件
-t
測試配置文件


對服務的操作

start all
啓動所有的監控的服務
stop all
停止所有的監控的服務
restart all
重啓所有的監控的服務
monitor all
監控所有的服務
unmonitor all
取消對所有服務的監控
start name
啓動指定的服務
stop name
停止指定的服務
restart name
重啓指定的服務
monitor name
監控指定的服務
unmonitor name
取消對指定服務的監控


monit自身相關的命令

reload
重新初始化monit
status
打印所有監控的服務的完整狀態報告
summary
打印所有監控的服務概要
quit
退出monit,配置成init後,可以使用monit quit進行重啓


11. 相關文檔

pdf文檔和官網手冊

   
    因爲我只需要用到對服務重啓,monit還有對文件目錄設備的監控,和對不包含的協議的支持,手冊中有詳細的說明。


發佈了32 篇原創文章 · 獲贊 3 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章