Skip to content

Commit 1bb9903

Browse files
committed
Added multipoll and multiping examples.
1 parent a9c2888 commit 1bb9903

3 files changed

Lines changed: 186 additions & 0 deletions

File tree

examples/README

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ miniedit.py:
3535

3636
This example demonstrates creating a network via a graphical editor.
3737

38+
multiping.py:
39+
40+
This example demonstrates one method for
41+
monitoring output from multiple hosts, using node.monitor().
42+
43+
multipoll.py:
44+
45+
This example demonstrates monitoring output files from multiple hosts.
46+
3847
multitest.py:
3948

4049
This example creates a network and runs multiple tests on it.

examples/multiping.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/python
2+
3+
"""
4+
multiping.py: monitor multiple sets of hosts using ping
5+
6+
This demonstrates how one may send a simple shell script to
7+
multiple hosts and monitor their output interactively for a period=
8+
of time.
9+
"""
10+
11+
from mininet.net import Mininet
12+
from mininet.node import Node
13+
from mininet.topo import SingleSwitchTopo
14+
from mininet.log import setLogLevel
15+
16+
from select import poll, POLLIN
17+
from time import time
18+
19+
def chunks( l, n ):
20+
"Divide list l into chunks of size n - thanks Stackoverflow"
21+
return [ l[ i : i + n ] for i in range( 0, len( l ), n ) ]
22+
23+
def startpings( host, targetips ):
24+
"Tell host to repeatedly ping targets"
25+
26+
targetips.append( '10.0.0.200' )
27+
28+
targetips = ' '.join( targetips )
29+
30+
# BL: Not sure why loopback intf isn't up!
31+
host.cmd( 'ifconfig lo up' )
32+
33+
34+
# Simple ping loop
35+
cmd = ( 'while true; do '
36+
' for ip in %s; do ' % targetips +
37+
' echo -n %s "->" $ip ' % host.IP() +
38+
' `ping -c1 -w 1 $ip | grep packets` ;'
39+
' sleep 1;'
40+
' done; '
41+
'done &' )
42+
43+
print ( '*** Host %s (%s) will be pinging ips: %s' %
44+
( host.name, host.IP(), targetips ) )
45+
46+
host.cmd( cmd )
47+
48+
def multiping( netsize, chunksize, seconds):
49+
"Ping subsets of size chunksize in net of size netsize"
50+
51+
# Create network and identify subnets
52+
topo = SingleSwitchTopo( netsize )
53+
net = Mininet( topo=topo )
54+
net.start()
55+
hosts = net.hosts
56+
subnets = chunks( hosts, chunksize )
57+
58+
# Create polling object
59+
fds = [ host.stdout.fileno() for host in hosts ]
60+
poller = poll()
61+
for fd in fds:
62+
poller.register( fd, POLLIN )
63+
64+
# Start pings
65+
for subnet in subnets:
66+
ips = [ host.IP() for host in subnet ]
67+
for host in subnet:
68+
startpings( host, ips )
69+
70+
# Monitor output
71+
endTime = time() + seconds
72+
while time() < endTime:
73+
readable = poller.poll(1000)
74+
for fd, _mask in readable:
75+
node = Node.outToNode[ fd ]
76+
print '%s:' % node.name, node.monitor().strip()
77+
78+
# Stop pings
79+
for host in hosts:
80+
host.cmd( 'kill %while' )
81+
82+
net.stop()
83+
84+
85+
if __name__ == '__main__':
86+
setLogLevel( 'info' )
87+
multiping( netsize=20, chunksize=4, seconds=10 )
88+
89+
90+
91+
92+
93+
94+

examples/multipoll.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/python
2+
3+
"""
4+
Simple example of sending output to multiple files and
5+
monitoring them
6+
"""
7+
8+
from mininet.topo import SingleSwitchTopo
9+
from mininet.net import Mininet
10+
from mininet.log import setLogLevel
11+
from mininet.util import quietRun
12+
13+
from time import time, sleep
14+
from select import poll, POLLIN
15+
from subprocess import Popen, PIPE
16+
17+
def monitorFiles( outfiles, seconds, timeoutms ):
18+
devnull = open( '/dev/null', 'w' )
19+
tails, fdToFile, fdToHost = {}, {}, {}
20+
for h, outfile in outfiles.iteritems():
21+
tail = Popen( [ 'tail', '-f', outfile ],
22+
stdout=PIPE, stderr=devnull )
23+
fd = tail.stdout.fileno()
24+
tails[ h ] = tail
25+
fdToFile[ fd ] = tail.stdout
26+
fdToHost[ fd ] = h
27+
# Prepare to poll output files
28+
readable = poll()
29+
for t in tails.values():
30+
readable.register( t.stdout.fileno(), POLLIN )
31+
# Run until a set number of seconds have elapsed
32+
endTime = time() + seconds
33+
while time() < endTime:
34+
fdlist = readable.poll(timeoutms)
35+
if fdlist:
36+
for fd, _flags in fdlist:
37+
f = fdToFile[ fd ]
38+
host = fdToHost[ fd ]
39+
# Wait for a line of output
40+
line = f.readline().strip()
41+
yield host, line
42+
else:
43+
# If we timed out, return nothing
44+
yield None, ''
45+
for t in tails.values():
46+
t.terminate()
47+
devnull.close() # Not really necessary
48+
49+
50+
def monitorTest( N=3, seconds=3 ):
51+
"Run pings and monitor multiple hosts"
52+
topo = SingleSwitchTopo( N )
53+
net = Mininet( topo )
54+
net.start()
55+
hosts = net.hosts
56+
print "Starting test..."
57+
server = hosts[ 0 ]
58+
outfiles, errfiles = {}, {}
59+
for h in hosts:
60+
# Create and/or erase output files
61+
outfiles[ h ] = '/tmp/%s.out' % h.name
62+
errfiles[ h ] = '/tmp/%s.err' % h.name
63+
h.cmd( 'echo >', outfiles[ h ] )
64+
h.cmd( 'echo >', errfiles[ h ] )
65+
# Start pings
66+
h.cmdPrint('ping', server.IP(),
67+
'>', outfiles[ h ],
68+
'2>', errfiles[ h ],
69+
'&' )
70+
print "Monitoring output for", seconds, "seconds"
71+
for h, line in monitorFiles( outfiles, seconds, timeoutms=500 ):
72+
if h:
73+
print '%s: %s' % ( h.name, line )
74+
for h in hosts:
75+
h.cmd('kill %ping')
76+
net.stop()
77+
78+
79+
if __name__ == '__main__':
80+
setLogLevel('info')
81+
monitorTest()
82+
83+

0 commit comments

Comments
 (0)