Masayoshi Kobayashi | f04afb3 | 2013-04-06 06:49:03 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | import sys |
| 3 | import time |
| 4 | import os |
Masayoshi Kobayashi | f9ccc38 | 2013-04-07 04:06:41 +0000 | [diff] [blame] | 5 | import re |
Pavlin Radoslavov | 092d0e2 | 2013-04-07 05:42:51 +0000 | [diff] [blame] | 6 | import json |
Masayoshi Kobayashi | f04afb3 | 2013-04-06 06:49:03 +0000 | [diff] [blame] | 7 | |
Masayoshi Kobayashi | f04afb3 | 2013-04-06 06:49:03 +0000 | [diff] [blame] | 8 | ping_cnt=3 |
Masayoshi Kobayashi | f9ccc38 | 2013-04-07 04:06:41 +0000 | [diff] [blame] | 9 | wait1=ping_cnt |
| 10 | wait2=10 |
Masayoshi Kobayashi | f04afb3 | 2013-04-06 06:49:03 +0000 | [diff] [blame] | 11 | |
Pavlin Radoslavov | 092d0e2 | 2013-04-07 05:42:51 +0000 | [diff] [blame] | 12 | CONFIG_FILE=os.getenv("HOME") + "/ONOS/web/config.json" |
| 13 | |
| 14 | def read_config(): |
| 15 | global controllers |
| 16 | f = open(CONFIG_FILE) |
| 17 | conf = json.load(f) |
| 18 | controllers = conf['controllers'] |
| 19 | f.close() |
Masayoshi Kobayashi | f04afb3 | 2013-04-06 06:49:03 +0000 | [diff] [blame] | 20 | |
Masayoshi Kobayashi | f9ccc38 | 2013-04-07 04:06:41 +0000 | [diff] [blame] | 21 | def do_pingall(): |
Masayoshi Kobayashi | f04afb3 | 2013-04-06 06:49:03 +0000 | [diff] [blame] | 22 | |
Masayoshi Kobayashi | f9ccc38 | 2013-04-07 04:06:41 +0000 | [diff] [blame] | 23 | pid=os.getpid() |
| 24 | os.popen("rm -f /tmp/ping.*") |
Masayoshi Kobayashi | f04afb3 | 2013-04-06 06:49:03 +0000 | [diff] [blame] | 25 | |
Masayoshi Kobayashi | f9ccc38 | 2013-04-07 04:06:41 +0000 | [diff] [blame] | 26 | filename = sys.argv[1] |
| 27 | f = open(filename, 'r') |
| 28 | nr_ping = 0 |
| 29 | for line in f: |
| 30 | if line[0] != "#": |
Masayoshi Kobayashi | f9ccc38 | 2013-04-07 04:06:41 +0000 | [diff] [blame] | 31 | fid=int(line.strip().split()[0]) |
Pavlin Radoslavov | 092d0e2 | 2013-04-07 05:42:51 +0000 | [diff] [blame] | 32 | # logfile="/tmp/ping.pid%d.%d" % (pid, fid) |
Masayoshi Kobayashi | f9ccc38 | 2013-04-07 04:06:41 +0000 | [diff] [blame] | 33 | logfile="/tmp/ping.%d" % (fid) |
| 34 | src_dpid=line.strip().split()[2] |
| 35 | dst_dpid=line.strip().split()[4] |
| 36 | src_nwid=int(src_dpid.split(':')[-2], 16) |
| 37 | dst_nwid=int(dst_dpid.split(':')[-2], 16) |
| 38 | src_hostid=int(src_dpid.split(':')[-1], 16) |
| 39 | dst_hostid=int(dst_dpid.split(':')[-1], 16) |
| 40 | cmd="echo \"Pingall flow %d : 192.168.%d.%d -> 192.168.%d.%d\" > %s" % (fid, src_nwid, src_hostid, dst_nwid, dst_hostid,logfile) |
| 41 | os.popen(cmd) |
Pavlin Radoslavov | 092d0e2 | 2013-04-07 05:42:51 +0000 | [diff] [blame] | 42 | cmd="ssh %s \'${HOME}/ONOS/test-network/mininet/mrun host%d \'ping -c %d -W 1 192.168.%d.%d\'\' >> %s 2>&1 &" % (controllers[src_nwid-1], src_hostid, ping_cnt, dst_nwid, dst_hostid,logfile) |
| 43 | print cmd |
Masayoshi Kobayashi | f9ccc38 | 2013-04-07 04:06:41 +0000 | [diff] [blame] | 44 | result = os.popen(cmd).read() |
| 45 | time.sleep(0.2) |
| 46 | nr_ping = nr_ping + 1 |
Masayoshi Kobayashi | f04afb3 | 2013-04-06 06:49:03 +0000 | [diff] [blame] | 47 | |
Masayoshi Kobayashi | f9ccc38 | 2013-04-07 04:06:41 +0000 | [diff] [blame] | 48 | f.close() |
| 49 | return nr_ping |
Masayoshi Kobayashi | f04afb3 | 2013-04-06 06:49:03 +0000 | [diff] [blame] | 50 | |
Masayoshi Kobayashi | f9ccc38 | 2013-04-07 04:06:41 +0000 | [diff] [blame] | 51 | def wait_ping_finish(nr_ping): |
| 52 | print "all pings started.. waiting for completion (%d sec)" % (wait1) |
| 53 | time.sleep(wait1) |
| 54 | cmd="cat /tmp/ping.* | grep \"packet loss\" |wc -l" |
| 55 | for i in range(wait2): |
| 56 | nr_done = int(os.popen(cmd).read()) |
| 57 | if nr_done == nr_ping: |
| 58 | break |
| 59 | print "%d ping finished" % nr_done |
| 60 | time.sleep(1) |
| 61 | |
| 62 | return nr_done |
Masayoshi Kobayashi | f04afb3 | 2013-04-06 06:49:03 +0000 | [diff] [blame] | 63 | |
Masayoshi Kobayashi | f9ccc38 | 2013-04-07 04:06:41 +0000 | [diff] [blame] | 64 | def report(nr_ping, nr_done): |
| 65 | cmd='cat /tmp/ping.* | grep " 0% packet loss" |wc -l' |
| 66 | nr_success = int(os.popen('cat /tmp/ping.* | grep " 0% packet loss" |wc -l').read()) |
| 67 | nr_incomplete = nr_ping - nr_done |
| 68 | nr_fail = nr_done - nr_success |
| 69 | |
| 70 | print "Pingall Result: success %d fail %d incomplete %d" % (nr_success, nr_fail, nr_incomplete) |
| 71 | if nr_fail > 0 or nr_incomplete > 0: |
| 72 | for i in range(nr_ping): |
| 73 | cmd="cat /tmp/ping.%d | head -n 1" % (i+1) |
| 74 | flow_desc = os.popen(cmd).read().strip() |
| 75 | |
| 76 | cmd="cat /tmp/ping.%d | grep \"packet loss\"" % (i+1) |
| 77 | result = os.popen(cmd).read().strip() |
| 78 | |
| 79 | if not re.search(" 0% packet loss", result): |
| 80 | print "flow # %d %s : %s" % (i+1, flow_desc, result) |
| 81 | |
| 82 | if __name__ == "__main__": |
Pavlin Radoslavov | 092d0e2 | 2013-04-07 05:42:51 +0000 | [diff] [blame] | 83 | read_config() |
Masayoshi Kobayashi | f9ccc38 | 2013-04-07 04:06:41 +0000 | [diff] [blame] | 84 | nr_ping=do_pingall() |
| 85 | nr_done=wait_ping_finish(nr_ping) |
| 86 | report(nr_ping, nr_done) |