posts - 156,  comments - 601,  trackbacks - 0

最近看到一位同事正在开发一个监控软件,要求就是通过针对服务器现有的一些接口,通过这些接口返回的数据进行分析,如果监控的值到达预先设定的范围则通过短信的方式发送给管理员。

从整个开发的功能上来看是一个比较单一也很明确的功能,所开发的系统对所其所监控的软件的依赖性也非常大,主要是监控的数据分析行为和监控信息的服务报警行为这块。既然这两块很难做成一个通用的功能模块,那就搭建一个监控平台,可以让这些功能模块通过组件的方式自由的注册和销毁。

所有我构思了这个监控平台,它对外有三个接口,分别是监控接口,报警接口和监控消息监控接口。由平台统一管理这些组件的生命周期,每个组件都过单独的线程运行。提供一个核心组件CoreComponent调度所有监控数据的流转。平台本身还使用基于jmx管理服务技术提供对所有当前使用的组件运行情况的监控,也包括动态的启动和停止组件的运行状态。

下载地址 
二进制程序
第三方类库下载,第三方类库下载2 放到lib目录下。
api-docs 
源代码

下面是部分设计图:

 

AlertComponent设计图



 

SpyComponent设计图:


MessageAlertChannelActiveAwareComponent设计图


下面我利用该平台开发一个监控ActiveMQ状态的组件ActiveMQJmxSpyComponent,该组件实现对AMQ运行状态的监控(监听失败或失败后重新连接成功)。可以通过指定Queue名称列表来指定要监控Queue队列的消费者是否为0(通常表示对方可能因为网络或服务中断而失去监控)或是队列消息都由0变为大于0表示消费者重新监听上服务。

 1public class ActiveMQJmxSpyComponent extends AbstractSpyComponent {   
 2    /**  
 3     * Logger for this class  
 4     */
  
 5    private static final Logger LOGGER = Logger.getLogger(ActiveMQJmxSpyComponent.class);   
 6    //AMQ jmx serverUrl to spy    
 7    private String serverUrl;   
 8    //detect interval(unit is ms)   
 9    private int detectInterval = 5000;   
10    //the Queue name list to spy   
11    private Set<String> destinationNamesToWatch;   
12    // if queue's consumer suspends after then certain time then to notify. default is 3 minutes   
13    private int queueSuspendNotifyTime = 3*60*1000;  


下面是一个报警组件的实现:只是简单的把监控消息打印在屏幕上PrintScreenAlertComponent

 1public class PrintScreenAlertComponent extends AbstractAlertComponent {   
 2  
 3    /* (non-Javadoc)  
 4     * @see org.xmatthew.spy2servers.core.Component#getName()  
 5     */
  
 6    public String getName() {   
 7        return "PrintScreenAlertComponent";   
 8    }
   
 9  
10    /* (non-Javadoc)  
11     * @see org.xmatthew.spy2servers.core.Component#startup()  
12     */
  
13    public void startup() {   
14        setStatusRun();   
15  
16    }
   
17  
18    /* (non-Javadoc)  
19     * @see org.xmatthew.spy2servers.core.Component#stop()  
20     */
  
21    public void stop() {   
22        setStatusStop();   
23  
24    }
   
25  
26    @Override  
27    protected void onAlert(Message message) {   
28        System.out.println(message);   
29           
30    }
   
31  
32}
  
33


下面该组件的注册。${CUR_PATH}/conf/spy2servers.xml

 1<?xml version="1.0" encoding="UTF-8"?>  
 2<beans xmlns="http://www.springframework.org/schema/beans"  
 3    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 4    xmlns:aop="http://www.springframework.org/schema/aop"  
 5    xmlns:tx="http://www.springframework.org/schema/tx"  
 6    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
 7           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd   
 8           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
 9  
10    <bean class="org.xmatthew.spy2servers.core.CoreComponent"></bean>  
11    <bean class="org.xmatthew.spy2servers.jmx.JmxServiceComponent"></bean>  
12       
13    <bean class="org.xmatthew.spy2servers.component.alert.PrintScreenAlertComponent"></bean>  
14       
15    <bean class="org.xmatthew.spy2servers.component.spy.jmx.ActiveMQJmxSpyComponent">  
16        <property name="serverUrl" value="service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi"></property>  
17        <property name="destinationNamesToWatch">  
18          <set>  
19            <value>Matthew.Queue</value>  
20            <value>Rocket.Queue</value>  
21          </set>             
22        </property>  
23        <property name="queueSuspendNotifyTime" value="50000"></property>  
24    </bean>  
25       
26</beans>  
27


ok,现在ActiveMQJmxSpyComponent监控到的消息能会被PrintScreenAlertComponent打印到屏幕上。
现在启动程序,我们看到ActiveMQJmxSpyComponent和PrintScreenAlertComponent组件已经启动了。


使用Jconsole进行监控





如果此时需要建立一个消息报警的规则,只要实现以下接口,并注入到CoreComponent的alertRule属性中即可。

1public interface AlertRule {   
2  
3    boolean isAlertAllow(MessageAlertChannel channel);   
4}
  

应用这个平台开发监控的组件就这么简单。

 备注:因为开发时间比较紧,如果有什么Bug也希望大家反馈给我,我会改进。

下载地址 
二进制程序
第三方类库下载  第三方类库下载2  放到lib目录下。
api-docs 
源代码

 Good luck!

Yours Matthew!

 

posted on 2008-03-12 13:41 x.matthew 阅读(2185) 评论(7)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: