source: trunk/packages/sipb-xen-remctl-auto/files/usr/sbin/sipb-xen-listvms @ 1022

Last change on this file since 1022 was 717, checked in by price, 16 years ago

xenstore isn't even consistent in what happens on error

Usually when the VM is gone and xsc.ls(, '/vm/..uuid..') would
return empty, an xsc.ls(
, '/vm/..uuid../start_time') returns None.
But sometimes it throws an xs.lowlevel.xs.Error, saying 22,
'Invalid argument', instead.

  • Property svn:executable set to *
File size: 1.3 KB
Line 
1#!/usr/bin/python
2
3import sys
4import time
5sys.path.insert(0, '/usr/lib/xen-3.1-1/lib/python')
6from xen.lowlevel import xs
7
8xsc = xs.xs()
9
10def live_vms():
11    domids = set(xsc.ls('', '/local/domain'))
12    domids.discard('0')
13
14    vms = dict()
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.startswith('d_'):
22            name = name[2:]
23            vms[name] = data
24    return vms
25
26def get_dom(domid):
27    name = xsc.read('', '/local/domain/%d/name' % domid)
28    data = dict()
29    data['domid'] = domid
30    # presence of a graphical console
31    data['console'] = xsc.read('', '/local/domain/%d/device/vfb/0/state' % domid)
32    # uptime
33    data['vm'] = xsc.read('', '/local/domain/%d/vm' % domid)
34    data['start_time'] = float(xsc.read('', '%s/start_time' % data['vm']))
35    data['uptime'] = time.time()-data['start_time']
36   
37    return name, data
38
39if __name__ == '__main__':
40    vms = live_vms()
41    if '--json' in sys.argv[1:]:
42        import cjson
43        print cjson.encode(vms)
44    elif '--pickle' in sys.argv[1:]:
45        import cPickle
46        cPickle.dump(vms, sys.stdout, cPickle.HIGHEST_PROTOCOL)
47    else:
48        import yaml
49        print yaml.dump(vms, Dumper=yaml.CSafeDumper, default_flow_style=False)
Note: See TracBrowser for help on using the repository browser.