Statistics
| Branch: | Tag: | Revision:

mininet / bin / mn_clean.py @ 8f20b95d

History | View | Annotate | Download (2.05 KB)

1
#!/usr/bin/env python
2
"""Mininet Cleanup
3

4
@author Bob Lantz (rlantz@cs.stanford.edu)
5

6
Unfortunately, Mininet and OpenFlow don't always clean up
7
properly after themselves. Until they do (or until cleanup
8
functionality is integrated into the python code), this
9
script may be used to get rid of unwanted garbage. It may
10
also get rid of 'false positives', but hopefully nothing
11
irreplaceable!
12
"""
13

    
14
from subprocess import Popen, PIPE
15
import re
16

    
17
from mininet.util import quietRun
18

    
19
def sh( cmd ): 
20
   "Print a command and send it to the shell"
21
   print cmd
22
   return Popen( [ '/bin/sh', '-c', cmd ], 
23
      stdout=PIPE ).communicate()[ 0 ]
24

    
25
def cleanUpScreens():
26
   "Remove moldy old screen sessions."      
27
   r = r'(\d+.[hsc]\d+)'
28
   output = sh( 'screen -ls' ).split( '\n' )
29
   for line in output:
30
      m = re.search( r, line )
31
      if m is not None:
32
         quietRun( 'screen -S ' + m.group( 1 ) + ' -X kill' )
33
 
34
def cleanup():
35
   """Clean up junk which might be left over from old runs;
36
      do fast stuff before slow dp and link removal!"""
37
      
38
   print "*** Removing excess controllers/ofprotocols/ofdatapaths/pings/noxes"
39
   zombies = 'controller ofprotocol ofdatapath ping nox_core lt-nox_core '
40
   zombies += 'udpbwtest'
41
   # Note: real zombie processes can't actually be killed, since they 
42
   # are already (un)dead. Then again,
43
   # you can't connect to them either, so they're mostly harmless.
44
   sh( 'killall -9 ' + zombies + ' 2> /dev/null' )
45

    
46
   print "*** Removing junk from /tmp"
47
   sh( 'rm -f /tmp/vconn* /tmp/vlogs* /tmp/*.out /tmp/*.log' )
48

    
49
   print "*** Removing old screen sessions"
50
   cleanUpScreens()
51

    
52
   print "*** Removing excess kernel datapaths"
53
   dps = sh( "ps ax | egrep -o 'dp[0-9]+' | sed 's/dp/nl:/'" ).split( '\n')
54
   for dp in dps: 
55
      if dp != '': sh( 'dpctl deldp ' + dp )
56
      
57
   print "*** Removing all links of the pattern foo-ethX"
58
   links = sh( "ip link show | egrep -o '(\w+-eth\w+)'" ).split( '\n' )
59
   for link in links: 
60
      if link != '': sh( "ip link del " + link )
61

    
62
   print "*** Cleanup complete."
63

    
64
if __name__ == "__main__":
65
   cleanup()