This app shows how to handle netflow packets.

Signed-off-by: OHMURA Kei <[email protected]>
---
 ryu/app/netflow_dumper.py |   72 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)
 create mode 100644 ryu/app/netflow_dumper.py

diff --git a/ryu/app/netflow_dumper.py b/ryu/app/netflow_dumper.py
new file mode 100644
index 0000000..af91b2d
--- /dev/null
+++ b/ryu/app/netflow_dumper.py
@@ -0,0 +1,72 @@
+# Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+import gevent
+import logging
+import struct
+
+from gevent import socket
+from ryu.base import app_manager
+from ryu.lib.xflow import netflow
+
+LOG = logging.getLogger('ryu.app.netflow_dumper')
+
+DEFAULT_HOST = '127.0.0.1'  # TODO: XXX
+DEFAULT_PORT = 9996  # TODO: XXX
+
+NETFLOW_V5 = 0x05
+
+
+class NetFlowDumper(app_manager.RyuApp):
+    def __init__(self, *args, **kwargs):
+        super(NetFlowDumper, self).__init__(*args, **kwargs)
+        self.sock = None
+        self.thread = None
+        self._start_recv()
+
+    def close(self):
+        # TODO: closing..
+        pass
+
+    def ipaddr_to_str(self, ip):
+        return socket.inet_ntoa(struct.pack('!I', ip))
+
+    def ipaddr_to_int(self, s):
+        return struct.unpack('!I', socket.inet_aton(s))[0]
+
+    def _recv_loop(self):
+        while True:
+            (data, addrport) = self.sock.recvfrom(8192)
+
+            nf = netflow.NetFlow.parser(data)
+            if nf:
+                flows = nf.flows
+                LOG.info('NetFlow V%d containing %d flows',
+                         nf.version, nf.count)
+
+                for i in range(nf.count):
+                    LOG.info('Flow%d: %s -> %s ', i,
+                             self.ipaddr_to_str(flows[i].srcaddr),
+                             self.ipaddr_to_str(flows[i].dstaddr))
+                    LOG.info('%d packets and %d bytes in the flow',
+                             flows[i].dpkts, flows[i].doctets)
+            else:
+                LOG.info('NetFlow V%d is not suppported yet', v)
+
+    def _start_recv(self):
+        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+        self.sock.bind((DEFAULT_HOST, DEFAULT_PORT))
+        self.thread = gevent.spawn_later(0, self._recv_loop)
-- 
1.7.9.5


------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_jan
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to