[531] | 1 | #!/usr/bin/python |
---|
[122] | 2 | |
---|
[531] | 3 | import sys |
---|
[533] | 4 | import time |
---|
[716] | 5 | from xen.lowlevel import xs |
---|
[531] | 6 | |
---|
[716] | 7 | xsc = xs.xs() |
---|
[531] | 8 | |
---|
| 9 | def 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 |
---|
[2098] | 20 | if name is not None and name.startswith('d_'): |
---|
[531] | 21 | name = name[2:] |
---|
| 22 | vms[name] = data |
---|
| 23 | return vms |
---|
| 24 | |
---|
| 25 | def 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'] |
---|
[2351] | 35 | # CD or autoinstall, if we're using either |
---|
[2245] | 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) |
---|
[2351] | 39 | dev_type = xsc.read('', '%s/device-type' % dev) |
---|
| 40 | dev_params = xsc.read('', '%s/params' % xsc.read('', '%s/backend' % dev)) |
---|
| 41 | if dev_type == 'cdrom': |
---|
| 42 | data['cdrom'] = dev_params |
---|
| 43 | elif dev_type == 'disk' and dev_params == '/dev/xenvg/s_install_hda': |
---|
| 44 | data['autoinstall'] = True |
---|
[531] | 45 | |
---|
| 46 | return name, data |
---|
| 47 | |
---|
| 48 | if __name__ == '__main__': |
---|
| 49 | vms = live_vms() |
---|
[553] | 50 | if '--json' in sys.argv[1:]: |
---|
| 51 | import cjson |
---|
| 52 | print cjson.encode(vms) |
---|
| 53 | elif '--pickle' in sys.argv[1:]: |
---|
| 54 | import cPickle |
---|
| 55 | cPickle.dump(vms, sys.stdout, cPickle.HIGHEST_PROTOCOL) |
---|
| 56 | else: |
---|
| 57 | import yaml |
---|
[564] | 58 | print yaml.dump(vms, Dumper=yaml.CSafeDumper, default_flow_style=False) |
---|