借鉴自http://blog.csdn.net/xjtuse2014/article/details/53968726

1.MoniterBandwidth模块:

 package net.floodlightcontroller.qos_test;

 import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import net.floodlightcontroller.core.IFloodlightProviderService;
import net.floodlightcontroller.core.internal.IOFSwitchService;
import net.floodlightcontroller.core.module.FloodlightModuleContext;
import net.floodlightcontroller.core.module.FloodlightModuleException;
import net.floodlightcontroller.core.module.IFloodlightModule;
import net.floodlightcontroller.core.module.IFloodlightService;
import net.floodlightcontroller.core.types.NodePortTuple;
import net.floodlightcontroller.statistics.IStatisticsService;
import net.floodlightcontroller.statistics.StatisticsCollector;
import net.floodlightcontroller.statistics.SwitchPortBandwidth;
import net.floodlightcontroller.threadpool.IThreadPoolService; /**
* 带宽获取模块
* @author cq
*
*/
public class MonitorBandwidth implements IFloodlightModule, IMonitorBandwidthService { //日志工具
private static final Logger log = LoggerFactory.getLogger(StatisticsCollector.class); //Floodlight最核心的service类,其他service类需要该类提供
protected static IFloodlightProviderService floodlightProvider; //链路数据分析模块,已经由Floodlight实现了,我们只需要调用一下就可以,然后对结果稍做加工,便于我们自己使用
protected static IStatisticsService statisticsService; //Floodllight实现的线程池,当然我们也可以使用Java自带的,但推荐使用这个
private static IThreadPoolService threadPoolService; //Future类,不明白的可以百度 Java现成future,其实C++11也有这个玩意了
private static ScheduledFuture<?> portBandwidthCollector; //交换机相关的service,通过这个服务,我们可以获取所有的交换机,即DataPath
private static IOFSwitchService switchService; //存放每条俩路的带宽使用情况
private static Map<NodePortTuple,SwitchPortBandwidth> bandwidth; //搜集数据的周期
private static final int portBandwidthInterval = 4; /**
* 获取带宽使用情况,计算带宽:
* switchPortBand.getBitsPerSecondRx().getValue()/(8*1024) + switchPortBand.getBitsPerSecondTx().getValue()/(8*1024)
*/
@Override
public Map<NodePortTuple, SwitchPortBandwidth> getBandwidthMap() {
bandwidth = statisticsService.getBandwidthConsumption();
Iterator<Entry<NodePortTuple, SwitchPortBandwidth>> iter = bandwidth.entrySet().iterator();
while(iter.hasNext()) {
Entry<NodePortTuple,SwitchPortBandwidth> entry = iter.next();
NodePortTuple tuple = entry.getKey();
SwitchPortBandwidth switchPortBand = entry.getValue();
System.out.print("节点id以及端口号:" + tuple.getNodeId()+","+tuple.getPortId().getPortNumber()+",");
System.out.println("端口带宽:" + switchPortBand.getBitsPerSecondRx().getValue()/(8*1024) + switchPortBand.getBitsPerSecondTx().getValue()/(8*1024));
}
return bandwidth;
} /**
* 告诉FL,我们添加了一个模块,提供了IMonitorBandwidthService
*/
@Override
public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Collection<Class<? extends IFloodlightService>> l = new ArrayList<Class<? extends IFloodlightService>>();
l.add(IMonitorBandwidthService.class);
return l;
} /**
* 我们前面声明了几个需要使用的service,在这里说明一下实现类
*/
@Override
public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Map<Class<? extends IFloodlightService>, IFloodlightService> m = new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
m.put(IMonitorBandwidthService.class, this);
return m;
} /**
* 告诉FL我们依赖哪些服务,以便于加载
*/
@Override
public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
Collection<Class<? extends IFloodlightService>> l = new ArrayList<Class<? extends IFloodlightService>>();
l.add(IFloodlightProviderService.class);
l.add(IStatisticsService.class);
l.add(IOFSwitchService.class);
l.add(IThreadPoolService.class);
return l;
} /**
* 初始化这些service
*/
@Override
public void init(FloodlightModuleContext context) throws FloodlightModuleException {
floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
statisticsService = context.getServiceImpl(IStatisticsService.class);
switchService = context.getServiceImpl(IOFSwitchService.class);
threadPoolService = context.getServiceImpl(IThreadPoolService.class);
} /**
* 启动
*/
@Override
public void startUp(FloodlightModuleContext context) throws FloodlightModuleException {
startCollectBandwidth();
}
//自定义的开始收集数据的方法,使用了线程池,定周期的执行
private synchronized void startCollectBandwidth(){
portBandwidthCollector = threadPoolService.getScheduledExecutor().scheduleAtFixedRate(new GetBandwidthThread(), portBandwidthInterval, portBandwidthInterval, TimeUnit.SECONDS);
log.warn("Statistics collection thread(s) started");
}
//自定义的线程类,在上面的方法中实例化,并被调用
private class GetBandwidthThread extends Thread implements Runnable {
private Map<NodePortTuple,SwitchPortBandwidth> bandwidth; public Map<NodePortTuple, SwitchPortBandwidth> getBandwidth() {
return bandwidth;
} @Override
public void run() {
System.out.println("GetBandwidthThread run()....");
bandwidth =getBandwidthMap();
System.out.println("bandwidth.size():"+bandwidth.size());
}
}
}

2.IMonitorBandwidthService类:

 package net.floodlightcontroller.qos_test;

 import java.util.Map;

 import net.floodlightcontroller.core.module.IFloodlightService;
import net.floodlightcontroller.core.types.NodePortTuple;
import net.floodlightcontroller.statistics.SwitchPortBandwidth; public interface IMonitorBandwidthService extends IFloodlightService{ //带宽使用情况
public Map<NodePortTuple, SwitchPortBandwidth> getBandwidthMap();
}

在ubuntu命令行,需执行

curl -X POST http://192.168.161.1:8080//wm/statistics/config/enable/json

才能获取到带宽,结果如下所示:

前提是执行了iperf h1 h2命令后才能看到,否则带宽一直显示是0

最新文章

  1. JQ常用代码
  2. 如何在Visual Studio中开发自己的代码生成器插件
  3. 利用TaskCompletionSource将EAP转换成TAP
  4. 夺命雷公狗—angularjs—3—表单验证的高级用法
  5. Unity C#和OC互相调用
  6. Linux命令-tar
  7. Flex博客
  8. 硬盘结构介绍--mbr及分区
  9. javascript 中 事件流和事件冒泡
  10. Linq 查询与普通查询的区别
  11. TurnipBit开发板掷骰子小游戏DIY教程实例
  12. EasyUI常用组件(基础)
  13. SimpleDateFormat日期格式解析
  14. supervisor进程管理的使用
  15. January 15th, 2018 Week 03rd Monday
  16. 【软件测试】Junit入门
  17. C++ map.insert: pair和make_pair区别
  18. 使用C++/libCurl/Jsoncpp读取arcgis wmts 服务(restful模式)
  19. linux 标准I/O (二)
  20. 在MS单元测试中引发期望异常

热门文章

  1. python开发基础之字符编码、文件处理和函数基础
  2. PHP.TP框架下商品项目的优化1-时间插件、鼠标所在行高亮、布局规划页面
  3. Keil如何生成bin文件【Keil生成Bin文件的方法】
  4. P1330 封锁阳光大学(染色问题)
  5. laravel5.5事件系统
  6. centos使用--防火墙
  7. MyEclipse - 问题集 - maven update project 后,项目jdk的版本变化
  8. Active Directory-Integrated Zones
  9. mongodb安装和配置三步走
  10. JMeter学习笔记(六) 文件下载接口测试