qvm-ls 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #!/usr/bin/python2
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. #
  21. #
  22. from qubes.qubes import QubesVmCollection
  23. from qubes.qubes import QubesHost
  24. from qubes.qubes import QubesException
  25. from optparse import OptionParser
  26. import sys
  27. fields = {
  28. "qid": {"func": "vm.qid"},
  29. "name": {"func": "('=>' if qvm_collection.get_default_template() is not None\
  30. and vm.qid == qvm_collection.get_default_template().qid else '')\
  31. + ('[' if vm.is_template() else '')\
  32. + ('<' if vm.is_disposablevm() else '')\
  33. + ('{' if vm.is_netvm() else '')\
  34. + vm.name \
  35. + (']' if vm.is_template() else '')\
  36. + ('>' if vm.is_disposablevm() else '')\
  37. + ('}' if vm.is_netvm() else '')"},
  38. "type": {"func": "'HVM' if vm.type == 'HVM' else \
  39. ('Tpl' if vm.is_template() else \
  40. ('' if vm.type in ['AppVM', 'DisposableVM'] else \
  41. vm.type.replace('VM','')))"},
  42. "updbl" : {"func": "'Yes' if vm.updateable else ''"},
  43. "template": {"func": "'n/a' if vm.is_template() else\
  44. ('None' if vm.template is None else\
  45. vm.template.name)"},
  46. "netvm": {"func": "'n/a' if vm.is_netvm() and not vm.is_proxyvm() else\
  47. ('*' if vm.uses_default_netvm else '') +\
  48. qvm_collection[vm.netvm.qid].name\
  49. if vm.netvm is not None else '-'"},
  50. "ip" : {"func": "vm.ip"},
  51. "ip back" : {"func": "vm.gateway if vm.is_netvm() else 'n/a'"},
  52. "gateway/DNS" : {"func": "vm.netvm.gateway if vm.netvm else 'n/a'"},
  53. "xid" : {"func" : "vm.get_xid() if vm.is_running() else '-'"},
  54. "mem" : {"func" : "(str(vm.get_mem()/1024) + ' MB') if vm.is_running() else '-'"},
  55. "cpu" : {"func" : "round (cpu_usages[vm.get_xid()]['cpu_usage'], 1) if vm.is_running() else '-'"},
  56. "disk": {"func" : "str(vm.get_disk_utilization()/(1024*1024)) + ' MB'"},
  57. "state": {"func" : "vm.get_power_state()"},
  58. "priv-curr": {"func" : "str(vm.get_disk_utilization_private_img()/(1024*1024)) + ' MB'"},
  59. "priv-max": {"func" : "str(vm.get_private_img_sz()/(1024*1024)) + ' MB'"},
  60. "priv-util": {"func" : "str(vm.get_disk_utilization_private_img()*100/vm.get_private_img_sz()) + '%' if vm.get_private_img_sz() != 0 else '-'"},
  61. "root-curr": {"func" : "str(vm.get_disk_utilization_root_img()/(1024*1024)) + ' MB'"},
  62. "root-max": {"func" : "str(vm.get_root_img_sz()/(1024*1024)) + ' MB'"},
  63. "root-util": {"func" : "str(vm.get_disk_utilization_root_img()*100/vm.get_root_img_sz()) + '%' if vm.get_root_img_sz() != 0 else '-'"},
  64. "label" : {"func" : "vm.label.name"},
  65. "kernel" : {"func" : "('*' if vm.uses_default_kernel else '') + str(vm.kernel) if hasattr(vm, 'kernel') else 'n/a'"},
  66. "kernelopts" : {"func" : "('*' if vm.uses_default_kernelopts else '') + str(vm.kernelopts) if hasattr(vm, 'kernelopts') else 'n/a'"},
  67. "on" : {"func" : "'*' if vm.is_running() else ''"},
  68. "last backup" : {"func": "str(vm.backup_timestamp.date()) if "
  69. "vm.backup_timestamp else '-'"},
  70. }
  71. def main():
  72. usage = "usage: %prog [options] <vm-name>"
  73. parser = OptionParser (usage)
  74. parser.add_option ("-n", "--network", dest="network",
  75. action="store_true", default=False,
  76. help="Show network addresses assigned to VMs")
  77. parser.add_option ("-c", "--cpu", dest="cpu",
  78. action="store_true", default=False,
  79. help="Show CPU load")
  80. parser.add_option ("-m", "--mem", dest="mem",
  81. action="store_true", default=False,
  82. help="Show memory usage")
  83. parser.add_option ("-d", "--disk", dest="disk",
  84. action="store_true", default=False,
  85. help="Show VM disk utilization statistics")
  86. parser.add_option ("-k", "--kernel", dest="kernel",
  87. action="store_true", default=False,
  88. help="Show VM kernel options")
  89. parser.add_option ("-i", "--ids", dest="ids",
  90. action="store_true", default=False,
  91. help="Show Qubes and Xen id#s")
  92. parser.add_option("-b", "--last-backup", dest="backup",
  93. action="store_true", default=False,
  94. help="Show date of last VM backup")
  95. parser.add_option("--raw-list", dest="raw_list",
  96. action="store_true", default=False,
  97. help="List only VM names one per line")
  98. (options, args) = parser.parse_args ()
  99. qvm_collection = QubesVmCollection()
  100. qvm_collection.lock_db_for_reading()
  101. qvm_collection.load()
  102. qvm_collection.unlock_db()
  103. if options.raw_list:
  104. for vm in qvm_collection.values():
  105. print vm.name
  106. return
  107. fields_to_display = ["name", "on", "state", "updbl", "type", "template", "netvm", "label" ]
  108. cpu_usages = None
  109. if (options.ids):
  110. fields_to_display += ["qid", "xid"]
  111. if (options.cpu):
  112. qhost = QubesHost()
  113. (measure_time, cpu_usages) = qhost.measure_cpu_usage()
  114. fields_to_display += ["cpu"]
  115. if (options.mem):
  116. fields_to_display += ["mem"]
  117. if options.backup:
  118. fields_to_display += ["last backup"]
  119. if (options.network):
  120. if 'template' in fields_to_display:
  121. fields_to_display.remove ("template")
  122. fields_to_display += ["ip", "ip back", "gateway/DNS"]
  123. if (options.disk):
  124. if 'template' in fields_to_display:
  125. fields_to_display.remove ("template")
  126. if 'netvm' in fields_to_display:
  127. fields_to_display.remove ("netvm")
  128. fields_to_display += ["priv-curr", "priv-max", "root-curr", "root-max", "disk" ]
  129. if (options.kernel):
  130. fields_to_display += ["kernel", "kernelopts" ]
  131. vms_list = [vm for vm in qvm_collection.values()]
  132. if len(args) > 0:
  133. vms_list = [vm for vm in vms_list if vm.name in args]
  134. no_vms = len (vms_list)
  135. vms_to_display = []
  136. # Frist, the NetVMs...
  137. for netvm in vms_list:
  138. if netvm.is_netvm():
  139. vms_to_display.append (netvm)
  140. # Now, the AppVMs without template (or with template not included in the list)...
  141. for appvm in vms_list:
  142. if appvm.is_appvm() and not appvm.is_template() and \
  143. (appvm.template is None or appvm.template not in vms_list):
  144. vms_to_display.append (appvm)
  145. # Now, the template, and all its AppVMs...
  146. for tvm in vms_list:
  147. if tvm.is_template():
  148. vms_to_display.append (tvm)
  149. for vm in vms_list:
  150. if (vm.is_appvm() or vm.is_disposablevm()) and \
  151. vm.template and vm.template.qid == tvm.qid:
  152. vms_to_display.append(vm)
  153. assert len(vms_to_display) == no_vms
  154. # First calculate the maximum width of each field we want to display
  155. # also collect data to display
  156. for f in fields_to_display:
  157. fields[f]["max_width"] = len(f)
  158. data_to_display = []
  159. for vm in vms_to_display:
  160. data_row = {}
  161. for f in fields_to_display:
  162. if vm.qid == 0 and (f.startswith('priv-') or f.startswith('root-') or f == 'disk'):
  163. data_row[f] = 'n/a'
  164. else:
  165. data_row[f] = str(eval(fields[f]["func"]))
  166. l = len(data_row[f])
  167. if l > fields[f]["max_width"]:
  168. fields[f]["max_width"] = l
  169. data_to_display.append(data_row)
  170. try:
  171. vm.verify_files()
  172. except QubesException as err:
  173. print >> sys.stderr, "WARNING: VM '{0}' has corrupted files!".format(vm.name)
  174. # XXX: For what?
  175. total_width = 0;
  176. for f in fields_to_display:
  177. total_width += fields[f]["max_width"]
  178. # Display the header
  179. s = ""
  180. for f in fields_to_display:
  181. fmt="{{0:-^{0}}}-+".format(fields[f]["max_width"] + 1)
  182. s += fmt.format('-')
  183. print s
  184. s = ""
  185. for f in fields_to_display:
  186. fmt="{{0:>{0}}} |".format(fields[f]["max_width"] + 1)
  187. s += fmt.format(f)
  188. print s
  189. s = ""
  190. for f in fields_to_display:
  191. fmt="{{0:-^{0}}}-+".format(fields[f]["max_width"] + 1)
  192. s += fmt.format('-')
  193. print s
  194. # ... and the actual data
  195. for row in data_to_display:
  196. s = ""
  197. for f in fields_to_display:
  198. fmt="{{0:>{0}}} |".format(fields[f]["max_width"] + 1)
  199. s += fmt.format(row[f])
  200. print s
  201. main()