官網nginx
# 下載二進制文件 文件目錄地址:https://mmonit.com/monit/dist/binary/ # 解壓下載的文件到/usr/local/monit [root@Server-130 monit]# ls bin conf COPYING man # 複製配置文件 [root@Server-130 monit]# cp ./conf/monitrc /etc/ # 環境變量 編輯/etc/profile文件,添加: 「 export PATH=/usr/local/monit/bin:$PATH 」 # 生效 [root@Server-130 monit]# . /etc/profile
daemon 120 # 設置monit做爲守護進程運行,而且每2分鐘監視一次 set logfile /var/log/monit.log # 設置日誌文件的位置,若是要寫入系統日誌能夠"set logfile syslog" # monit內置了一個用於查看被監視服務狀態的http服務器,須要在防火牆中開啓2812 set httpd port 2812 and #use address localhost # only accept connection from localhost use address 192.168.137.130 #allow localhost # allow localhost to connect to the server and allow 192.168.137.0/8 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 # 設置發送郵件的服務器及郵箱 set mailserver smtp.sina.com port 25 USERNAME "xxx@sina.com" PASSWORD "***" # 制定報警郵件的格式 set mail-format { from: linlianpengit@sina.com subject: $SERVICE $EVENT at $DATE message: Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION. } # 指定郵件接收者 set alert XXX@qq.com with reminder on 3 cycles # 檢測Nginx check process Nginx with pidfile /var/run/nginx.pid start program = "/usr/sbin/nginx -c /etc/nginx/nginx.conf" stop program = "/usr/sbin/nginx -s stop" if failed host 127.0.0.1 port 80 then restart if 5 restarts within 5 cycles then timeout # 檢測Tomcat # tomcat進程默認是不使用pid文件的,pid文件須要顯式爲tomcat設置,能夠打開tomcat目錄下的bin目錄,打開catalina.sh文件,在開頭(非首行)加入:CATALINA_PID=/var/run/catalina.pid check process Tomcat with pidfile /var/run/tomcat.pid start program = "/usr/local/apache-tomcat-7.0.70/bin/startup.sh" stop program = "/usr/local/apache-tomcat-7.0.70/bin/shutdown.sh" if failed port 8080 for 5 cycles then restart if 3 restarts within 5 cycles then timeout
[root@Server-130 ~]# monit -t Control file syntax OK
[root@Server-130 ~]# monit
在inittab中加入隨系統啓動的設置,使得monit進程若是中止,init進程會將其重啓git
[root@Server-130 ~]# echo "moni:2345:respawn:/usr/local/monit-5.10/bin/monit -Ic /etc/monitrc" >> /etc/inittab
因爲將monit設置成了守護進程,而且在inittab中加入了隨系統啓動的設置,則monit進程若是中止,init進程會將其重啓,而monit又監視着其它的服務, 這意味着monit所監視的服務不能使用通常的方法來中止,由於一中止,monit又會將其啓動。要中止monit所監視的服務,應該使用monit stop name這樣的命令。 例如要中止tomcat:
[root@Server-130 ~]# monit stop tomcat
要中止所有monit所監視的服務能夠使用:
[root@Server-130 ~]# monit stop all
啓動所有,則是
[root@Server-130 ~]# monit start all
web