Re: [Ryu-devel] List to .csv in "port stats"

2017-06-25 Thread Iwase Yusuke


Hi,

Currently, Ryu does not provide such feature, but how about using "csv" module (Python standard 
library)?


e.g.)
diff --git a/ryu/app/simple_switch_13.py b/ryu/app/simple_switch_13.py
index 3e7c598..f509b67 100644
--- a/ryu/app/simple_switch_13.py
+++ b/ryu/app/simple_switch_13.py
@@ -13,11 +13,15 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.

+import csv
+
 from ryu.base import app_manager
 from ryu.controller import ofp_event
 from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
 from ryu.controller.handler import set_ev_cls
 from ryu.ofproto import ofproto_v1_3
+from ryu.ofproto import ofproto_v1_3_parser
+from ryu.lib import hub
 from ryu.lib.packet import packet
 from ryu.lib.packet import ethernet
 from ryu.lib.packet import ether_types
@@ -30,6 +34,15 @@ class SimpleSwitch13(app_manager.RyuApp):
 super(SimpleSwitch13, self).__init__(*args, **kwargs)
 self.mac_to_port = {}

+self._f = open('port_stats.csv', 'w')
+self.csv_writer = csv.DictWriter(
+self._f, ofproto_v1_3_parser.OFPPortStats._fields)
+self.csv_writer.writeheader()
+
+def stop(self):
+super(SimpleSwitch13, self).stop()
+self._f.close()
+
 @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
 def switch_features_handler(self, ev):
 datapath = ev.msg.datapath
@@ -48,6 +61,8 @@ class SimpleSwitch13(app_manager.RyuApp):
   ofproto.OFPCML_NO_BUFFER)]
 self.add_flow(datapath, 0, match, actions)

+hub.spawn(self.send_port_stats_request_loop, datapath)
+
 def add_flow(self, datapath, priority, match, actions, buffer_id=None):
 ofproto = datapath.ofproto
 parser = datapath.ofproto_parser
@@ -117,3 +132,21 @@ class SimpleSwitch13(app_manager.RyuApp):
 out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,
   in_port=in_port, actions=actions, data=data)
 datapath.send_msg(out)
+
+def send_port_stats_request_loop(self, datapath):
+while self.is_active:
+self._send_port_stats_request(datapath)
+
+hub.sleep(3)
+
+def _send_port_stats_request(self, datapath):
+ofp = datapath.ofproto
+ofp_parser = datapath.ofproto_parser
+
+req = ofp_parser.OFPPortStatsRequest(datapath, 0, ofp.OFPP_ANY)
+datapath.send_msg(req)
+
+@set_ev_cls(ofp_event.EventOFPPortStatsReply, MAIN_DISPATCHER)
+def port_stats_reply_handler(self, ev):
+for stat in ev.msg.body:
+self.csv_writer.writerow(stat._asdict())


$ cat port_stats.csv
port_no,rx_packets,tx_packets,rx_bytes,tx_bytes,rx_dropped,tx_dropped,rx_errors,tx_errors,rx_frame_err,rx_over_err,rx_crc_err,collisions,duration_sec,duration_nsec
4294967294,0,0,0,0,0,0,0,0,0,0,0,0,723,94000
1,8,32,648,4016,0,0,0,0,0,0,0,0,723,94400
2,8,32,648,4016,0,0,0,0,0,0,0,0,723,94400
4294967294,0,0,0,0,0,0,0,0,0,0,0,0,726,94400
1,8,32,648,4016,0,0,0,0,0,0,0,0,726,94800
2,8,32,648,4016,0,0,0,0,0,0,0,0,726,94800
...(snip)


Thanks,
Iwase


On 2017年06月25日 21:25, Sergio Silva wrote:

Dear friend,
Please, I would like to know if there is a possibility that the "port stats" files could be modified 
from list to default ".csv". This my suggestion is due to the fact that the standard 
"Comma-Separated Values" are files are widely used with numpy, pandas, database and others. For 
scientific analysis, statistical surveys with prediction and artificial intelligence. I think if Ryu 
would have a lot more to offer if the collected files had the format ".csv" than a list. I thank you.


2017-06-25 9:03 GMT-03:00 Sergio Silva >:

Dear friend,
Please, I would like to know if there is a possibility that the "port 
stats" files could be
modified from list to default ".csv". This my suggestion is due to the fact 
that the standard
"Comma-Separated Values" are files are widely used with numpy, pandas, 
database and others. For
scientific analysis, statistical surveys with prediction and artificial 
intelligence. I think if
Ryu would have a lot more to offer if the collected files had the format 
".csv" than a list. I
thank you.




--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot



___
Ryu-devel mailing list
Ryu-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ryu-devel



--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___

Re: [Ryu-devel] List to .csv in "port stats"

2017-06-25 Thread Sergio Silva
Dear friend,
Please, I would like to know if there is a possibility that the "port
stats" files could be modified from list to default ".csv". This my
suggestion is due to the fact that the standard "Comma-Separated Values"
are files are widely used with numpy, pandas, database and others. For
scientific analysis, statistical surveys with prediction and artificial
intelligence. I think if Ryu would have a lot more to offer if the
collected files had the format ".csv" than a list. I thank you.

2017-06-25 9:03 GMT-03:00 Sergio Silva :

> Dear friend,
> Please, I would like to know if there is a possibility that the "port
> stats" files could be modified from list to default ".csv". This my
> suggestion is due to the fact that the standard "Comma-Separated Values"
> are files are widely used with numpy, pandas, database and others. For
> scientific analysis, statistical surveys with prediction and artificial
> intelligence. I think if Ryu would have a lot more to offer if the
> collected files had the format ".csv" than a list. I thank you.
>
--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot___
Ryu-devel mailing list
Ryu-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ryu-devel