最近需要做一个对应用访问是否正常,用一天时间简单做了一个web监控系统,具体一下,分析给大家。
一、数据库设计
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 monitorURL() throws Exception{
List<PageData> list=this.query(Common.MONITOR_TYPE_URL);
int size=StringUtil.getListSize(list);
for (int i = 0; i < size; i++) {
PageData pd=list.get(i);
String url=pd.getString("msql");
String cont=pd.getString("mdes");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse response = httpclient.execute(httpPost);
try {
if (response.getStatusLine().getStatusCode() == 200) {
//如果状态正常,什么操作都不做
}else{
//如果不正常就发短信
this.sendMsg(cont);
}
}catch (Exception e){
this.sendMsg(cont);
e.printStackTrace();
}
}
}
三、定时监控实现
<?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.zrsc.table.common.TableQuartz"></bean><bean id="monitorURLJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="tableQuartz"></property>
<property name="targetMethod" value="monitorURL"></property><!-- 配置调度指定类中的指定的方法 -->
<property name="concurrent" value="false"></property>
</bean><bean id="monitorURLTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="monitorURLJob" />
<property name="cronExpression" value="0 0 */1 * * ?" />
</bean><bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="monitorURLTrigger"/>
</list>
</property>
<property name="autoStartup" value="true"/>
</bean>
</beans>