Make the listvms remctl include domains that get stuck in pygrub
[invirt/packages/invirt-remote.git] / host / usr / sbin / invirt-listvms
1 #!/usr/bin/python
2
3 import sys
4 import time
5 from xen.lowlevel import xs
6
7 xsc = xs.xs()
8
9 def live_vms():
10     domids = set(xsc.ls('', '/local/domain'))
11     domids.discard('0')
12
13     vms = dict()
14     xenserver = None
15
16     for domid in domids:
17         try:
18             name, data = get_dom(int(domid))
19         except (xs.Error, TypeError):
20             continue # went down since we started
21         if name is None: # xenstore may not be fully populated if it hung early
22             try:
23                 if xenserver is None:
24                     from xen.xm.main import SERVER_LEGACY_XMLRPC, parseServer
25                     from xen.util.xmlrpcclient import ServerProxy
26                     serverType, serverURI = parseServer()
27                     if serverType != SERVER_LEGACY_XMLRPC:
28                         continue # we can't handle a XenAPI server yet
29                     xenserver = ServerProxy(serverURI)
30                 name = filter(lambda x: isinstance(x, list) and x[0]=='name',
31                               xenserver.xend.domain(int(domid)))[0][1]
32             except:
33                 continue # we have nothing we can show about this domain
34         if name.startswith('d_'):
35             name = name[2:]
36             vms[name] = data
37     return vms
38
39 def get_dom(domid):
40     name = xsc.read('', '/local/domain/%d/name' % domid)
41     data = dict()
42     data['domid'] = domid
43     # presence of a graphical console
44     data['console'] = xsc.read('', '/local/domain/%d/device/vfb/0/state' % domid)
45     # uptime
46     data['vm'] = xsc.read('', '/local/domain/%d/vm' % domid)
47     start_time = xsc.read('', '%s/start_time' % data['vm'])
48     if start_time is not None:
49         data['start_time'] = float(start_time)
50         data['uptime'] = time.time()-data['start_time']
51     else:
52         data['uptime'] = 0
53     # CD or autoinstall, if we're using either
54     vbds = xsc.ls('', '/local/domain/%d/device/vbd' % domid)
55     if vbds is not None:
56         for vbd in vbds:
57             dev = '/local/domain/%d/device/vbd/%s' % (domid, vbd)
58             dev_type = xsc.read('', '%s/device-type' % dev)
59             dev_params = xsc.read('', '%s/params' % xsc.read('', '%s/backend' % dev))
60             if dev_type == 'cdrom':
61                 data['cdrom'] = dev_params
62             elif dev_type == 'disk' and dev_params == '/dev/xenvg/s_install_hda':
63                 data['autoinstall'] = True
64     cmdline = xsc.read('', '%s/image/cmdline' % data['vm'])
65     if cmdline is not None and 'auto=true' in cmdline:
66         data['autoinstall'] = True
67
68     return name, data
69
70 if __name__ == '__main__':
71     vms = live_vms()
72     if '--json' in sys.argv[1:]:
73         import cjson
74         print cjson.encode(vms)
75     elif '--pickle' in sys.argv[1:]:
76         import cPickle
77         cPickle.dump(vms, sys.stdout, cPickle.HIGHEST_PROTOCOL)
78     else:
79         import yaml
80         print yaml.dump(vms, Dumper=yaml.CSafeDumper, default_flow_style=False)