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

Last change on this file since 1989 was 1822, checked in by broder, 16 years ago

Merge invirt-remote-host and invirt-remote-server into invirt-remote

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