RYU流量监控

前言

Ryu book上的一个流量监控的应用,相对比较好看懂

实验代码

github源码

from ryu.app import simple_switch_13
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER, DEAD_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.lib import hub
from operator import attrgetter class SimpleMonitor13(simple_switch_13.SimpleSwitch13): def __init__(self, *args, **kwargs):
super(SimpleMonitor13, self).__init__(*args, **kwargs)
self.datapaths = {}
self.monitor_thread = hub.spawn(self._monitor) """
EventOFPStateChange An event class for negotiation phase change notification. An instance of this class is sent to observer after changing the negotiation phase. An instance has at least the following attributes.
"""
@set_ev_cls(ofp_event.EventOFPStateChange, [MAIN_DISPATCHER, DEAD_DISPATCHER])
def _state_change_handler(self, ev):
datapath = ev.datapath
if ev.state == MAIN_DISPATCHER:
if datapath.id not in self.datapaths:
self.logger.debug('register datapath: %016x', datapath.id)
self.datapaths[datapath.id] = datapath
elif ev.state == DEAD_DISPATCHER:
if datapath.id in self.datapaths:
self.logger.debug('unregister datapath: %016x', datapath.id)
del self.datapaths[datapath.id] def _monitor(self):
while True:
for dp in self.datapaths.values():
self._request_stats(dp)
hub.sleep(10) """
OFPFlowStatsRequest Individual flow statistics request message The controller uses this message to query individual flow statistics.
""" def _request_stats(self, datapath):
self.logger.debug('send stats request: %016x', datapath.id)
ofproto = datapath.ofproto
parser = datapath.ofproto_parser req = parser.OFPFlowStatsRequest(datapath)
datapath.send_msg(req) req = parser.OFPPortStatsRequest(datapath, 0, ofproto.OFPP_ANY)
datapath.send_msg(req) """
OFPFlowStatsReply Individual flow statistics reply message The switch responds with this message to an individual flow statistics request.
""" @set_ev_cls(ofp_event.EventOFPFlowStatsReply, MAIN_DISPATCHER)
def _flow_stats_reply_handler(self, ev):
body = ev.msg.body print body self.logger.info('datapath '
'in-port eth-dst '
'out-port packets bytes')
self.logger.info('---------------- '
'-------- ----------------- '
'-------- -------- --------')
for stat in sorted([flow for flow in body if flow.priority == 1],
key=lambda flow: (flow.match['out_port'],
flow.match['eth_dst'])):
self.logger.info('%016x %8x %17s %8x %8d %8d',
ev.msg.datapath.id,
stat.match['in_port'], stat.match['eth_dst'],
stat.instructions[0].actions[0].port,
stat.packet_count, stat.byte_count) """
OFPPortStatsReply Port statistics reply message The switch responds with this message to a port statistics request.
"""
@set_ev_cls(ofp_event.EventOFPPortStatsReply, MAIN_DISPATCHER)
def _port_stats_reply_handler(self, ev):
body = ev.msg.body
# print body
# print type(body)
self.logger.info('datapath port '
'rx-pkts rx-bytes rx-error '
'tx-pkts tx-bytes tx-error')
self.logger.info('---------------- -------- '
'-------- -------- -------- '
'-------- -------- --------')
for stat in sorted(body, key=attrgetter('port_no')):
# print type(stat)
self.logger.info('%016x %8x %8d %8d %8d %8d %8d %8d',
ev.msg.datapath.id, stat.port_no,
stat.rx_packets, stat.rx_bytes, stat.rx_errors,
stat.tx_packets, stat.tx_bytes, stat.tx_errors)

代码细节

这次实验细节主要是关于 sorted, lambda, attrgetter

回忆--RYU流量监控的更多相关文章

  1. SDN实验---Ryu的应用开发(三)流量监控

    一:实现流量监控 (一)流量监控原理 其中控制器向交换机周期下发获取统计消息,请求交换机消息------是主动下发过程 流速公式:是(t1时刻的流量-t0时刻的流量)/(t1-t0) 剩余带宽公式:链 ...

  2. 《Unix/Linux日志分析与流量监控》书稿完成

    <Unix/Linux日志分析与流量监控>书稿完成 近日,历时3年创作的75万字书稿已完成,本书紧紧围绕网络安全的主题,对各种Unix/Linux系统及网络服务日志进行了全面系统的讲解,从 ...

  3. 《UNIX/Linux网络日志分析与流量监控》新书发布

    本书从UNIX/Linux系统的原始日志(Raw Log)采集与分析讲起,逐步深入到日志审计与计算机取证环节.书中提供了多个案例,每个案例都以一种生动的记事手法讲述了网络遭到入侵之后,管理人员开展系统 ...

  4. 《Unix/Linux网络日志分析与流量监控》获2015年度最受读者喜爱的IT图书奖

    <Unix/Linux网络日志分析与流量监控>获2015年度最受读者喜爱的IT图书奖.刊登在<中华读书报>( 2015年01月28日 19 版) 我的2015年新作刊登在< ...

  5. linux系统CPU,内存,磁盘,网络流量监控脚本

    前序 1,#cat /proc/stat/ 信息包含了所有CPU活动的信息,该文件中的所有值都是从系统启动开始累积到当前时刻 2,#vmstat –s 或者#vmstat 虚拟内存统计 3, #cat ...

  6. 从零开始学习iftop流量监控(找出服务器耗费流量最多的ip和端口)

    一.iftop是什么 iftop是类似于top的实时流量监控工具. 作用:监控网卡的实时流量(可以指定网段).反向解析IP.显示端口信息等 官网:http://www.ex-parrot.com/~p ...

  7. Linux服务器---流量监控bandwidthd

    Bandwidthd Bandwidthd是一款免费的流量监控软件,它可以用图标的方式展现出网络流量行为,并且可区分出ftp.tcp等各种协议的流量. 1.安装一些依赖软件 [root@localho ...

  8. vue table中使用多选的问题(翻页后如何保存已选项),联动echarts图表实现流量监控

    流量监控项目需求: 根据表格数据,添加多选功能,默认全选,根据已选项更新图表视图 1.表格需要多选 2.要联动图表,所以关键是要利用表格多选的触发回调函数 vue table中使用多选: 很简单,只需 ...

  9. linux 网络和流量监控

    1.流量监控 工具iptraf 安装:sudo apt-get install iptraf 运行:sudo iptraf   2.网络监控扫描 参考:https://www.cnblogs.com/ ...

随机推荐

  1. 计数dp

    计数dp 计数类的$dp$没做过几个,所以之前都放到"思维"标签下了,后来发现原来这属于一类问题啊...搬过来了. 管道取珠:https://www.lydsy.com/Judge ...

  2. [luogu2469] 星际竞速

    题面 ​ 巨佬一眼就能看出这是最小路径覆盖, 我这个蒟蒻还是太弱了... ​ 我们可以知道跳跃值为点权w[i], 两点之间距离为边权ww ​ 对于每个点, 在最小路径覆盖问题中, 假设每个点都是一条路 ...

  3. linux问题解答

    1.Linux如何查询进程?杀死一个进程? ps命令提供进程的一次性查看(瞬时信息),结果不是动态的:top对命令实时监控 ps只是查看进程,而top还可以监视系统性能,如平均负载,cpu和内存的消耗 ...

  4. Dubbo -- 系统学习 笔记 -- 目录

    用户指南 入门 背景 需求 架构 用法 快速启动 服务提供者 服务消费者 依赖 必需依赖 缺省依赖 可选依赖 成熟度 功能成熟度 策略成熟度 配置 Xml配置 属性配置 注解配置 API配置 示例 启 ...

  5. js中获取url后面的参数值

    方法: //获取url路径?号后面的参数值.function GetRequest() { var url = location.search; //获取url中"?"符后的字串 ...

  6. CentOS7服务器上部署深度/机器学习环境推荐首选anaconda3

    CentOS7服务器上部署深度/机器学习环境推荐首选anaconda3,亲测~~ 因为可以创建不同的环境版本或虚拟环境 CentOS7服务器安装anaconda3后,CentOS7服务器开启后自动将a ...

  7. HDU1754

    https://vjudge.net/contest/66989#problem/B 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜 ...

  8. day72

    今日内容: 1 创建多表模型(详情见代码) from django.db import models # Create your models here. class Publish(models.M ...

  9. Docker一键部署Hadoop心得(一)

    最近一直在折腾使用docker一键部署全分布式hadoop集群,虽然一键部署的脚本写好了并且可以成功运行出各个节点,但在运行一个wordcount实例时出现了错误,错误如下: java.io.IOEx ...

  10. 【C/C++】1~20的阶乘之和

    一. 前情   能点进这篇文章的,想必也已经知道了C语言和C++语言,以及阶乘的定义,所以在此不赘述了.SUM(1!~20!)这个问题是我在大一学C语言时的一个小题,最近又要把编译器装回来,所以装完之 ...