qvm-ls 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/usr/bin/python2.6
  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 QubesException
  24. from optparse import OptionParser
  25. fields = {
  26. "qid": {"func": "vm.qid"},
  27. "name": {"func": "('=>' if qvm_collection.get_default_template_vm() is not None\
  28. and vm.qid == qvm_collection.get_default_template_vm().qid else '')\
  29. + ('[' if vm.is_templete() else '')\
  30. + ('{' if vm.is_netvm() else '')\
  31. + vm.name \
  32. + (']' if vm.is_templete() else '')\
  33. + ('}' if vm.is_netvm() else '')"},
  34. "type": {"func": "'Tpl' if vm.is_templete() else \
  35. (' Net' if vm.is_netvm() else '')"},
  36. "updbl" : {"func": "'Yes' if vm.is_updateable() else ''"},
  37. "template": {"func": "'n/a' if vm.is_templete() or vm.is_netvm() else\
  38. qvm_collection[vm.template_vm.qid].name"},
  39. "netvm": {"func": "'n/a' if vm.is_netvm() else\
  40. ('*' if vm.uses_default_netvm else '') +\
  41. qvm_collection[vm.netvm_vm.qid].name\
  42. if vm.netvm_vm is not None else '-'"},
  43. "ip" : {"func": "vm.ip"},
  44. "netmask" : {"func": "vm.netmask"},
  45. "gateway" : {"func": "vm.gateway"},
  46. "xid" : {"func" : "vm.get_xid() if vm.is_running() else '-'"},
  47. "mem" : {"func" : "(str(vm.get_mem()/1024/1024) + ' MB') if vm.is_running() else '-'"},
  48. "cpu" : {"func" : "round (vm.get_cpu_total_load(), 1) if vm.is_running() else '-'"},
  49. "disk": {"func" : "str(vm.get_disk_utilization()/(1024*1024)) + ' MB'"},
  50. "state": {"func" : "vm.get_power_state()"},
  51. "priv-curr": {"func" : "str(vm.get_disk_utilization_private_img()/(1024*1024)) + ' MB'"},
  52. "priv-max": {"func" : "str(vm.get_private_img_sz()/(1024*1024)) + ' MB'"},
  53. "priv-util": {"func" : "str(vm.get_disk_utilization_private_img()*100/vm.get_private_img_sz()) + '%' if vm.get_private_img_sz() != 0 else '-'"},
  54. "root-curr": {"func" : "str(vm.get_disk_utilization_root_img()/(1024*1024)) + ' MB'"},
  55. "root-max": {"func" : "str(vm.get_root_img_sz()/(1024*1024)) + ' MB'"},
  56. "root-util": {"func" : "str(vm.get_disk_utilization_root_img()*100/vm.get_root_img_sz()) + '%' if vm.get_root_img_sz() != 0 else '-'"},
  57. "label" : {"func" : "vm.label.name"},
  58. "on" : {"func" : "'*' if vm.is_running() else ''"}
  59. }
  60. def main():
  61. usage = "usage: %prog [options] <vm-name>"
  62. parser = OptionParser (usage)
  63. parser.add_option ("-n", "--network", dest="network",
  64. action="store_true", default=False,
  65. help="Show network addresses assigned to VMs")
  66. parser.add_option ("-c", "--cpu", dest="cpu",
  67. action="store_true", default=False,
  68. help="Show CPU load")
  69. parser.add_option ("-m", "--mem", dest="mem",
  70. action="store_true", default=False,
  71. help="Show memory usage")
  72. parser.add_option ("-d", "--disk", dest="disk",
  73. action="store_true", default=False,
  74. help="Show VM disk utilization statistics")
  75. parser.add_option ("-i", "--ids", dest="ids",
  76. action="store_true", default=False,
  77. help="Show Qubes and Xen id#s")
  78. (options, args) = parser.parse_args ()
  79. qvm_collection = QubesVmCollection()
  80. qvm_collection.lock_db_for_reading()
  81. qvm_collection.load()
  82. qvm_collection.unlock_db()
  83. fields_to_display = ["name", "on", "state", "updbl", "type", "template", "netvm", "label" ]
  84. if (options.ids):
  85. fields_to_display += ["qid", "xid"]
  86. if (options.cpu):
  87. fields_to_display += ["cpu"]
  88. if (options.mem):
  89. fields_to_display += ["mem"]
  90. if (options.network):
  91. fields_to_display.remove ("template")
  92. fields_to_display += ["ip", "netmask", "gateway"]
  93. if (options.disk):
  94. fields_to_display.remove ("template")
  95. fields_to_display.remove ("netvm")
  96. fields_to_display += ["priv-curr", "priv-max", "root-curr", "root-max", "disk" ]
  97. vms_list = [vm for vm in qvm_collection.values()]
  98. no_vms = len (vms_list)
  99. vms_to_display = []
  100. # Frist, the NetVMs...
  101. for netvm in vms_list:
  102. if netvm.is_netvm():
  103. vms_to_display.append (netvm)
  104. # Now, the template, and all its AppVMs...
  105. for tvm in vms_list:
  106. if tvm.is_templete():
  107. vms_to_display.append (tvm)
  108. for appvm in vms_list:
  109. if appvm.is_appvm() and appvm.template_vm.qid == tvm.qid:
  110. vms_to_display.append(appvm)
  111. assert len(vms_to_display) == no_vms
  112. # First calculate the maximum width of each field we want to display
  113. total_width = 0;
  114. for f in fields_to_display:
  115. fields[f]["max_width"] = len(f)
  116. for vm in vms_to_display:
  117. l = len(str(eval(fields[f]["func"])))
  118. if l > fields[f]["max_width"]:
  119. fields[f]["max_width"] = l
  120. total_width += fields[f]["max_width"]
  121. # Display the header
  122. s = ""
  123. for f in fields_to_display:
  124. fmt="{{0:-^{0}}}-+".format(fields[f]["max_width"] + 1)
  125. s += fmt.format('-')
  126. print s
  127. s = ""
  128. for f in fields_to_display:
  129. fmt="{{0:>{0}}} |".format(fields[f]["max_width"] + 1)
  130. s += fmt.format(f)
  131. print s
  132. s = ""
  133. for f in fields_to_display:
  134. fmt="{{0:-^{0}}}-+".format(fields[f]["max_width"] + 1)
  135. s += fmt.format('-')
  136. print s
  137. # ... and the actual data
  138. for vm in vms_to_display:
  139. s = ""
  140. for f in fields_to_display:
  141. fmt="{{0:>{0}}} |".format(fields[f]["max_width"] + 1)
  142. s += fmt.format(eval(fields[f]["func"]))
  143. print s
  144. try:
  145. vm.verify_files()
  146. except QubesException as err:
  147. print "WARNING: VM '{0}' has corrupted files!".format(vm.name)
  148. main()