公司的tomcat服务器java虚拟机运行不稳定,为了安全考虑,安装monit自监控。
Monit是一款功能非常丰富的进程、文件、目录和设备的监测软件,用于Unix平台。它可以自动修复那些已经停止运作的程序,特使适合处理那些由于多种原因导致的软件错误。服务能“死而复生”。
官网 http://mmonit.com/monit/documentation/monit.html
一、安装
#tar -zxvf monit-5.8.1.tar.gz
#cd monit-5.8.1
#./configure --prefix=/usr/local/monit \
--without-pam \
--sysconfdir=/usr/local/monit/etc
#make&make install
#cp -a monit-5.8.1/monitrc /usr/local/monit/etc
#chmod 700 /usr/local/monit/etc/monitrc
二、配置文件
#vim /urs/local/monit/etc/monitrc
set daemon 300 #设置多少时间监测一次,单位为秒
set logfile /usr/local/monit/log/monit.log #设置日志文件位置
set mailserver smtp.xxxxx.com USERNAME "" PASSWORD "" #设置报警邮箱
set mail-format { from: 任意取名@域名 } #设置报警邮件格式,则收到的报警都是来自任 意取名@域名 这个邮箱
set alert 接收报警邮件的邮箱 #设置接收报警邮件的邮箱、有多个的话相同格式另取一行。
set httpd port 2812 and
use address 本机ip # only accept connection from localhost
allow ip(能访问的ip) # allow localhost to connect to the server
allow admin:ccmonit # 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
#自监控tomcat
check host tomcat with address ip(本机ip)
start program = "/usr/local/tomcat/bin/catalina.sh start"
stop program = "/usr/local/tomcat/bin/shutdown.sh"
if failed
port 80
protocol http
request "/test/time.jsp" #时间测试页面
then restart
if 4 restarts within 4 cycles then unmonitor #如果连续重启4次都不能启动tmcat则停止重启
三、测试页
#cd /usr/local/tomcat/webapps/
#mkdir test
#cd test
#vim time.jsp (显示系统当前时间)
<HTML> <HEAD> <SCRIPT LANGUAGE="JavaScript"> function gettime(){ var d = new Date(); document.getElementById("t").innerHTML =d; window.setTimeout("gettime()",1000); } window.onload = gettime; </SCRIPT> </HEAD> <BODY> <div id="t"> </div> </BODY> </HTML>
访问http://ip/test/time.jsp验证
四、monit init脚本
#!/bin/bash # # Init file for Monit # # # chkconfig: - 80 20 # description: monit daemon # # processname: monit source /etc/rc.d/init.d/functions ### Default variables CONFIG="/usr/local/monit/etc/monitrc" [ -x /usr/local/monit/bin/monit ] || exit 1 [ -r "$CONFIG" ] || exit 1 bin_path=/usr/local/monit/bin RETVAL=0 prog="monit" desc="Monit daemon" start() { echo -n $"Starting $desc ($prog): " daemon $bin_path/$prog -c "$CONFIG" RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog return $RETVAL } stop() { echo -n $"Shutting down $desc ($prog): " killproc $prog RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog return $RETVAL } restart() { stop start } reload() { echo -n $"Reloading $desc ($prog): " killproc $prog -HUP RETVAL=$? echo return $RETVAL } case "$1" in start) start ;; stop) stop ;; restart) restart ;; reload) reload ;; condrestart) [ -e /var/lock/subsys/$prog ] && restart RETVAL=$? ;; status) #status $prog $bin_path/$prog status RETVAL=$? ;; *) echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}" RETVAL=1 esac exit $RETV
五、管理进程
#service monit monitor name|all 启动对某服务|所有服务的监控
#service monit unmonitor name|all 关闭对某服务|所有服务的监控
#service monit start name|all 启动某服务|所有服务
#service monit stop name|all 关闭某服务|所有服务
#service monit status 显示监控的服务状态
本文出自 “不莱梅乐队” 博客,谢绝转载!