qvm-ls 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. (options, args) = parser.parse_args ()
  96. qvm_collection = QubesVmCollection()
  97. qvm_collection.lock_db_for_reading()
  98. qvm_collection.load()
  99. qvm_collection.unlock_db()
  100. fields_to_display = ["name", "on", "state", "updbl", "type", "template", "netvm", "label" ]
  101. cpu_usages = None
  102. if (options.ids):
  103. fields_to_display += ["qid", "xid"]
  104. if (options.cpu):
  105. qhost = QubesHost()
  106. (measure_time, cpu_usages) = qhost.measure_cpu_usage()
  107. fields_to_display += ["cpu"]
  108. if (options.mem):
  109. fields_to_display += ["mem"]
  110. if options.backup:
  111. fields_to_display += ["last backup"]
  112. if (options.network):
  113. if 'template' in fields_to_display:
  114. fields_to_display.remove ("template")
  115. fields_to_display += ["ip", "ip back", "gateway/DNS"]
  116. if (options.disk):
  117. if 'template' in fields_to_display:
  118. fields_to_display.remove ("template")
  119. if 'netvm' in fields_to_display:
  120. fields_to_display.remove ("netvm")
  121. fields_to_display += ["priv-curr", "priv-max", "root-curr", "root-max", "disk" ]
  122. if (options.kernel):
  123. fields_to_display += ["kernel", "kernelopts" ]
  124. vms_list = [vm for vm in qvm_collection.values()]
  125. if len(args) > 0:
  126. vms_list = [vm for vm in vms_list if vm.name in args]
  127. no_vms = len (vms_list)
  128. vms_to_display = []
  129. # Frist, the NetVMs...
  130. for netvm in vms_list:
  131. if netvm.is_netvm():
  132. vms_to_display.append (netvm)
  133. # Now, the AppVMs without template (or with template not included in the list)...
  134. for appvm in vms_list:
  135. if appvm.is_appvm() and not appvm.is_template() and \
  136. (appvm.template is None or appvm.template not in vms_list):
  137. vms_to_display.append (appvm)
  138. # Now, the template, and all its AppVMs...
  139. for tvm in vms_list:
  140. if tvm.is_template():
  141. vms_to_display.append (tvm)
  142. for vm in vms_list:
  143. if (vm.is_appvm() or vm.is_disposablevm()) and \
  144. vm.template and vm.template.qid == tvm.qid:
  145. vms_to_display.append(vm)
  146. assert len(vms_to_display) == no_vms
  147. # First calculate the maximum width of each field we want to display
  148. # also collect data to display
  149. for f in fields_to_display:
  150. fields[f]["max_width"] = len(f)
  151. data_to_display = []
  152. for vm in vms_to_display:
  153. data_row = {}
  154. for f in fields_to_display:
  155. if vm.qid == 0 and (f.startswith('priv-') or f.startswith('root-') or f == 'disk'):
  156. data_row[f] = 'n/a'
  157. else:
  158. data_row[f] = str(eval(fields[f]["func"]))
  159. l = len(data_row[f])
  160. if l > fields[f]["max_width"]:
  161. fields[f]["max_width"] = l
  162. data_to_display.append(data_row)
  163. try:
  164. vm.verify_files()
  165. except QubesException as err:
  166. print >> sys.stderr, "WARNING: VM '{0}' has corrupted files!".format(vm.name)
  167. # XXX: For what?
  168. total_width = 0;
  169. for f in fields_to_display:
  170. total_width += fields[f]["max_width"]
  171. # Display the header
  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. s = ""
  178. for f in fields_to_display:
  179. fmt="{{0:>{0}}} |".format(fields[f]["max_width"] + 1)
  180. s += fmt.format(f)
  181. print s
  182. s = ""
  183. for f in fields_to_display:
  184. fmt="{{0:-^{0}}}-+".format(fields[f]["max_width"] + 1)
  185. s += fmt.format('-')
  186. print s
  187. # ... and the actual data
  188. for row in data_to_display:
  189. s = ""
  190. for f in fields_to_display:
  191. fmt="{{0:>{0}}} |".format(fields[f]["max_width"] + 1)
  192. s += fmt.format(row[f])
  193. print s
  194. main()