posts - 156,  comments - 601,  trackbacks - 0
          公告
          <2008年3月>
          2425262728291
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿(45)

          隨筆分類(145)

          隨筆檔案(110)

          文章檔案(1)

          友情鏈接

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 680009
          • 排名 - 68

          最新評論

          閱讀排行榜

          評論排行榜

          最近看到一位同事正在開發(fā)一個監(jiān)控軟件,要求就是通過針對服務器現(xiàn)有的一些接口,通過這些接口返回的數(shù)據(jù)進行分析,如果監(jiān)控的值到達預先設定的范圍則通過短信的方式發(fā)送給管理員。

          從整個開發(fā)的功能上來看是一個比較單一也很明確的功能,所開發(fā)的系統(tǒng)對所其所監(jiān)控的軟件的依賴性也非常大,主要是監(jiān)控的數(shù)據(jù)分析行為和監(jiān)控信息的服務報警行為這塊。既然這兩塊很難做成一個通用的功能模塊,那就搭建一個監(jiān)控平臺,可以讓這些功能模塊通過組件的方式自由的注冊和銷毀。

          所有我構思了這個監(jiān)控平臺,它對外有三個接口,分別是監(jiān)控接口,報警接口和監(jiān)控消息監(jiān)控接口。由平臺統(tǒng)一管理這些組件的生命周期,每個組件都過單獨的線程運行。提供一個核心組件CoreComponent調(diào)度所有監(jiān)控數(shù)據(jù)的流轉(zhuǎn)。平臺本身還使用基于jmx管理服務技術提供對所有當前使用的組件運行情況的監(jiān)控,也包括動態(tài)的啟動和停止組件的運行狀態(tài)。

          下載地址 
          二進制程序
          第三方類庫下載,第三方類庫下載2 放到lib目錄下。
          api-docs 
          源代碼

          下面是部分設計圖:

           

          AlertComponent設計圖



           

          SpyComponent設計圖:


          MessageAlertChannelActiveAwareComponent設計圖


          下面我利用該平臺開發(fā)一個監(jiān)控ActiveMQ狀態(tài)的組件ActiveMQJmxSpyComponent,該組件實現(xiàn)對AMQ運行狀態(tài)的監(jiān)控(監(jiān)聽失敗或失敗后重新連接成功)。可以通過指定Queue名稱列表來指定要監(jiān)控Queue隊列的消費者是否為0(通常表示對方可能因為網(wǎng)絡或服務中斷而失去監(jiān)控)或是隊列消息都由0變?yōu)榇笥?表示消費者重新監(jiān)聽上服務。

           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;  


          下面是一個報警組件的實現(xiàn):只是簡單的把監(jiān)控消息打印在屏幕上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,現(xiàn)在ActiveMQJmxSpyComponent監(jiān)控到的消息能會被PrintScreenAlertComponent打印到屏幕上。
          現(xiàn)在啟動程序,我們看到ActiveMQJmxSpyComponent和PrintScreenAlertComponent組件已經(jīng)啟動了。


          使用Jconsole進行監(jiān)控





          如果此時需要建立一個消息報警的規(guī)則,只要實現(xiàn)以下接口,并注入到CoreComponent的alertRule屬性中即可。

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

          應用這個平臺開發(fā)監(jiān)控的組件就這么簡單。

           備注:因為開發(fā)時間比較緊,如果有什么Bug也希望大家反饋給我,我會改進。

          下載地址 
          二進制程序
          第三方類庫下載  第三方類庫下載2  放到lib目錄下。
          api-docs 
          源代碼

           Good luck!

          Yours Matthew!

           

          posted on 2008-03-12 13:41 x.matthew 閱讀(2207) 評論(7)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 林芝县| 烟台市| 昌都县| 安义县| 富阳市| 凤冈县| 长宁区| 沾化县| 丘北县| 新泰市| 瓦房店市| 谢通门县| 新竹县| 留坝县| 南宫市| 黔东| 西乡县| 周至县| 台北市| 贡嘎县| 惠州市| 新安县| 洪泽县| 绥芬河市| 安丘市| 乌拉特前旗| 阿尔山市| 桓仁| 莱芜市| 辽宁省| 鹤庆县| 贵州省| 如东县| 锦屏县| 古丈县| 龙陵县| 墨江| 吴桥县| 宜兴市| 修武县| 沐川县|