Statistics
| Branch: | Tag: | Revision:

mininet / examples / clustercli.py @ e0bf8ece

History | View | Annotate | Download (3.48 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
nx, graphviz_layout, plt = None, None, None  # Will be imported on demand
9

    
10

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

    
14
    @staticmethod
15
    def colorsFor( seq ):
16
        "Return a list of background colors for a sequence"
17
        colors = [ 'red', 'lightgreen', 'cyan', 'yellow', 'orange',
18
                  'magenta', 'pink', 'grey', 'brown',
19
                  'white' ]
20
        slen, clen = len( seq ), len( colors )
21
        reps = max( 1, slen / clen )
22
        colors = colors * reps
23
        colors = colors[ 0 : slen ]
24
        return colors
25
    
26
    def do_plot( self, line ):
27
        "Plot topology colored by node placement"
28
        # Import networkx if needed
29
        global nx, plt
30
        if not nx:
31
            try:
32
                import networkx as nx
33
                import matplotlib.pyplot as plt
34
                import pygraphviz
35
                assert pygraphviz  # silence pyflakes
36
            except:
37
                error( 'plot requires networkx, matplotlib and pygraphviz - '
38
                       'please install them and try again\n' )
39
                return
40
        # Make a networkx Graph
41
        g = nx.Graph()
42
        mn = self.mn
43
        servers, hosts, switches = mn.servers, mn.hosts, mn.switches
44
        nodes = hosts + switches
45
        g.add_nodes_from( nodes )
46
        links = [ ( link.intf1.node, link.intf2.node )
47
                  for link in self.mn.links ]
48
        g.add_edges_from( links )
49
        # Pick some shapes and colors
50
        # shapes = hlen * [ 's' ] + slen * [ 'o' ]
51
        color = dict( zip( servers, self.colorsFor( servers ) ) )
52
        # Plot it!
53
        pos = nx.graphviz_layout( g )
54
        opts = { 'ax': None, 'font_weight': 'bold',
55
                 'width': 2, 'edge_color': 'darkblue' }
56
        hcolors = [ color[ getattr( h, 'server', 'localhost' ) ] for h in hosts ]
57
        scolors = [ color[ getattr( s, 'server', 'localhost' ) ] for s in switches ]
58
        nx.draw_networkx( g, pos=pos, nodelist=hosts, node_size=800, label='host',
59
                          node_color=hcolors, node_shape='s', **opts )
60
        nx.draw_networkx( g, pos=pos, nodelist=switches, node_size=1000,
61
                          node_color=scolors, node_shape='o', **opts )
62
        # Get rid of axes, add title, and show
63
        fig = plt.gcf()
64
        ax = plt.gca()
65
        ax.get_xaxis().set_visible( False )
66
        ax.get_yaxis().set_visible( False )
67
        fig.canvas.set_window_title( 'Mininet')
68
        plt.title( 'Node Placement', fontweight='bold' )
69
        plt.show()
70

    
71
    def do_status( self, line ):
72
        "Report on node shell status"
73
        nodes = self.mn.hosts + self.mn.switches
74
        for node in nodes:
75
            node.shell.poll()
76
        exited = [ node for node in nodes
77
                   if node.shell.returncode is not None ]
78
        if exited:
79
            for node in exited:
80
                output( '%s has exited with code %d\n'
81
                        % ( node, node.shell.returncode ) )
82
        else:
83
            output( 'All nodes are still running.\n' )
84

    
85

    
86
    def do_placement( self, line ):
87
        "Describe node placement"
88
        mn = self.mn
89
        nodes = mn.hosts + mn.switches + mn.controllers
90
        for server in mn.servers:
91
            names = [ n.name for n in nodes if hasattr( n, 'server' )
92
                      and n.server == server ]
93
            output( '%s: %s\n' % ( server, ' '.join( names ) ) )