Last change
on this file since 714 was
714,
checked in by price, 16 years ago
|
use a slightly cleaner Xen API in listvms
|
-
Property svn:executable set to
*
|
File size:
1.3 KB
|
Rev | Line | |
---|
[531] | 1 | #!/usr/bin/python |
---|
[122] | 2 | |
---|
[531] | 3 | import sys |
---|
[533] | 4 | import time |
---|
[531] | 5 | sys.path.insert(0, '/usr/lib/xen-3.1-1/lib/python') |
---|
[714] | 6 | from xen.xend.xenstore import xstransact |
---|
[531] | 7 | |
---|
[714] | 8 | xst = xstransact.xstransact() |
---|
[531] | 9 | |
---|
| 10 | def live_vms(): |
---|
[714] | 11 | domids = set(xst.List('/local/domain')) |
---|
[533] | 12 | domids.discard('0') |
---|
[531] | 13 | |
---|
| 14 | vms = dict() |
---|
| 15 | |
---|
| 16 | for domid in domids: |
---|
[652] | 17 | try: |
---|
| 18 | name, data = get_dom(int(domid)) |
---|
[712] | 19 | except TypeError: |
---|
[654] | 20 | continue # went down since we started |
---|
[531] | 21 | if name.startswith('d_'): |
---|
| 22 | name = name[2:] |
---|
| 23 | vms[name] = data |
---|
| 24 | return vms |
---|
| 25 | |
---|
| 26 | def get_dom(domid): |
---|
[714] | 27 | name = xst.Read('/local/domain/%d/name' % domid) |
---|
[531] | 28 | data = dict() |
---|
| 29 | data['domid'] = domid |
---|
| 30 | # presence of a graphical console |
---|
[714] | 31 | data['console'] = xst.Read('/local/domain/%d/device/vfb/0/state' % domid) |
---|
[531] | 32 | # uptime |
---|
[714] | 33 | data['vm'] = xst.Read('/local/domain/%d/vm' % domid) |
---|
| 34 | data['start_time'] = float(xst.Read('%s/start_time' % data['vm'])) |
---|
[533] | 35 | data['uptime'] = time.time()-data['start_time'] |
---|
[531] | 36 | |
---|
| 37 | return name, data |
---|
| 38 | |
---|
| 39 | if __name__ == '__main__': |
---|
| 40 | vms = live_vms() |
---|
[553] | 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 |
---|
[564] | 49 | print yaml.dump(vms, Dumper=yaml.CSafeDumper, default_flow_style=False) |
---|
Note: See
TracBrowser
for help on using the repository browser.