Statistics
| Branch: | Tag: | Revision:

mininet / cleanup @ 0a9ea29f

History | View | Annotate | Download (1.8 KB)

1
#!/usr/bin/python
2

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

    
12
from subprocess import Popen, PIPE
13
import re
14

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

    
21
def cleanUpScreens():
22
   "Remove moldy old screen sessions."      
23
   r = r'(\d+.[hsc]\d+)'
24
   output = sh( 'screen -ls' ).split( '\n' )
25
   for line in output:
26
      m = re.search( r, line )
27
      if m is not None:
28
         quietRun( 'screen -S ' + m.group( 1 ) + ' -X kill' )
29
 
30
def cleanup():
31
   print "*** Removing all links of the pattern foo-ethX"
32
   links = sh( "ip link show | egrep -o '(\w+-eth\w+)'" ).split( '\n' )
33
   for link in links: 
34
      if link != '': sh( "ip link del " + link )
35

    
36
   print "*** Removing excess controllers/ofprotocols/ofdatapaths/pings/noxes"
37
   zombies = 'controller ofprotocol ofdatapath ping nox_core lt-nox_core'
38
   # Note: real zombie processes can't actually be killed, since they 
39
   # are already (un)dead. Then again,
40
   # you can't connect to them either, so they're mostly harmless.
41
   sh( 'killall -9 ' + zombies + ' 2> /dev/null' )
42

    
43
   print "*** Removing excess kernel datapaths"
44
   dps = sh( "ps ax | egrep -o 'dp[0-9]+' | sed 's/dp/nl:/'" ).split( '\n')
45
   for dp in dps: 
46
      if dp != '': sh( 'ip link del ' + link )
47

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

    
51
   print "*** Removing old screen sessions"
52
   cleanUpScreens()
53

    
54
   print "*** Cleanup complete."
55

    
56
if __name__ == "__main__":
57
   cleanup()