• 回忆--RYU流量监控


    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

  • 相关阅读:
    thrift 依赖的库
    环球雅思名师公益大讲堂课表2.182.23 雅思资讯 环球雅思
    经验分享band 7.5已经工作的人如何准备雅思考试学习心得雅思频道|太傻网考试频道|常识 辅导 技巧 机经 名师 访谈 交流 资料 试题库
    Chapter 14. Boost.PropertyTree
    少讨论概念,少争论特征、少议论模型;多写代码、多做测试、多做应用。
    澳洲技术移民申请流程
    neo4j——图数据库初探 JDream314的专栏 博客频道 CSDN.NET
    Australia Immigration Network
    boost::property_tree
    海外澳洲技术移民花费一览表(2006年11月完整版) Topboy 博客园
  • 原文地址:https://www.cnblogs.com/wpqwpq/p/6610064.html
Copyright © 2020-2023  润新知