diff options
author | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2014-08-18 14:44:01 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2014-08-18 16:54:11 +0900 |
commit | 950785ee8d9c7c1dfe2fde9c64181df634b83bd7 (patch) | |
tree | 18cb1706064c30a9c4375ceede3410bcc03bb4a2 | |
parent | 7a06df454b89a33472925121d71e71d213238989 (diff) |
bmpstation: make configurable through environment variables
you can specify listening HOST and PORT by setting env variable
'RYU_BMP_SERVER_HOST' and 'RYU_BMP_SERVER_PORT'.
'RYU_BMP_OUTPUT_FILE' specifes the output file name
'RYU_BMP_FAILED_DUMP' specifes the file name of the error dump
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | ryu/app/bmpstation.py | 50 |
1 files changed, 39 insertions, 11 deletions
diff --git a/ryu/app/bmpstation.py b/ryu/app/bmpstation.py index 67c069c2..f8d22de6 100644 --- a/ryu/app/bmpstation.py +++ b/ryu/app/bmpstation.py @@ -13,9 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. +import os import socket -import logging -logging.basicConfig(level=logging.DEBUG) +import time from ryu.base import app_manager @@ -23,23 +23,32 @@ from ryu.lib import hub from ryu.lib.hub import StreamServer from ryu.lib.packet import bmp -SERVER_HOST = '0.0.0.0' -SERVER_PORT = 11019 - class BMPStation(app_manager.RyuApp): def __init__(self): super(BMPStation, self).__init__() self.name = 'bmpstation' + self.server_host = os.environ.get('RYU_BMP_SERVER_HOST', '0.0.0.0') + self.server_port = int(os.environ.get('RYU_BMP_SERVER_PORT', 11019)) + output_file = os.environ.get('RYU_BMP_OUTPUT_FILE', 'ryu_bmp.log') + failed_dump = os.environ.get('RYU_BMP_FAILED_DUMP', + 'ryu_bmp_failed.dump') + + self.output_fd = open(output_file, 'w') + self.failed_dump_fd = open(failed_dump, 'w') + + self.failed_pkt_count = 0 def start(self): super(BMPStation, self).start() + self.logger.debug("listening on %s:%s" % (self.server_host, + self.server_port)) - return hub.spawn(StreamServer((SERVER_HOST, SERVER_PORT), + return hub.spawn(StreamServer((self.server_host, self.server_port), self.loop).serve_forever) def loop(self, sock, addr): - logging.debug("started bmp loop.") + self.logger.debug("start bmp loop: client %s" % str(addr)) is_active = True buf = bytearray() required_len = bmp.BMPMessage._HDR_LEN @@ -53,7 +62,7 @@ class BMPStation(app_manager.RyuApp): while len(buf) >= required_len: version, len_, _ = bmp.BMPMessage.parse_header(buf) if version != bmp.VERSION: - logging.error("unsupported bmp version: %d" % version) + self.logger.error("unsupported bmp version: %d" % version) is_active = False break @@ -61,8 +70,27 @@ class BMPStation(app_manager.RyuApp): if len(buf) < required_len: break - msg, rest = bmp.BMPMessage.parser(buf) - print msg, '\n' + try: + msg, rest = bmp.BMPMessage.parser(buf) + except Exception, e: + pkt = buf[:len_] + self.failed_dump_fd.write(pkt) + self.failed_dump_fd.flush() + buf = buf[len_:] + self.failed_pkt_count += 1 + self.logger.error("failed to parse: %s" + " (total fail count: %d)" % + (e, self.failed_pkt_count)) + else: + t = time.strftime("%Y %b %d %H:%M:%S", time.localtime()) + self.logger.debug("%s | %s\n" % (t, msg)) + self.output_fd.write("%s | %s\n\n" % (t, msg)) + self.output_fd.flush() + buf = rest - buf = rest required_len = bmp.BMPMessage._HDR_LEN + + self.logger.debug("end bmp loop.") + sock.close() + self.output_fd.close() + self.failed_dump_fd.close() |