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

Last change on this file since 560 was 553, checked in by quentin, 16 years ago

Support YAML (default), JSON, and Pickle formats, depending on the client's choice

  • Property svn:executable set to *
File size: 1.2 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.xs import xs
7
8trans = xs()
9
10def live_vms():
11    domids = set(trans.ls('', '/local/domain'))
12    domids.discard('0')
13
14    vms = dict()
15
16    for domid in domids:
17        name, data = get_dom(int(domid))
18        if name.startswith('d_'):
19            name = name[2:]
20            vms[name] = data
21    return vms
22
23def get_dom(domid):
24    name = trans.read('', '/local/domain/%d/name' % domid)
25    data = dict()
26    data['domid'] = domid
27    # presence of a graphical console
28    data['console'] = trans.read('', '/local/domain/%d/device/vfb/0/state' % domid)
29    # uptime
30    data['vm'] = trans.read('', '/local/domain/%d/vm' % domid)
31    data['start_time'] = float(trans.read('', '%s/start_time' % data['vm']))
32    data['uptime'] = time.time()-data['start_time']
33   
34    return name, data
35
36if __name__ == '__main__':
37    vms = live_vms()
38    if '--json' in sys.argv[1:]:
39        import cjson
40        print cjson.encode(vms)
41    elif '--pickle' in sys.argv[1:]:
42        import cPickle
43        cPickle.dump(vms, sys.stdout, cPickle.HIGHEST_PROTOCOL)
44    else:
45        import yaml
46        print yaml.safe_dump(vms, default_flow_style=False)
Note: See TracBrowser for help on using the repository browser.