Statistics
| Branch: | Tag: | Revision:

mininet / examples / clustercli.py @ 18aab5b7

History | View | Annotate | Download (3.68 KB)

1
#!/usr/bin/python
2

    
3
"CLI for Mininet Cluster Edition prototype demo"
4

    
5
from mininet.cli import CLI
6
from mininet.log import output, error
7

    
8
# pylint: disable=global-statement
9
nx, graphviz_layout, plt = None, None, None  # Will be imported on demand
10

    
11

    
12
class ClusterCLI( CLI ):
13
    "CLI with additional commands for Cluster Edition demo"
14

    
15
    @staticmethod
16
    def colorsFor( seq ):
17
        "Return a list of background colors for a sequence"
18
        colors = [ 'red', 'lightgreen', 'cyan', 'yellow', 'orange',
19
                  'magenta', 'pink', 'grey', 'brown',
20
                  'white' ]
21
        slen, clen = len( seq ), len( colors )
22
        reps = max( 1, slen / clen )
23
        colors = colors * reps
24
        colors = colors[ 0 : slen ]
25
        return colors
26

    
27
    def do_plot( self, _line ):
28
        "Plot topology colored by node placement"
29
        # Import networkx if needed
30
        global nx, plt
31
        if not nx:
32
            try:
33
                import networkx
34
                nx = networkx  # satisfy pylint
35
                from matplotlib import pyplot
36
                plt = pyplot   # satisfiy pylint
37
                import pygraphviz
38
                assert pygraphviz  # silence pyflakes
39
            except ImportError:
40
                error( 'plot requires networkx, matplotlib and pygraphviz - '
41
                       'please install them and try again\n' )
42
                return
43
        # Make a networkx Graph
44
        g = nx.Graph()
45
        mn = self.mn
46
        servers, hosts, switches = mn.servers, mn.hosts, mn.switches
47
        nodes = hosts + switches
48
        g.add_nodes_from( nodes )
49
        links = [ ( link.intf1.node, link.intf2.node )
50
                  for link in self.mn.links ]
51
        g.add_edges_from( links )
52
        # Pick some shapes and colors
53
        # shapes = hlen * [ 's' ] + slen * [ 'o' ]
54
        color = dict( zip( servers, self.colorsFor( servers ) ) )
55
        # Plot it!
56
        pos = nx.graphviz_layout( g )
57
        opts = { 'ax': None, 'font_weight': 'bold',
58
                 'width': 2, 'edge_color': 'darkblue' }
59
        hcolors = [ color[ getattr( h, 'server', 'localhost' ) ]
60
                    for h in hosts ]
61
        scolors = [ color[ getattr( s, 'server', 'localhost' ) ]
62
                    for s in switches ]
63
        nx.draw_networkx( g, pos=pos, nodelist=hosts, node_size=800,
64
                          label='host', node_color=hcolors, node_shape='s',
65
                          **opts )
66
        nx.draw_networkx( g, pos=pos, nodelist=switches, node_size=1000,
67
                          node_color=scolors, node_shape='o', **opts )
68
        # Get rid of axes, add title, and show
69
        fig = plt.gcf()
70
        ax = plt.gca()
71
        ax.get_xaxis().set_visible( False )
72
        ax.get_yaxis().set_visible( False )
73
        fig.canvas.set_window_title( 'Mininet')
74
        plt.title( 'Node Placement', fontweight='bold' )
75
        plt.show()
76

    
77
    def do_status( self, _line ):
78
        "Report on node shell status"
79
        nodes = self.mn.hosts + self.mn.switches
80
        for node in nodes:
81
            node.shell.poll()
82
        exited = [ node for node in nodes
83
                   if node.shell.returncode is not None ]
84
        if exited:
85
            for node in exited:
86
                output( '%s has exited with code %d\n'
87
                        % ( node, node.shell.returncode ) )
88
        else:
89
            output( 'All nodes are still running.\n' )
90

    
91

    
92
    def do_placement( self, _line ):
93
        "Describe node placement"
94
        mn = self.mn
95
        nodes = mn.hosts + mn.switches + mn.controllers
96
        for server in mn.servers:
97
            names = [ n.name for n in nodes if hasattr( n, 'server' )
98
                      and n.server == server ]
99
            output( '%s: %s\n' % ( server, ' '.join( names ) ) )