问题描述
如何在tomcat服务器启动后就监控数据库,用来定时发送邮件、短信、消息等,这个监控服务应该如何实现或给我点提示?
解决方案
你的意思应该就是在tomcat启动后监控数据库某个表,然后定时发送邮件、短信、消息?如果是这个意思,那么有以下几个办法:1.使用java ee中的Listener,具体方式,就是实现ServletContextListener接口,并且在实现类中,使用jdk的Timer、TimerTask完成定时任务。具体可参考:http://blog.csdn.net/jinchun1234/article/details/4205358这种方式的缺点:使用jdk的timer,不满足精确度很高的定时任务,而且某个任务出现问题,会导致timer死掉,不再执行。2.你的项目使用了spring,可以通过spring集成timer来完成定时任务,可参考:http://blog.csdn.net/qingzhizhu888/article/details/4500922但是我更建议使用quartz这个定时任务框架。1.2方式都是依赖于tomcat应用,即tomcat挂掉,那么定时任务的线程也随之消失。3.使用独立的定时任务应用监控数据库某个表,由于你需要定时发送邮件、短信、消息,会出现线程执行比较久的情况,那么在独立的定时任务的应用中完成,对tomcat跑的应用无任何影响。简单点,使用1方式。如果定时要求比较高,就使用spring+quartz,如果定时频率高,且耗时长,推荐第三种。
解决方案二:
建议LZ参照一下这个软件——ManageEngine Applications Manager,它支持对各种软硬件资源的监控,包括。可以把软件下来,是商业软件,但是可以免费使用,有使用限制,开发当中够用了,LZ可以参照它监控了哪些数据和指标,甚至可以反编译看它的源码(俺就不厚道地这么干过)。
解决方案三:
如果你是linux服务器 且需要报警 可以参考我的#!/bin/sh mailAddresses=(13466789569@139.com);function sendMail() { for address in ${mailAddresses[*]}; do echo $1 | mutt -s $1 13466789569@139.com $address; done; }#服务不可用监控tomcatSSonlinePid=`ps aux | grep tomcat-ssonline | grep -v grep | grep -v retomcat | awk '{print $2}'` ;echo "tomcat ssonline pid=" $tomcatSSonlinePid;if [ -z "$tomcatSSonlinePid" ]then sh /usr/program/scripts/startSsonline.sh & sendMail "ssonline alert-server tomcat-ssonline not start,starting!";fitomcatForumPid=`ps aux | grep tomcat-forum | grep -v grep | grep -v tail | grep -v retomcat | awk '{print $2}'` ;echo "tomcat forum pid=" $tomcatForumPid;if [ -z "$tomcatForumPid" ]then sh /usr/program/scripts/startForum.sh & sendMail "ssonline alert-server tomcat-forum not start,starting!";fitomcatV1Pid=`ps aux | grep tomcat-v1 | grep -v grep | grep -v tail | grep -v tail | grep -v retomcat | awk '{print $2}'` ;echo "tomcat v1 pid=" $tomcatV1Pid;if [ -z "$tomcatV1Pid" ]then sh /usr/program/scripts/startV1.sh & sendMail "ssonline alert-server tomcat-v1 not start,starting!";fijamesPid=`ps aux | grep james | grep -v grep | grep -v tail | grep -v retomcat | awk '{print $2}'` ;echo "james pid=" $jamesPid;if [ -z "$jamesPid" ]then sh /usr/program/scripts/startJames.sh & sendMail "ssonline alert-server tomcat-james not start,starting!";fimysqlPid=`ps aux | grep libexec/mysqld | grep -v grep | grep -v tail | grep -v retomcat | awk '{print $2}'` ;echo "mysql pid=" $mysqlPid;if [ -z "$mysqlPid" ]then /usr/local/mysql/bin/mysqld_safe & sendMail "ssonline alert-mysql not start,starting!";fi#服务异常监控wget http://localhost:8080/monitor/jvm/status.jsp -O status;ssonlineStatus=` cat ./status | tr 'n' ' ' | tr 'r' ' ' | awk 'gsub(/^ *| *$/,"")'`;echo "ssonline status";echo $ssonlineStatus;if [ -n v1Status -a "200" = "$ssonlineStatus" ]then echo "ssonline ok";else sendMail "ssonline alert-tomcat-ssonline status warn!";fiwget http://localhost:9080/forum/monitor/jvm/status.jsp -O status;forumStatus=` cat ./status | tr 'n' ' ' | tr 'r' ' ' | awk 'gsub(/^ *| *$/,"")'`;echo "forum status";echo $forumStatus;if [ -n v1Status -a "200" = "$forumStatus" ]then echo "forum ok";else sendMail "ssonline alert-tomcat-forum status warn!";fiwget http://localhost:10080/v1/monitor/jvm/status.jsp -O status;v1Status=` cat ./status | tr 'n' ' ' | tr 'r' ' ' | awk 'gsub(/^ *| *$/,"")'`;echo "v1 status";echo $v1Status;if [ -n v1Status -a "200" = "$v1Status" ]then echo "v1 ok";else sendMail "ssonline alert-tomcat-v1 status warn!";fiwget http://localhost/monitor/status_db.jsp -O status;dbStatus=` cat ./status | tr 'n' ' ' | tr 'r' ' ' | awk 'gsub(/^ *| *$/,"")'`;echo "mysql status";echo $dbStatus;if [ -n v1Status -a "200" = "$dbStatus" ]then echo "mysql ok";else sendMail "ssonline alert-mysql status warn!";fi#端口异常监控ports=(80 8080 9080 10080 25 110 11211 3306);for port in ${ports[*]}; do status=`netstat -ntlp | grep :$port`; echo "check port " $port " " $status; if [ -z "$status" ]then echo "ssonline alert-port$port warn!"; sendMail "ssonline alert-port$port warn!"; fidone;#cpu 磁盘 内存异常eth="eth0 eth1 "function get_eth_info(){ name=$1 cat /proc/net/dev | awk -F ':' '{if(NR>2)print $1" "$2" "$9" "$10}' | awk '{print$1" "$2" "$3" "$5}' | while read line do# echo $line data=`echo $name" "$line | awk '{if($1==$2)print $3" "$4" "$5}'` if ! [ -z "$data" ] then echo $data fi done}function sum_eth(){ rm -fr 12321.txt for name in $eth do ret=`get_eth_info $name` #echo $ret | awk '{bps+=$0;pps+$1;dps+=$2;print bps" "pps" "dps}' echo $ret >> 12321.txt done cat 12321.txt | awk 'BEGIN{bps=0;pps=0;dps=0}{bps+=$1;pps+=$2;dps+=$3}END{print bps" "pps" "dps}'}flow=`sum_eth`disk=`df | awk 'BEGIN{total=0;avl=0;used=0;}NR > 1{total+=$2;used+=$3;avl+=$4;}END{printf"%d", avl/total*100}'`#top -b -n 1 | grep -w Mem | awk '{print"tot_mem: "$2" used_mem: " $4 " free_mem: "$6" left:"$6/$2"%"}'mem=`top -b -n 1 | grep -w Mem | awk '{printf"%d",$6/$2*100}'` #top -b -n 1 | grep -w Cpu | awk '{print"cpu: "$5}' | awk -F '%' {print$1} cpu=`top -b -n 1 | grep -w Cpu | awk '{print$5}' | awk -F '%' '{printf"%d",$1}'` tm=`date +%s` if ! [ -f "flow.txt" ] then echo $tm" "$flow >flow.txt sleep 1 tm=`date +%s` flow=`sum_eth` fi old_flow=`cat flow.txt` new_flow=`echo $tm $flow` echo $new_flow >flow.txt #echo $old_flow #echo $new_flow final_flow=`echo $old_flow $new_flow | awk '{dif_tm=$5-$1;dif_byte=$6-$2;dif_pkt=$7-$3;dif_dpkt=$8-$4;printf"%d %d %d",dif_byte*8/dif_tm, dif_pkt/dif_tm, dif_dpkt/dif_tm}'` #final_flow=`echo $old_flow $new_flow | awk '{print$0}'`echo "cpu "$cpuecho "disk "$diskecho "mem "$memecho "nic "$final_flowif [ $cpu -le 30 ]then echo "ssonline alert-cpu unused $cpu warn!"; sendMail "ssonline alert-cpu unused $cpu warn!"; fiif [ $disk -le 30 ]then echo "ssonline alert-disk unused $disk warn!"; sendMail "ssonline alert-disk unused $disk warn!"; fi#if [ $mem -le 0 ]#then# echo "ssonline alert-mem unused $mem warn!";# sendMail "ssonline alert-mem unused $mem warn!"; #fi 如果你要纯走java 单机 可以考虑1、jmx获取服务器的一些信息 如堆内存等 可以使用sigar收集单机信息2、数据库 可以查看通过数据库api获取连接数等3、发email可以通过javamail4、发短信需要第三方提供 这个之前公司 有自己的一套东西如果监控多服务器snmp等第三方现成的munin(我目前有这个 只监控)nagios(这个比较强大 能报警)
解决方案四:
这里的ssh是什么意思?你是要监控数据库的什么呢?你是要监控数据库发现状态异常或指标超标即将有状态异常时,想IT运维管理员发送警告(邮件、短信、消息)吗?