Use YAML on listvms
[invirt/packages/invirt-remote.git] / files / usr / sbin / sipb-xen-listvms
1 #!/usr/bin/python
2
3 import sys
4 import time
5 sys.path.insert(0, '/usr/lib/xen-3.1-1/lib/python')
6 from xen.lowlevel.xs import xs
7 import yaml
8
9 trans = xs()
10
11 def live_vms():
12     domids = set(trans.ls('', '/local/domain'))
13     domids.discard('0')
14
15     vms = dict()
16
17     for domid in domids:
18         name, data = get_dom(int(domid))
19         if name.startswith('d_'):
20             name = name[2:]
21             vms[name] = data
22     return vms
23
24 def get_dom(domid):
25     name = trans.read('', '/local/domain/%d/name' % domid)
26     data = dict()
27     data['domid'] = domid
28     # presence of a graphical console
29     data['console'] = trans.read('', '/local/domain/%d/device/vfb/0/state' % domid)
30     # uptime
31     data['vm'] = trans.read('', '/local/domain/%d/vm' % domid)
32     data['start_time'] = float(trans.read('', '%s/start_time' % data['vm']))
33     data['uptime'] = time.time()-data['start_time']
34     
35     return name, data
36
37 if __name__ == '__main__':
38     vms = live_vms()
39     print yaml.safe_dump(vms, default_flow_style=False)