| 1 | from xen.xend.XendClient import server |
|---|
| 2 | from xen.xend import sxp |
|---|
| 3 | from xen.xend import PrettyPrint |
|---|
| 4 | |
|---|
| 5 | import types |
|---|
| 6 | |
|---|
| 7 | def getDomInfo( domain ): |
|---|
| 8 | domInfoHash = {} |
|---|
| 9 | try: |
|---|
| 10 | domInfoHash = sxp2hash( server.xend_domain( domain ) ) |
|---|
| 11 | domInfoHash['dom'] = domain |
|---|
| 12 | except: |
|---|
| 13 | domInfoHash['name'] = "Error getting domain details" |
|---|
| 14 | return domInfoHash |
|---|
| 15 | |
|---|
| 16 | def sxp2hash( s ): |
|---|
| 17 | sxphash = {} |
|---|
| 18 | |
|---|
| 19 | for child in sxp.children( s ): |
|---|
| 20 | if isinstance( child, types.ListType ) and len( child ) > 1: |
|---|
| 21 | if isinstance( child[1], types.ListType ) and len( child ) > 1: |
|---|
| 22 | sxphash[ child[0] ] = sxp2hash( child[1] ) |
|---|
| 23 | else: |
|---|
| 24 | sxphash[ child[0] ] = child[1] |
|---|
| 25 | |
|---|
| 26 | return sxphash |
|---|
| 27 | |
|---|
| 28 | def ssxp2hash( s ): |
|---|
| 29 | sxphash = {} |
|---|
| 30 | |
|---|
| 31 | for i in s: |
|---|
| 32 | if isinstance( i, types.ListType ) and len( i ) > 1: |
|---|
| 33 | sxphash[ i[0] ] = i[1] |
|---|
| 34 | |
|---|
| 35 | return sxphash |
|---|
| 36 | |
|---|
| 37 | def hash2sxp( h ): |
|---|
| 38 | hashsxp = [] |
|---|
| 39 | |
|---|
| 40 | for (key, item) in h.items(): |
|---|
| 41 | hashsxp.append( [key, item] ) |
|---|
| 42 | |
|---|
| 43 | return hashsxp |
|---|
| 44 | |
|---|
| 45 | def string2sxp( string ): |
|---|
| 46 | pin = sxp.Parser() |
|---|
| 47 | pin.input( string ) |
|---|
| 48 | return pin.get_val() |
|---|
| 49 | |
|---|
| 50 | def sxp2string( sexp ): |
|---|
| 51 | return sxp.to_string( sexp ) |
|---|
| 52 | |
|---|
| 53 | def sxp2prettystring( sxp ): |
|---|
| 54 | class tmp: |
|---|
| 55 | def __init__( self ): |
|---|
| 56 | self.str = "" |
|---|
| 57 | def write( self, str ): |
|---|
| 58 | self.str = self.str + str |
|---|
| 59 | temp = tmp() |
|---|
| 60 | PrettyPrint.prettyprint( sxp, out=temp ) |
|---|
| 61 | return temp.str |
|---|
| 62 | |
|---|
| 63 | def getVar( var, request, default=None ): |
|---|
| 64 | |
|---|
| 65 | arg = request.args.get( var ) |
|---|
| 66 | |
|---|
| 67 | if arg is None: |
|---|
| 68 | return default |
|---|
| 69 | else: |
|---|
| 70 | return arg[ len( arg )-1 ] |
|---|
| 71 | |
|---|
| 72 | def bigTimeFormatter( time ): |
|---|
| 73 | time = float( time ) |
|---|
| 74 | weeks = time // 604800 |
|---|
| 75 | remainder = time % 604800 |
|---|
| 76 | days = remainder // 86400 |
|---|
| 77 | |
|---|
| 78 | remainder = remainder % 86400 |
|---|
| 79 | |
|---|
| 80 | hms = smallTimeFormatter( remainder ) |
|---|
| 81 | |
|---|
| 82 | return "%d weeks, %d days, %s" % ( weeks, days, hms ) |
|---|
| 83 | |
|---|
| 84 | def smallTimeFormatter( time ): |
|---|
| 85 | time = float( time ) |
|---|
| 86 | hours = time // 3600 |
|---|
| 87 | remainder = time % 3600 |
|---|
| 88 | mins = remainder // 60 |
|---|
| 89 | secs = time % 60 |
|---|
| 90 | return "%02d:%02d:%04.1f (hh:mm:ss.s)" % ( hours, mins, secs ) |
|---|
| 91 | |
|---|
| 92 | def stateFormatter( state ): |
|---|
| 93 | states = [ 'Running', 'Blocked', 'Paused', 'Shutdown', 'Crashed' ] |
|---|
| 94 | |
|---|
| 95 | stateStr = "" |
|---|
| 96 | |
|---|
| 97 | for i in range( len( state ) ): |
|---|
| 98 | if state[i] != "-": |
|---|
| 99 | stateStr += "%s, " % states[ i ] |
|---|
| 100 | |
|---|
| 101 | return stateStr + " (%s)" % state |
|---|
| 102 | |
|---|
| 103 | def memoryFormatter( mem ): |
|---|
| 104 | mem = int( mem ) |
|---|
| 105 | if mem >= 1024: |
|---|
| 106 | mem = float( mem ) / 1024 |
|---|
| 107 | return "%3.2fGb" % mem |
|---|
| 108 | else: |
|---|
| 109 | return "%7dMb" % mem |
|---|
| 110 | |
|---|
| 111 | def cpuFormatter( mhz ): |
|---|
| 112 | mhz = int( mhz ) |
|---|
| 113 | if mhz > 1000: |
|---|
| 114 | ghz = float( mhz ) / 1000.0 |
|---|
| 115 | return "%4.2fGHz" % ghz |
|---|
| 116 | else: |
|---|
| 117 | return "%4dMHz" % mhz |
|---|
| 118 | |
|---|
| 119 | def hyperthreadFormatter( threads ): |
|---|
| 120 | try: |
|---|
| 121 | if int( threads ) > 1: |
|---|
| 122 | return "Yes" |
|---|
| 123 | else: |
|---|
| 124 | return "No" |
|---|
| 125 | except: |
|---|
| 126 | return "No" |
|---|