监控openfire数据

策划和开发需求openfire数据

(1)使用jvm暴露接口进行监控:
jvm接口介绍:http://blog.csdn.net/linghunhong/article/details/6438409

在ServerStarter.java中start方法最后添加mbean注册:

[java] view plaincopy

  1. <span style="white-space:pre">    </span>/** 

  2.             * 注册jmx 

  3.             *  

  4.             */  

  5.            MBeanServer ms =  ManagementFactory.getPlatformMBeanServer();  

  6.         ObjectName ofJmxName = new ObjectName("******:type=***");  

  7.            Class jmxClass = loader.loadClass(  

  8.                    "org.jmx.你的mbean实现");  

  9.            ms.registerMBean(jmxClass.newInstance(),ofJmxName);  

  10.            //注册结束  


你的mbean注册中可以实现对已有统计的接口,openfire统计相关都在StatisticsManager.java中

然后可以使用jdk提供的jconsole.exe监控查看

(2)数据库连接监控:

安装Load Statistic插件

(3)数据包聊天、登陆用户监控:

安装Monitoring Service插件

(4)

要求:统计消息转发的最长时长、最短时长、平均时长
统计IQ最长时长、最短时长、平均时长

方案:

1、利用openfire提供的PacketInterceptor,在interceptPacket方法中实现对IQ和Message的日志打印(具体打印内容自定义)。可以每个一段时间对日志内容进行分析

2、利用Monitoring Service提供的接口进行仿照开发

制作插件,主体部分逻辑如下:

[java] view plaincopy

  1. import java.util.concurrent.ConcurrentHashMap;  

  2. import java.util.concurrent.atomic.AtomicInteger;  

  3. import java.util.concurrent.atomic.AtomicLong;  

  4.   

  5. import org.jivesoftware.openfire.interceptor.InterceptorManager;  

  6. import org.jivesoftware.openfire.interceptor.PacketInterceptor;  

  7. import org.jivesoftware.openfire.session.Session;  

  8. import org.jivesoftware.openfire.stats.Statistic;  

  9. import org.jivesoftware.openfire.stats.StatisticsManager;  

  10. import org.jivesoftware.openfire.stats.i18nStatistic;  

  11. import org.picocontainer.Startable;  

  12. import org.xmpp.packet.IQ;  

  13. import org.xmpp.packet.Message;  

  14. import org.xmpp.packet.Packet;  

  15.   

  16. /** 

  17.  * Creates and manages Enteprise-specific statistics, specifically: <ul> 

  18.  *      message 转发最长时间、最短时间、平均时间、所有时间、数目 

  19.  *       

  20.  * </ul> 

  21.  * 

  22.  * @author  

  23.  */  

  24. public class StatisticsModule implements Startable {  

  25.     //message 统计相关key  

  26.     public static final String MESSAGE_COUNT_KEY = "m_count";  

  27.     public static final String MESSAGE_LONGEST_TIME_KEY = "m_longest_time";  

  28.     public static final String MESSAGE_SHORTEST_TIME_KEY = "m_shortest_time";  

  29.     public static final String MESSAGE_ALL_TIME_KEY = "m_all_time";  

  30.     public static final String MESSAGE_AVG_TIME_KEY = "m_avg_time";  

  31.      

  32.     //message统计存储  

  33.     private ConcurrentHashMap<String,AtomicLong> m_timeHash = new ConcurrentHashMap<String,AtomicLong>() ;  

  34.     private AtomicInteger message_Count = new AtomicInteger();  

  35.     private AtomicLong messgae_longestTime = new AtomicLong();  

  36.     private AtomicLong message_shortestTime = new AtomicLong(100000l);  

  37.     private AtomicLong message_allTime = new AtomicLong();  

  38.       

  39.       

  40.       

  41.     private StatisticsManager statisticsManager;  

  42.     private PacketInterceptor packetInterceptor;  

  43.       

  44.       

  45.       

  46. //    private long   

  47.     public void start() {  

  48.         // Retrieve instance of StatisticsManager  

  49.         statisticsManager = StatisticsManager.getInstance();  

  50.   

  51.         // Register a packet listener so that we can track packet traffic.  

  52.         packetInterceptor = new PacketInterceptor() {  

  53.             public void interceptPacket(Packet packet, Session session, boolean incoming,  

  54.                                         boolean processed)  

  55.             {  

  56.                 if(null==packet.getID()){  

  57.                     //没有id的不能统计  

  58.                     return;  

  59.                 }  

  60.                 // Only track processed packets so that we don't count them twice.  

  61.                 if (incoming&&!processed) {  

  62.                     if (packet instanceof Message) {  

  63.                         m_timeHash.put(packet.getID(),new AtomicLong(System.currentTimeMillis()));  

  64.                     }else if(packet instanceof IQ){  

  65.                         iq_timeHash.put(packet.getID(), new AtomicLong(System.currentTimeMillis()));  

  66.                     }  

  67.                       

  68.                 }else if(incoming&&processed){  

  69.                     if (packet instanceof Message) {  

  70.                         AtomicLong time = m_timeHash.get(packet.getID());  

  71.                         if(time!=null){  

  72.                             //end-start  

  73.                             //获取longest时间和shortest时间,比较,如果需要的话保存  

  74.                             //获取all,进行加法操作  

  75.                             //获取count进行自增1操作  

  76.                             //删除这个map  

  77.                             long t = System.currentTimeMillis()-time.longValue();  

  78.                             if(messgae_longestTime.longValue()<t){  

  79.                                 messgae_longestTime.set(t);  

  80.                             }  

  81.                             if(message_shortestTime.longValue()>t){  

  82.                                 message_shortestTime.set(t);  

  83.                             }  

  84.                             message_allTime.getAndAdd(t);  

  85.                             message_Count.incrementAndGet();  

  86.                             m_timeHash.remove(packet.getID());  

  87.                         }  

  88.                     }  

  89.                 }  

  90.             }  

  91.         };  

  92.         InterceptorManager.getInstance().addInterceptor(packetInterceptor);  

  93.   

  94.         // Register all statistics.  

  95.         addPacketNumberStatistic();  

  96.         addPacketAllTimeStatistic();  

  97.         addPacketLongestTimeStatistic();  

  98.         addPacketShortestTimeStatistic();  

  99.         addPacketAvgTimeStatistic();  

  100.           

  101.         

  102.     }  

  103.   

  104.     /** 

  105.      * Remove all registered statistics. 

  106.      */  

  107.     public void stop() {  

  108.   

  109.   

  110.         // Remove Statistic  

  111.         statisticsManager.removeStatistic(MESSAGE_COUNT_KEY);  

  112.         statisticsManager.removeStatistic(MESSAGE_LONGEST_TIME_KEY);  

  113.         statisticsManager.removeStatistic(MESSAGE_SHORTEST_TIME_KEY);  

  114.         statisticsManager.removeStatistic(MESSAGE_ALL_TIME_KEY);  

  115.         statisticsManager.removeStatistic(MESSAGE_AVG_TIME_KEY);  

  116.         

  117.   

  118.         statisticsManager = null;  

  119.   

  120.         // Remove the packet listener.  

  121.         InterceptorManager.getInstance().removeInterceptor(packetInterceptor);  

  122.         packetInterceptor = null;  

  123.         message_Count = null;  

  124.         messgae_longestTime = null;  

  125.         message_shortestTime = null;  

  126.         message_allTime = null;  

  127.         m_timeHash = null;  

  128.           

  129.         

  130.     }  

  131.   

  132.      

  133.     /** 

  134.      * Tracks the total number of message 

  135.      */  

  136.     private void addPacketAvgTimeStatistic() {  

  137.         // Register a statistic.  

  138.         Statistic packetTrafficStatistic = new i18nStatistic(MESSAGE_AVG_TIME_KEY, null, Statistic.Type.count) {  

  139.             public double sample() {  

  140.                 if(message_Count.longValue()==0){  

  141.                     return 0;  

  142.                 }  

  143.                 return message_allTime.longValue()/message_Count.longValue();  

  144.             }  

  145.   

  146.             public boolean isPartialSample() {  

  147.                 return true;  

  148.             }  

  149.         };  

  150.         statisticsManager.addStatistic(MESSAGE_AVG_TIME_KEY, packetTrafficStatistic);  

  151.     }  

  152.   

  153.     /** 

  154.      * Tracks the total number of message 

  155.      */  

  156.     private void addPacketNumberStatistic() {  

  157.         // Register a statistic.  

  158.         Statistic packetTrafficStatistic = new i18nStatistic(MESSAGE_COUNT_KEY, null, Statistic.Type.count) {  

  159.             public double sample() {  

  160.                 return message_Count.get();  

  161.             }  

  162.   

  163.             public boolean isPartialSample() {  

  164.                 return true;  

  165.             }  

  166.         };  

  167.         statisticsManager.addStatistic(MESSAGE_COUNT_KEY, packetTrafficStatistic);  

  168.     }  

  169.     /** 

  170.      * Tracks the total time of message 

  171.      */  

  172.     private void addPacketAllTimeStatistic() {  

  173.         // Register a statistic.  

  174.         Statistic packetTrafficStatistic = new i18nStatistic(MESSAGE_ALL_TIME_KEY, null, Statistic.Type.count) {  

  175.             public double sample() {  

  176.                 return message_allTime.get();  

  177.             }  

  178.   

  179.             public boolean isPartialSample() {  

  180.                 return true;  

  181.             }  

  182.         };  

  183.         statisticsManager.addStatistic(MESSAGE_ALL_TIME_KEY, packetTrafficStatistic);  

  184.     }  

  185.     /** 

  186.      * Tracks the longestTime of message 

  187.      */  

  188.     private void addPacketLongestTimeStatistic() {  

  189.         // Register a statistic.  

  190.         Statistic packetTrafficStatistic = new i18nStatistic(MESSAGE_LONGEST_TIME_KEY, null, Statistic.Type.count) {  

  191.             public double sample() {  

  192.                 return messgae_longestTime.get();  

  193.             }  

  194.   

  195.             public boolean isPartialSample() {  

  196.                 return true;  

  197.             }  

  198.         };  

  199.         statisticsManager.addStatistic(MESSAGE_LONGEST_TIME_KEY, packetTrafficStatistic);  

  200.     }  

  201.       

  202.     /** 

  203.      * Tracks the shortestTime of message 

  204.      */  

  205.     private void addPacketShortestTimeStatistic() {  

  206.         // Register a statistic.  

  207.         Statistic packetTrafficStatistic = new i18nStatistic(MESSAGE_SHORTEST_TIME_KEY, null, Statistic.Type.count) {  

  208.             public double sample() {  

  209.                 return message_shortestTime.get();  

  210.             }  

  211.   

  212.             public boolean isPartialSample() {  

  213.                 return true;  

  214.             }  

  215.         };  

  216.         statisticsManager.addStatistic(MESSAGE_SHORTEST_TIME_KEY, packetTrafficStatistic);  

  217.     }  

  218.       

  219.       

  220. }  


这样,以上信息就能在jmx接口中暴露出来了


转载于:https://my.oschina.net/vdroid/blog/311821

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值