@@ -0,0 +1,2597 @@
+from operator import attrgetter%0Afrom ryu.app import simple_switch_13%0Afrom ryu.controller import ofp_event%0Afrom ryu.controller.handler import MAIN_DISPATCHER, DEAD_DISPATCHER%0Afrom ryu.controller.handler import set_ev_cls%0Afrom ryu.lib import hub%0Aimport os%0A%0A%0Aclass SimpleMonitor(simple_switch_13.SimpleSwitch13):%0A    def __init__(self, *args, **kwargs):%0A        super(SimpleMonitor, self).__init__(*args, **kwargs)%0A        self.datapaths = %7B%7D%0A        self.monitor_thread = hub.spawn(self._monitor)%0A%0A    @set_ev_cls(ofp_event.EventOFPStateChange,%0A                %5BMAIN_DISPATCHER, DEAD_DISPATCHER%5D)%0A    def _state_change_handler(self, ev):%0A        datapath = ev.datapath%0A        if ev.state == MAIN_DISPATCHER:%0A            if not datapath.id in self.datapaths:%0A                self.logger.debug('register datapath: %25016x', datapath.id)%0A                self.datapaths%5Bdatapath.id%5D = datapath%0A        elif ev.state == DEAD_DISPATCHER:%0A            if datapath.id in self.datapaths:%0A                self.logger.debug('unregister datapath: %25016x', datapath.id)%0A                del self.datapaths%5Bdatapath.id%5D%0A%0A    def _monitor(self):%0A        while True:%0A            for dp in self.datapaths.values():%0A                self._request_stats(dp)%0A            hub.sleep(5)%0A%0A%0A    def _request_stats(self, datapath):%0A        self.logger.debug('send stats request: %25016x', datapath.id)%0A        ofproto = datapath.ofproto%0A        parser = datapath.ofproto_parser%0A%0A        req = parser.OFPFlowStatsRequest(datapath)%0A        datapath.send_msg(req)%0A%0A        req = parser.OFPPortStatsRequest(datapath, 0, ofproto.OFPP_ANY)%0A        datapath.send_msg(req)%0A%0A    @set_ev_cls(ofp_event.EventOFPPortStatsReply, MAIN_DISPATCHER)%0A    def _port_stats_reply_handler(self, ev):%0A        body = ev.msg.body%0A        self.logger.info('start')%0A        self.logger.info('datapath,         port,    '%0A                     'rx_pkts,  rx_bytes, rx_error,'%0A                     'tx_pkts,  tx_bytes, tx_error,')%0A        for stat in sorted(body, key=attrgetter('port_no')):%0A            self.logger.info('%25016x, %258x, %258d, %258d, %258d, %258d, %258d, %258d,',%0A                             ev.msg.datapath.id, stat.port_no,%0A                             stat.rx_packets, stat.rx_bytes, stat.rx_errors,%0A                             stat.tx_packets, stat.tx_bytes, stat.tx_errors)%0A        self.logger.info('end')%0A%0A    @set_ev_cls(ofp_event.EventOFPFlowStatsReply, MAIN_DISPATCHER)%0A    def _flow_stats_reply_handler(self, ev):%0A        body = ev.msg.body%0A        os.system('sudo rm /ss.log') %0A        self.logger.info('start') %0A        self.logger.info('start')%0A

create simple monitor