数据库数据进行定时监控实现,包含数据库脚本和核心Java源码

最近需要做一个定时监控数据库同步结果,用一天时间简单做了一个web监控系统,具体一下,分析给大家。

一、数据库设计

table_monitor为监控原始数据

table_monitor_result为监控结果数据

具体如下:

CREATE TABLE `table_monitor` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `msql` varchar(255) DEFAULT NULL,
  `mcompare` varchar(50) DEFAULT NULL,
  `mnum` decimal(10,2) DEFAULT NULL,
  `mdes` varchar(50) DEFAULT NULL,
  `type` int(20) DEFAULT '1' COMMENT '1=sql监控,2=网址监控',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=54 DEFAULT CHARSET=utf8;

CREATE TABLE `table_monitor_result` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `createTime` datetime DEFAULT NULL COMMENT '处理时间',
  `mSQL` varchar(255) DEFAULT NULL COMMENT '监控SQL或地址',
  `mResult` text DEFAULT NULL COMMENT '监控结果',
  `doResult` varchar(50) DEFAULT NULL COMMENT '处理结果',
  `doMsg` text DEFAULT NULL COMMENT '处理备注',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

二、监控数据比对实现

//检测报表系统中数据同步是否正常
public void monitorOrderNum() {
   long startTime=System.currentTimeMillis();
   try {
      String COM_EQUAL = "=";
      String COM_GREATER_THAN = ">";
      String COM_LESS_THAN = "<";
      List<PageData> list = this.query(Common.MONITOR_TYPE_TABLEDB);
      int size = StringUtil.getListSize(list);
      for (int i = 0; i < size; i++) {
         //获取监控SQL
         PageData pd = list.get(i);
               String id = (String) pd.get("mid");
         String msql = pd.getString("msql");
         String mcompare = pd.getString("mcompare");
         BigDecimal mnum = (BigDecimal) pd.get("mnum");
         String mdes = pd.getString("mdes");
         mdes=id+mdes;
         //检查
         PageData queryResultPD = this.executeSelectSQL(msql);
         Double dbnum = Double.parseDouble(queryResultPD.get("count") + "");
         Double expectnum = mnum.doubleValue();
         //有问题的发送短信
         if (COM_EQUAL.equals(mcompare) ? Math.abs(dbnum - expectnum) > 0.01 : false) {
            this.monitorFailure(msql, mdes + "数据不准!库值为" + dbnum + ",预期值为" + expectnum);
         } else if (COM_GREATER_THAN.equals(mcompare) ? getScale(dbnum - expectnum) < 0.00 : false) {
            this.monitorFailure(msql, mdes + "数据不准!库值为" + dbnum + ",预期值为" + expectnum);
         } else if (COM_LESS_THAN.equals(mcompare) ? getScale(dbnum - expectnum) > 0.00 : false) {
            this.monitorFailure(msql, mdes + "数据不准!库值为" + dbnum + ",预期值为" + expectnum);
         }
      }
   }catch (Exception e){
      this.monitorFailure("", Tools.getExceptionAllinformation(e),false);
           this.sendMsg("报表数据检测失败,失败原因"+e.getMessage());
   }
   this.sendMsg("报表数据检测完毕,总用时"+(System.currentTimeMillis()-startTime)+"毫秒");
}

三、定时监控实现

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
       xmlns:amq="http://activemq.apache.org/schema/core" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
         http://activemq.apache.org/schema/core 
         http://activemq.apache.org/schema/core/activemq-core.xsd">  
        <!-- 添加调度的任务bean 配置对应的class-->
        <bean id="tableQuartz" class="com.table.common.TableQuartz"></bean>
       
        <bean id="monitorJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
           <property name="targetObject" ref="tableQuartz"></property>
           <property name="targetMethod" value="monitor"></property><!-- 配置调度指定类中的指定的方法 -->
           <property name="concurrent" value="false"></property>
        </bean>
        <bean id="monitorTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
          <property name="jobDetail" ref="monitorJob" />
             <property name="cronExpression" value="0 0 7 * * ?" />
               <!--  每隔5秒执行一次:*/5 * * * * ?
                          每隔1分钟执行一次:0 */1 * * * ?
                          每天23点执行一次:0 0 23 * * ?
                          每天凌晨1点执行一次:0 0 1 * * ?
                          每月1号凌晨1点执行一次:0 0 1 1 * ?
                          每月最后一天23点执行一次:0 0 23 L * ?
                          每周星期天凌晨1点实行一次:0 0 1 ? * L
                          在26分、29分、33分执行一次:0 26,29,33 * * * ?
                          每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ?
              -->
           </bean>

          <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
               <list>
                  <ref bean="monitorTrigger"/>
               </list>
            </property>
            <property name="autoStartup" value="true"/>
          </bean>
   </beans>

抽空,我把源码整理一下,分享给大家。 

猜你喜欢

转载自blog.csdn.net/jlq_diligence/article/details/89513810