目前用它監控某些服務,失敗自動重啟,同時監控特定的日志文件,如果有變化,就發郵件報警
安裝不細寫了,網上好多
我先用cat /proc/version看了下我的系統是el6的,於是wget http://pkgs.repoforge.org/monit/monit-5.5-1.el6.rf.x86_64.rpm
啟動什么的就不管了,直接上配置文件:
set daemon 10 set logfile /var/monit/monit.log set idfile /var/monit/id set statefile /var/monit/state set mailserver smtp.xxxxxxxxx.com port 25 USERNAME "xxxxxxxx@xxxxxxxx.cn" PASSWORD "xxxxxxxx" set mail-format { from: xxxxxx@xxxxxxx.cn subject: monit alert -- $EVENT $SERVICE message: $EVENT Service $SERVICE Date: $DATE Action: $ACTION Host: $HOST Description: $DESCRIPTION } set alert xxxxxxxxx@xxxxxxxx.com with reminder on 3 cycles set httpd port 2812 and use address 192.168.1.45 allow xxxxx:***** allow @users readonly include /etc/monit.d/*
兩個例子:
check process nginx with pidfile /var/run/nginx/nginx.pid start program = "/etc/init.d/nginx start" stop program = "/etc/init.d/nginx stop" if failed host localhost port 80 protocol http then restart
和文件變化的監控
check file test.log with path /tmp/test.log if changed size then alert
##########################################################
今天又測試了下監控目錄變化:
從github上找了下代碼,代碼里的文檔比官網上的細多了。。。。
check directory root with path /root if changed timestamp then alert alert ****@******.com
然后干脆把rocket的監控也加上了
check host 192.168.1.45 with address 192.168.1.45 #check system 192.168.1.45 # if failed icmp type echo count 1 with timeout 3 seconds then alert if failed host 192.168.1.45 port 9876 type tcp then alert if failed host 192.168.1.45 port 10911 type TCP then alert alert xxxxxxx@xxxxxxxxxx.com
##########################################################
#從http://blog.chinaunix.net/uid-21516619-id-1825022.html copy 過來的tomcat監控
check process tomcat with pidfile /var/run/catalina.pid # 這個要另外說明【2】
start program = "/etc/init.d/tomcat start" # 設置啟動命令
stop program = "/etc/init.d/tomcat stop" # 設置停止命令
if 9 restarts within 10 cycles then timeout # 設置在10個監視周期內重
# 啟了9次則超時,不再監視
# 這個服務。原因另外說明【3】
if cpu usage > 90% for 5 cycles then alert # 如果在5個周期內該服務
# 的cpu使用率都超過90%
# 則提示
# 若連續5個周期打開url都失敗(120秒超時,超時也認為失敗)
# 則重啟服務
if failed url http://127.0.0.1:4000/ timeout 120 seconds for 5 cycles then restart
if failed url http://127.0.0.1:5000/ timeout 120 seconds for 5 cycles then restart
使用/var/run/catalina.pid這個pid文件來檢查tomcat這個服務(服務名可以隨便起),tomcat進程默認是不使用pid文件的,pid文件需要顯式為tomcat設置,可以打開tomcat目錄下的bin目錄,打開catalina.sh文件,在開頭(但不是第一行)處加入:
CATALINA_PID=/var/run/catalina.pid
即可指定pid文件,然后重啟tomcat,這樣就可以monit的配置中指定pid文件了。
參考:
http://blog.chinaunix.net/uid-24774106-id-3705291.html
http://blog.csdn.net/fan_hai_ping/article/details/41733783
http://blog.chinaunix.net/uid-21516619-id-1825022.html
https://github.com/arnaudsj/monit
http://mmonit.com/monit/