qvm-ls 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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": "'Tpl' if vm.is_template() else \
  39. ('Proxy' if vm.is_proxyvm() else \
  40. (' Net' if vm.is_netvm() else \
  41. ('HVM' if vm.type == 'HVM' else '')))"},
  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. qvm_collection[vm.template.qid].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. }
  69. def main():
  70. usage = "usage: %prog [options] <vm-name>"
  71. parser = OptionParser (usage)
  72. parser.add_option ("-n", "--network", dest="network",
  73. action="store_true", default=False,
  74. help="Show network addresses assigned to VMs")
  75. parser.add_option ("-c", "--cpu", dest="cpu",
  76. action="store_true", default=False,
  77. help="Show CPU load")
  78. parser.add_option ("-m", "--mem", dest="mem",
  79. action="store_true", default=False,
  80. help="Show memory usage")
  81. parser.add_option ("-d", "--disk", dest="disk",
  82. action="store_true", default=False,
  83. help="Show VM disk utilization statistics")
  84. parser.add_option ("-k", "--kernel", dest="kernel",
  85. action="store_true", default=False,
  86. help="Show VM kernel options")
  87. parser.add_option ("-i", "--ids", dest="ids",
  88. action="store_true", default=False,
  89. help="Show Qubes and Xen id#s")
  90. (options, args) = parser.parse_args ()
  91. qvm_collection = QubesVmCollection()
  92. qvm_collection.lock_db_for_reading()
  93. qvm_collection.load()
  94. qvm_collection.unlock_db()
  95. fields_to_display = ["name", "on", "state", "updbl", "type", "template", "netvm", "label" ]
  96. cpu_usages = None
  97. if (options.ids):
  98. fields_to_display += ["qid", "xid"]
  99. if (options.cpu):
  100. qhost = QubesHost()
  101. (measure_time, cpu_usages) = qhost.measure_cpu_usage()
  102. fields_to_display += ["cpu"]
  103. if (options.mem):
  104. fields_to_display += ["mem"]
  105. if (options.network):
  106. if 'template' in fields_to_display:
  107. fields_to_display.remove ("template")
  108. fields_to_display += ["ip", "ip back", "gateway/DNS"]
  109. if (options.disk):
  110. if 'template' in fields_to_display:
  111. fields_to_display.remove ("template")
  112. if 'netvm' in fields_to_display:
  113. fields_to_display.remove ("netvm")
  114. fields_to_display += ["priv-curr", "priv-max", "root-curr", "root-max", "disk" ]
  115. if (options.kernel):
  116. fields_to_display += ["kernel", "kernelopts" ]
  117. vms_list = [vm for vm in qvm_collection.values()]
  118. no_vms = len (vms_list)
  119. vms_to_display = []
  120. # Frist, the NetVMs...
  121. for netvm in vms_list:
  122. if netvm.is_netvm():
  123. vms_to_display.append (netvm)
  124. # Now, the AppVMs without template...
  125. for appvm in vms_list:
  126. if appvm.is_appvm() and appvm.template is None:
  127. vms_to_display.append (appvm)
  128. # Now, the template, and all its AppVMs...
  129. for tvm in vms_list:
  130. if tvm.is_template():
  131. vms_to_display.append (tvm)
  132. for vm in vms_list:
  133. if (vm.is_appvm() or vm.is_disposablevm()) and \
  134. vm.template and vm.template.qid == tvm.qid:
  135. vms_to_display.append(vm)
  136. assert len(vms_to_display) == no_vms
  137. # First calculate the maximum width of each field we want to display
  138. # also collect data to display
  139. for f in fields_to_display:
  140. fields[f]["max_width"] = len(f)
  141. data_to_display = []
  142. for vm in vms_to_display:
  143. data_row = {}
  144. for f in fields_to_display:
  145. if vm.qid == 0 and (f.startswith('priv-') or f.startswith('root-') or f == 'disk'):
  146. data_row[f] = 'n/a'
  147. else:
  148. data_row[f] = str(eval(fields[f]["func"]))
  149. l = len(data_row[f])
  150. if l > fields[f]["max_width"]:
  151. fields[f]["max_width"] = l
  152. data_to_display.append(data_row)
  153. try:
  154. vm.verify_files()
  155. except QubesException as err:
  156. print >> sys.stderr, "WARNING: VM '{0}' has corrupted files!".format(vm.name)
  157. # XXX: For what?
  158. total_width = 0;
  159. for f in fields_to_display:
  160. total_width += fields[f]["max_width"]
  161. # Display the header
  162. s = ""
  163. for f in fields_to_display:
  164. fmt="{{0:-^{0}}}-+".format(fields[f]["max_width"] + 1)
  165. s += fmt.format('-')
  166. print s
  167. s = ""
  168. for f in fields_to_display:
  169. fmt="{{0:>{0}}} |".format(fields[f]["max_width"] + 1)
  170. s += fmt.format(f)
  171. print s
  172. s = ""
  173. for f in fields_to_display:
  174. fmt="{{0:-^{0}}}-+".format(fields[f]["max_width"] + 1)
  175. s += fmt.format('-')
  176. print s
  177. # ... and the actual data
  178. for row in data_to_display:
  179. s = ""
  180. for f in fields_to_display:
  181. fmt="{{0:>{0}}} |".format(fields[f]["max_width"] + 1)
  182. s += fmt.format(row[f])
  183. print s
  184. main()