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

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

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

下载地址

二进制程序第三方类库下载,第三方类库下载2 放到lib目录下。

api-docs

源代码

下面是部分设计图:

java 硬件系统监控 java监控平台_spring

AlertComponent设计图

java 硬件系统监控 java监控平台_xml_02

SpyComponent设计图:

java 硬件系统监控 java监控平台_java 硬件系统监控_03

MessageAlertChannelActiveAwareComponent设计图

java 硬件系统监控 java监控平台_spring_04

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

1

publicclassActiveMQJmxSpyComponentextendsAbstractSpyComponent
{2
/** *//**3
     * Logger for this class4
*/5
privatestaticfinalLogger LOGGER=Logger.getLogger(ActiveMQJmxSpyComponent.class);6
//AMQ jmx serverUrl to spy7
privateString serverUrl;8
//detect interval(unit is ms)9
privateintdetectInterval=5000;10
//the Queue name list to spy11
privateSetdestinationNamesToWatch;12
//if queue's consumer suspends after then certain time then to notify. default is 3 minutes13
privateintqueueSuspendNotifyTime=3*60*1000;
下面是一个报警组件的实现:只是简单的把监控消息打印在屏幕上PrintScreenAlertComponent
1
publicclassPrintScreenAlertComponentextendsAbstractAlertComponent
{2
3
/**//*(non-Javadoc)4
     * @see org.xmatthew.spy2servers.core.Component#getName()5
*/6
publicString getName()
{7
return"PrintScreenAlertComponent";8
    }9
10
/**//*(non-Javadoc)11
     * @see org.xmatthew.spy2servers.core.Component#startup()12
*/13
publicvoidstartup()
{14
        setStatusRun();15
16
    }17
18
/**//*(non-Javadoc)19
     * @see org.xmatthew.spy2servers.core.Component#stop()20
*/21
publicvoidstop()
{22
        setStatusStop();23
24
    }25
26
    @Override27
protectedvoidonAlert(Message message)
{28
        System.out.println(message);29
30
    }31
32
}33
下面该组件的注册。${CUR_PATH}/conf/spy2servers.xml
1
<?xml  versinotallow="1.0" encoding="UTF-8"?>2
    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:schemaLocatinotallow="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd7
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd8
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">9
10
11
12
13
14
15
16
17
18
19
Matthew.Queue20
Rocket.Queue21
22
23
24
25
26
27

ok,现在ActiveMQJmxSpyComponent监控到的消息能会被PrintScreenAlertComponent打印到屏幕上。

现在启动程序,我们看到ActiveMQJmxSpyComponent和PrintScreenAlertComponent组件已经启动了。

java 硬件系统监控 java监控平台_spring_05

使用Jconsole进行监控

java 硬件系统监控 java监控平台_功能模块_06

java 硬件系统监控 java监控平台_功能模块_06

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

1
publicinterfaceAlertRule
{2
3
booleanisAlertAllow(MessageAlertChannel channel);4
}

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

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