ia64/xen-unstable

changeset 13652:bb2b1b6662fa

[XENAPI] Make server uri easily configurable via ENV or CLI opts.

Add a vm-stat command that just polls a vm for instantaneous statistics.

Signed-off-by: Alastair Tse <atse@xensource.com>
author Alastair Tse <atse@xensource.com>
date Fri Jan 26 15:41:16 2007 +0000 (2007-01-26)
parents ace66ef96b5e
children b8b75fdbd21f
files tools/python/scripts/xapi.py
line diff
     1.1 --- a/tools/python/scripts/xapi.py	Fri Jan 26 15:08:28 2007 +0000
     1.2 +++ b/tools/python/scripts/xapi.py	Fri Jan 26 15:41:16 2007 +0000
     1.3 @@ -19,6 +19,7 @@
     1.4  import sys
     1.5  import time
     1.6  import re
     1.7 +import os
     1.8  sys.path.append('/usr/lib/python')
     1.9  
    1.10  from xen.util.xmlrpclib2 import ServerProxy
    1.11 @@ -27,6 +28,10 @@ from pprint import pprint
    1.12  from types import DictType
    1.13  from getpass import getpass
    1.14  
    1.15 +# Get default values from the environment
    1.16 +SERVER_URI = os.environ.get('XAPI_SERVER_URI', 'http://localhost:9363/')
    1.17 +SERVER_USER = os.environ.get('XAPI_SERVER_USER', '')
    1.18 +SERVER_PASS = os.environ.get('XAPI_SERVER_PASS', '')
    1.19  
    1.20  MB = 1024 * 1024
    1.21  
    1.22 @@ -173,16 +178,16 @@ def connect(*args):
    1.23      global _server, _session, _initialised
    1.24      
    1.25      if not _initialised:
    1.26 -        # try without password
    1.27 +        # try without password or default credentials
    1.28          try:
    1.29 -            _server = ServerProxy('http://localhost:9363/')
    1.30 +            _server = ServerProxy(SERVER_URI)
    1.31              _session = execute(_server.session, 'login_with_password',
    1.32 -                               ('',''))
    1.33 +                               (SERVER_USER, SERVER_PASS))
    1.34          except:
    1.35              login = raw_input("Login: ")
    1.36              password = getpass()
    1.37              creds = (login, password)            
    1.38 -            _server = ServerProxy('http://localhost:9363/')
    1.39 +            _server = ServerProxy(SERVER_URI)
    1.40              _session = execute(_server.session, 'login_with_password',
    1.41                                 creds)
    1.42  
    1.43 @@ -262,6 +267,8 @@ def xapi_vm_name(args, async = False):
    1.44  def xapi_vm_list(args, async = False):
    1.45      opts, args = parse_args('vm-list', args, set_defaults = True)
    1.46      is_long = opts and opts.long
    1.47 +
    1.48 +    list_only = args
    1.49      
    1.50      server, session = connect()
    1.51      vm_uuids = execute(server, 'VM.get_all', (session,))
    1.52 @@ -274,6 +281,11 @@ def xapi_vm_list(args, async = False):
    1.53  
    1.54      for uuid in vm_uuids:
    1.55          vm_info = execute(server, 'VM.get_record', (session, uuid))
    1.56 +
    1.57 +        # skip domain if we don't want
    1.58 +        if list_only and vm_info['name_label'] not in list_only:
    1.59 +            continue
    1.60 +        
    1.61          if is_long:
    1.62              vbds = vm_info['VBDs']
    1.63              vifs = vm_info['VIFs']
    1.64 @@ -692,6 +704,28 @@ def xapi_debug_wait(args, async = False)
    1.65      task_uuid = execute(server, 'debug.wait', (session, secs), async=async)
    1.66      print 'Task UUID: %s' % task_uuid
    1.67  
    1.68 +def xapi_vm_stat(args, async = False):
    1.69 +    domname = args[0]
    1.70 +    
    1.71 +    server, session = connect()
    1.72 +    vm_uuid = resolve_vm(server, session, domname)
    1.73 +    vif_uuids = execute(server, 'VM.get_VIFs', (session, vm_uuid))
    1.74 +    vbd_uuids = execute(server, 'VM.get_VBDs', (session, vm_uuid))
    1.75 +    vcpus_utils = execute(server, 'VM.get_VCPUs_utilisation',
    1.76 +                          (session, vm_uuid))
    1.77 +
    1.78 +    for vcpu_num in sorted(vcpus_utils.keys()):
    1.79 +        print 'CPU %s : %5.2f%%' % (vcpu_num, vcpus_utils[vcpu_num] * 100)
    1.80 +        
    1.81 +    for vif_uuid in vif_uuids:
    1.82 +        vif = execute(server, 'VIF.get_record', (session, vif_uuid))
    1.83 +        print '%(device)s: rx: %(io_read_kbs)10.2f tx: %(io_write_kbs)10.2f' \
    1.84 +              % vif
    1.85 +    for vbd_uuid in vbd_uuids:
    1.86 +        vbd = execute(server, 'VBD.get_record', (session, vbd_uuid))
    1.87 +        print '%(device)s: rd: %(io_read_kbs)10.2f wr: %(io_write_kbs)10.2f' \
    1.88 +              % vbd
    1.89 +        
    1.90  #
    1.91  # Command Line Utils
    1.92  #
    1.93 @@ -777,12 +811,55 @@ def usage(command = None, print_usage = 
    1.94          parse_args(command, ['-h'])
    1.95  
    1.96  def main(args):
    1.97 +    
    1.98 +    # poor man's optparse that doesn't abort on unrecognised opts
    1.99  
   1.100 -    if len(args) < 1 or args[0] in ('-h', '--help', 'help'):
   1.101 +    options = {}
   1.102 +    remaining = []
   1.103 +    
   1.104 +    arg_n = 0
   1.105 +    while args:
   1.106 +        arg = args.pop(0)
   1.107 +        
   1.108 +        if arg in ('--help', '-h'):
   1.109 +            options['help'] = True
   1.110 +        elif arg in ('--server', '-s') and args:
   1.111 +            options['server'] = args.pop(0)
   1.112 +        elif arg in ('--user', '-u') and args:
   1.113 +            options['user'] = args.pop(0)
   1.114 +        elif arg in ('--password', '-p') and args:
   1.115 +            options['password'] = args.pop(0)
   1.116 +        else:
   1.117 +            remaining.append(arg)
   1.118 +
   1.119 +    # abort here if these conditions are true
   1.120 +
   1.121 +    if options.get('help') and not remaining:
   1.122          usage()
   1.123          sys.exit(1)
   1.124  
   1.125 -    subcmd = args[0].replace('-', '_')
   1.126 +    if options.get('help') and remaining:
   1.127 +        usage(remaining[0])
   1.128 +        sys.exit(1)
   1.129 +
   1.130 +    if not remaining:
   1.131 +        usage()
   1.132 +        sys.exit(1)
   1.133 +
   1.134 +    if options.get('server'):
   1.135 +        # it is ugly to use a global, but it is simple
   1.136 +        global SERVER_URI
   1.137 +        SERVER_URI = options['server']
   1.138 +
   1.139 +    if options.get('user'):
   1.140 +        global SERVER_USER
   1.141 +        SERVER_USER = options['user']
   1.142 +
   1.143 +    if options.get('password'):
   1.144 +        global SERVER_PASS
   1.145 +        SERVER_PASS = options['password']
   1.146 +
   1.147 +    subcmd = remaining[0].replace('-', '_')
   1.148      is_async = 'async' in subcmd
   1.149      if is_async:
   1.150          subcmd = re.sub('async_', '', subcmd)
   1.151 @@ -796,12 +873,8 @@ def main(args):
   1.152          usage()
   1.153          sys.exit(1)
   1.154  
   1.155 -    if '-h' in args[1:] or '--help' in args[1:]:
   1.156 -        usage(subcmd)
   1.157 -        sys.exit(1)
   1.158 -        
   1.159      try:
   1.160 -        subcmd_func(args[1:], async = is_async)
   1.161 +        subcmd_func(remaining[1:], async = is_async)
   1.162      except XenAPIError, e:
   1.163          print 'Error: %s' % str(e.args[0])
   1.164          sys.exit(2)