simple_monitor.py 3.91 KB
Newer Older
1 2 3
from operator import attrgetter
from ryu.app import simple_switch_13
from ryu.controller import ofp_event
4 5 6
from ryu.controller import dpset
from ryu.controller.handler import (MAIN_DISPATCHER, DEAD_DISPATCHER,
                                    CONFIG_DISPATCHER)
7
from ryu.controller.handler import set_ev_cls 
8
from ryu.lib.dpid import dpid_to_str
9
from ryu.lib import hub
10
from ryu.lib import bwDB
11 12 13 14 15 16 17

class SimpleMonitor(simple_switch_13.SimpleSwitch13):
    def __init__(self, *args, **kwargs): 
        super(SimpleMonitor,
                self).__init__(*args, **kwargs)
        self.datapaths = {}
        self.monitor_thread = hub.spawn(self._monitor)
18
        bwDB.createBWDBConnection();
19 20 21 22 23

    @set_ev_cls(ofp_event.EventOFPStateChange,
                        [MAIN_DISPATCHER, DEAD_DISPATCHER])
    def _state_change_handler(self, ev):
        datapath = ev.datapath
24 25
        dp_str = dpid_to_str(datapath.id)
        bwDBEntry = {'dpid':dp_str,
26 27 28
                'usedbw':0,
                'rxBytes':0,
                'txBytes':0,
29
                'txDrops':0,
30 31 32
                'pktError':0,
                'timeStamp':0
                }
33 34 35 36
        if ev.state == MAIN_DISPATCHER:
            if not datapath.id in self.datapaths:
                self.logger.debug('register datapath: %016x', datapath.id)
                self.datapaths[datapath.id] = datapath
37 38 39 40
                for port,desc in datapath.ports.items():
                    bwDBEntry['port']=port
                    bwDBEntry['bandwidth']=desc.curr_speed
                    bwDB.insertBWDBDict(bwDBEntry);
41 42 43
        elif ev.state == DEAD_DISPATCHER:
            if datapath.id in self.datapaths:
                self.logger.debug('unregister datapath: %016x', datapath.id)
44 45 46
                for port in datapath.ports:
                    bwDBEntry['port']=port
                    bwDB.deleteBWDict(bwDBEntry);
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
                del self.datapaths[datapath.id]

    def _monitor(self):
        while True:
            for dp in self.datapaths.values():
                self._request_stats(dp)
            hub.sleep(10)

    def _request_stats(self, datapath):
        self.logger.debug('send stats request: %016x', datapath.id) 
        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser
        #no need for stat request for now
        #req = parser.OFPFlowStatsRequest(datapath)
        #datapath.send_msg(req)
        req = parser.OFPPortStatsRequest(datapath, 0,
                ofproto.OFPP_ANY)
        datapath.send_msg(req)

    @set_ev_cls(ofp_event.EventOFPPortStatsReply,[MAIN_DISPATCHER])
    def _port_stats_reply_handler(self, ev):
         body = ev.msg.body
69
         self.logger.info('datapath         port     timestamp '
70
                                  'rx-pkts  rx-bytes rx-error '
71 72
                                  'tx-pkts  tx-bytes tx-error '
                                  'rx-drop  tx-drop')
73
         self.logger.info('---------------- -------- '
74
                 '-------- -------- -------- '
75 76 77
                 '-------- -------- -------- '
                 '-------- -------- --------')
         for stat in sorted(body, key=attrgetter('port_no')):
78 79
             self.logger.info('%016x %8x %8d %8d %8d %8d %8d %8d %8d %8d %8d',
                     ev.msg.datapath.id, stat.port_no, stat.duration_sec,
80
                     stat.rx_packets, stat.rx_bytes, stat.rx_errors,
81 82
                     stat.tx_packets, stat.tx_bytes, stat.tx_errors,
                     stat.rx_dropped, stat.tx_dropped)
83
             dp_str = dpid_to_str(ev.msg.datapath.id)
84
             bwDbEntry= {
85
                     'dpid': dp_str,
86 87
                     'port':stat.port_no,
                     'usedbw':0, 
88 89
                     'rxBytes':stat.rx_bytes, 
                     'txBytes':stat.tx_bytes,
90
                     'pktError':stat.tx_dropped, 
91
                     'txDrops':stat.tx_dropped, 
92
                     'timeStamp':stat.duration_sec
93 94
                     };
             bwDB.updateBWDict(bwDbEntry);