Statistics
| Branch: | Tag: | Revision:

mininet / examples / popen.py @ 50cebe67

History | View | Annotate | Download (1023 Bytes)

1
#!/usr/bin/python
2

    
3
"""
4
This example monitors a number of hosts using host.popen() and
5
pmonitor()
6
"""
7

    
8
from mininet.net import Mininet
9
from mininet.node import CPULimitedHost
10
from mininet.topo import SingleSwitchTopo
11
from mininet.log import setLogLevel
12
from mininet.util import custom, pmonitor
13

    
14
def monitorhosts( hosts=5, sched='cfs' ):
15
    "Start a bunch of pings and monitor them using popen"
16
    mytopo = SingleSwitchTopo( hosts )
17
    cpu = .5 / hosts
18
    myhost = custom( CPULimitedHost, cpu=cpu, sched=sched )
19
    net = Mininet( topo=mytopo, host=myhost )
20
    net.start()
21
    # Start a bunch of pings
22
    popens = {}
23
    last = net.hosts[ -1 ]
24
    for host in net.hosts:
25
        popens[ host ] = host.popen( "ping -c5 %s" % last.IP() )
26
        last = host
27
    # Monitor them and print output
28
    for host, line in pmonitor( popens ):
29
        if host:
30
            print "<%s>: %s" % ( host.name, line.strip() )
31
    # Done
32
    net.stop()
33

    
34
if __name__ == '__main__':
35
    setLogLevel( 'info' )
36
    monitorhosts( hosts=5 )