source: trunk/packages/invirt-remote/host/usr/sbin/invirt-listvms @ 2336

Last change on this file since 2336 was 2245, checked in by broder, 16 years ago

Include a CDROM drive in listvms if there is one.

  • Property svn:executable set to *
File size: 1.6 KB
Line 
1#!/usr/bin/python
2
3import sys
4import time
5from xen.lowlevel import xs
6
7xsc = xs.xs()
8
9def live_vms():
10    domids = set(xsc.ls('', '/local/domain'))
11    domids.discard('0')
12
13    vms = dict()
14
15    for domid in domids:
16        try:
17            name, data = get_dom(int(domid))
18        except (xs.Error, TypeError):
19            continue # went down since we started
20        if name is not None and name.startswith('d_'):
21            name = name[2:]
22            vms[name] = data
23    return vms
24
25def get_dom(domid):
26    name = xsc.read('', '/local/domain/%d/name' % domid)
27    data = dict()
28    data['domid'] = domid
29    # presence of a graphical console
30    data['console'] = xsc.read('', '/local/domain/%d/device/vfb/0/state' % domid)
31    # uptime
32    data['vm'] = xsc.read('', '/local/domain/%d/vm' % domid)
33    data['start_time'] = float(xsc.read('', '%s/start_time' % data['vm']))
34    data['uptime'] = time.time()-data['start_time']
35    # CD, if there is one
36    vbds = xsc.ls('', '/local/domain/%d/device/vbd' % domid)
37    for vbd in vbds:
38        dev = '/local/domain/%d/device/vbd/%s' % (domid, vbd)
39        if xsc.read('', '%s/device-type' % dev) == 'cdrom':
40            data['cdrom'] = xsc.read('', '%s/params' % xsc.read('', '%s/backend' % dev))
41   
42    return name, data
43
44if __name__ == '__main__':
45    vms = live_vms()
46    if '--json' in sys.argv[1:]:
47        import cjson
48        print cjson.encode(vms)
49    elif '--pickle' in sys.argv[1:]:
50        import cPickle
51        cPickle.dump(vms, sys.stdout, cPickle.HIGHEST_PROTOCOL)
52    else:
53        import yaml
54        print yaml.dump(vms, Dumper=yaml.CSafeDumper, default_flow_style=False)
Note: See TracBrowser for help on using the repository browser.