最近看到一位同事正在开发一个监控软件,要求就是通过针对服务器现有的一些接口,通过这些接口返回的数据进行分析,如果监控的值到达预先设定的范围则通过短信的方式发送给管理员。
从整个开发的功能上来看是一个比较单一也很明确的功能,所开发的系统对所其所监控的软件的依赖性也非常大,主要是监控的数据分析行为和监控信息的服务报警行为这块。既然这两块很难做成一个通用的功能模块,那就搭建一个监控平台,可以让这些功能模块通过组件的方式自由的注册和销毁。
所有我构思了这个监控平台,它对外有三个接口,分别是监控接口,报警接口和监控消息监控接口。由平台统一管理这些组件的生命周期,每个组件都过单独的线程运行。提供一个核心组件CoreComponent调度所有监控数据的流转。平台本身还使用基于jmx管理服务技术提供对所有当前使用的组件运行情况的监控,也包括动态的启动和停止组件的运行状态。
附件中程序和部分的开发说明文档(由只用了几个晚上开发出来,文档不够完整,以后慢慢补上)
从整个开发的功能上来看是一个比较单一也很明确的功能,所开发的系统对所其所监控的软件的依赖性也非常大,主要是监控的数据分析行为和监控信息的服务报警行为这块。既然这两块很难做成一个通用的功能模块,那就搭建一个监控平台,可以让这些功能模块通过组件的方式自由的注册和销毁。
所有我构思了这个监控平台,它对外有三个接口,分别是监控接口,报警接口和监控消息监控接口。由平台统一管理这些组件的生命周期,每个组件都过单独的线程运行。提供一个核心组件CoreComponent调度所有监控数据的流转。平台本身还使用基于jmx管理服务技术提供对所有当前使用的组件运行情况的监控,也包括动态的启动和停止组件的运行状态。
附件中程序和部分的开发说明文档(由只用了几个晚上开发出来,文档不够完整,以后慢慢补上)
下面我利用该平台开发一个监控ActiveMQ状态的组件ActiveMQJmxSpyComponent,该组件实现对AMQ运行状态的监控(监听失败或失败后重新连接成功)。可以通过指定Queue名称列表来指定要监控Queue队列的消费者是否为0(通常表示对方可能因为网络或服务中断而失去监控)或是队列消息都由0变为大于0表示消费者重新监听上服务。
public class ActiveMQJmxSpyComponent extends AbstractSpyComponent {
/**
* Logger for this class
*/
private static final Logger LOGGER = Logger.getLogger(ActiveMQJmxSpyComponent.class);
//AMQ jmx serverUrl to spy
private String serverUrl;
//detect interval(unit is ms)
private int detectInterval = 5000;
//the Queue name list to spy
private Set<String> destinationNamesToWatch;
// if queue's consumer suspends after then certain time then to notify. default is 3 minutes
private int queueSuspendNotifyTime = 3*60*1000;
下面是一个报警组件的实现:只是简单的把监控消息打印在屏幕上PrintScreenAlertComponent
public class PrintScreenAlertComponent extends AbstractAlertComponent {
/* (non-Javadoc)
* @see org.xmatthew.spy2servers.core.Component#getName()
*/
public String getName() {
return "PrintScreenAlertComponent";
}
/* (non-Javadoc)
* @see org.xmatthew.spy2servers.core.Component#startup()
*/
public void startup() {
setStatusRun();
}
/* (non-Javadoc)
* @see org.xmatthew.spy2servers.core.Component#stop()
*/
public void stop() {
setStatusStop();
}
@Override
protected void onAlert(Message message) {
System.out.println(message);
}
}
下面该组件的注册。${CUR_PATH}/conf/spy2servers.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean class="org.xmatthew.spy2servers.core.CoreComponent"></bean> <bean class="org.xmatthew.spy2servers.jmx.JmxServiceComponent"></bean> <bean class="org.xmatthew.spy2servers.component.alert.PrintScreenAlertComponent"></bean> <bean class="org.xmatthew.spy2servers.component.spy.jmx.ActiveMQJmxSpyComponent"> <property name="serverUrl" value="service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi"></property> <property name="destinationNamesToWatch"> <set> <value>Matthew.Queue</value> <value>Rocket.Queue</value> </set> </property> <property name="queueSuspendNotifyTime" value="50000"></property> </bean> </beans>
ok,现在ActiveMQJmxSpyComponent监控到的消息能会被PrintScreenAlertComponent打印到屏幕上。
如果此时需要建立一个消息报警的规则,只要实现以下接口,并注入到CoreComponent的alertRule属性中即可。
public interface AlertRule {
boolean isAlertAllow(MessageAlertChannel channel);
}
通过jconsole大家就可以看到那几个组件已经在正常运行了,如果本地没有运行MQ,运行时会提示ActiveMQJmxSpyComponent监控失败,在jmx管理上会收到条AMQ服务未启动的报警信息。
应用这个平台开发监控的组件就这么简单。
备注:因为开发时间比较紧,如果有什么Bug也希望大家反馈给我,我会改进。
Good luck!
Yours Matthew!