2010-04-05 20:58:57 +02:00
|
|
|
#!/usr/bin/python2.6
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
from qubes.qubes import QubesVmCollection
|
2011-06-01 23:44:06 +02:00
|
|
|
from qubes.qubes import QubesHost
|
2010-04-05 20:58:57 +02:00
|
|
|
from qubes.qubes import QubesException
|
|
|
|
from optparse import OptionParser
|
2011-10-07 21:56:58 +02:00
|
|
|
import sys
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
fields = {
|
|
|
|
"qid": {"func": "vm.qid"},
|
|
|
|
|
2012-03-09 11:01:20 +01:00
|
|
|
"name": {"func": "('=>' if qvm_collection.get_default_template() is not None\
|
|
|
|
and vm.qid == qvm_collection.get_default_template().qid else '')\
|
2011-03-05 15:13:31 +01:00
|
|
|
+ ('[' if vm.is_template() else '')\
|
2010-09-21 15:59:22 +02:00
|
|
|
+ ('<' if vm.is_disposablevm() else '')\
|
2010-04-05 20:58:57 +02:00
|
|
|
+ ('{' if vm.is_netvm() else '')\
|
|
|
|
+ vm.name \
|
2011-03-05 15:13:31 +01:00
|
|
|
+ (']' if vm.is_template() else '')\
|
2010-09-21 15:59:22 +02:00
|
|
|
+ ('>' if vm.is_disposablevm() else '')\
|
2010-04-05 20:58:57 +02:00
|
|
|
+ ('}' if vm.is_netvm() else '')"},
|
|
|
|
|
2011-03-05 15:13:31 +01:00
|
|
|
"type": {"func": "'Tpl' if vm.is_template() else \
|
2011-03-11 02:44:11 +01:00
|
|
|
('Proxy' if vm.is_proxyvm() else \
|
2011-02-09 21:21:41 +01:00
|
|
|
(' Net' if vm.is_netvm() else ''))"},
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2012-03-09 11:28:06 +01:00
|
|
|
"updbl" : {"func": "'Yes' if vm.updateable else ''"},
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2011-03-06 17:06:45 +01:00
|
|
|
"template": {"func": "'n/a' if vm.is_template() else\
|
2012-03-09 11:01:20 +01:00
|
|
|
('None' if vm.template is None else\
|
|
|
|
qvm_collection[vm.template.qid].name)"},
|
2010-04-05 20:58:57 +02:00
|
|
|
|
2011-03-06 17:06:45 +01:00
|
|
|
"netvm": {"func": "'n/a' if vm.is_netvm() and not vm.is_proxyvm() else\
|
2010-04-05 20:58:57 +02:00
|
|
|
('*' if vm.uses_default_netvm else '') +\
|
2012-03-04 21:59:02 +01:00
|
|
|
qvm_collection[vm.netvm.qid].name\
|
|
|
|
if vm.netvm is not None else '-'"},
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
"ip" : {"func": "vm.ip"},
|
|
|
|
"netmask" : {"func": "vm.netmask"},
|
|
|
|
"gateway" : {"func": "vm.gateway"},
|
|
|
|
|
|
|
|
"xid" : {"func" : "vm.get_xid() if vm.is_running() else '-'"},
|
|
|
|
|
2011-06-01 23:44:06 +02:00
|
|
|
"mem" : {"func" : "(str(vm.get_mem()/1024) + ' MB') if vm.is_running() else '-'"},
|
|
|
|
"cpu" : {"func" : "round (cpu_usages[vm.get_xid()]['cpu_usage'], 1) if vm.is_running() else '-'"},
|
2010-04-05 20:58:57 +02:00
|
|
|
"disk": {"func" : "str(vm.get_disk_utilization()/(1024*1024)) + ' MB'"},
|
|
|
|
"state": {"func" : "vm.get_power_state()"},
|
|
|
|
|
|
|
|
"priv-curr": {"func" : "str(vm.get_disk_utilization_private_img()/(1024*1024)) + ' MB'"},
|
|
|
|
"priv-max": {"func" : "str(vm.get_private_img_sz()/(1024*1024)) + ' MB'"},
|
|
|
|
"priv-util": {"func" : "str(vm.get_disk_utilization_private_img()*100/vm.get_private_img_sz()) + '%' if vm.get_private_img_sz() != 0 else '-'"},
|
|
|
|
|
|
|
|
"root-curr": {"func" : "str(vm.get_disk_utilization_root_img()/(1024*1024)) + ' MB'"},
|
|
|
|
"root-max": {"func" : "str(vm.get_root_img_sz()/(1024*1024)) + ' MB'"},
|
|
|
|
"root-util": {"func" : "str(vm.get_disk_utilization_root_img()*100/vm.get_root_img_sz()) + '%' if vm.get_root_img_sz() != 0 else '-'"},
|
|
|
|
|
|
|
|
"label" : {"func" : "vm.label.name"},
|
|
|
|
|
|
|
|
"on" : {"func" : "'*' if vm.is_running() else ''"}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
usage = "usage: %prog [options] <vm-name>"
|
|
|
|
parser = OptionParser (usage)
|
|
|
|
|
|
|
|
parser.add_option ("-n", "--network", dest="network",
|
|
|
|
action="store_true", default=False,
|
|
|
|
help="Show network addresses assigned to VMs")
|
|
|
|
|
|
|
|
parser.add_option ("-c", "--cpu", dest="cpu",
|
|
|
|
action="store_true", default=False,
|
|
|
|
help="Show CPU load")
|
|
|
|
|
|
|
|
parser.add_option ("-m", "--mem", dest="mem",
|
|
|
|
action="store_true", default=False,
|
|
|
|
help="Show memory usage")
|
|
|
|
|
|
|
|
parser.add_option ("-d", "--disk", dest="disk",
|
|
|
|
action="store_true", default=False,
|
|
|
|
help="Show VM disk utilization statistics")
|
|
|
|
|
|
|
|
parser.add_option ("-i", "--ids", dest="ids",
|
|
|
|
action="store_true", default=False,
|
|
|
|
help="Show Qubes and Xen id#s")
|
|
|
|
|
|
|
|
|
|
|
|
(options, args) = parser.parse_args ()
|
|
|
|
|
|
|
|
qvm_collection = QubesVmCollection()
|
|
|
|
qvm_collection.lock_db_for_reading()
|
|
|
|
qvm_collection.load()
|
|
|
|
qvm_collection.unlock_db()
|
|
|
|
|
|
|
|
fields_to_display = ["name", "on", "state", "updbl", "type", "template", "netvm", "label" ]
|
|
|
|
|
2011-06-01 23:44:06 +02:00
|
|
|
cpu_usages = None
|
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
if (options.ids):
|
|
|
|
fields_to_display += ["qid", "xid"]
|
|
|
|
|
|
|
|
if (options.cpu):
|
2011-06-01 23:44:06 +02:00
|
|
|
qhost = QubesHost()
|
|
|
|
(measure_time, cpu_usages) = qhost.measure_cpu_usage()
|
2010-04-05 20:58:57 +02:00
|
|
|
fields_to_display += ["cpu"]
|
|
|
|
|
|
|
|
if (options.mem):
|
|
|
|
fields_to_display += ["mem"]
|
|
|
|
|
|
|
|
if (options.network):
|
2012-03-09 17:06:50 +01:00
|
|
|
if 'template' in fields_to_display:
|
|
|
|
fields_to_display.remove ("template")
|
2010-04-05 20:58:57 +02:00
|
|
|
fields_to_display += ["ip", "netmask", "gateway"]
|
|
|
|
|
|
|
|
if (options.disk):
|
2012-03-09 17:06:50 +01:00
|
|
|
if 'template' in fields_to_display:
|
|
|
|
fields_to_display.remove ("template")
|
|
|
|
if 'netvm' in fields_to_display:
|
|
|
|
fields_to_display.remove ("netvm")
|
2010-04-05 20:58:57 +02:00
|
|
|
fields_to_display += ["priv-curr", "priv-max", "root-curr", "root-max", "disk" ]
|
|
|
|
|
|
|
|
|
|
|
|
vms_list = [vm for vm in qvm_collection.values()]
|
|
|
|
no_vms = len (vms_list)
|
|
|
|
vms_to_display = []
|
|
|
|
# Frist, the NetVMs...
|
|
|
|
for netvm in vms_list:
|
|
|
|
if netvm.is_netvm():
|
|
|
|
vms_to_display.append (netvm)
|
|
|
|
|
2011-03-16 23:45:02 +01:00
|
|
|
# Now, the AppVMs without template...
|
|
|
|
for appvm in vms_list:
|
2012-03-09 11:01:20 +01:00
|
|
|
if appvm.is_appvm() and appvm.template is None:
|
2011-03-16 23:45:02 +01:00
|
|
|
vms_to_display.append (appvm)
|
|
|
|
|
2010-04-05 20:58:57 +02:00
|
|
|
# Now, the template, and all its AppVMs...
|
|
|
|
for tvm in vms_list:
|
2011-03-05 15:13:31 +01:00
|
|
|
if tvm.is_template():
|
2010-04-05 20:58:57 +02:00
|
|
|
vms_to_display.append (tvm)
|
2010-09-21 15:59:22 +02:00
|
|
|
for vm in vms_list:
|
2011-03-16 23:45:02 +01:00
|
|
|
if (vm.is_appvm() or vm.is_disposablevm()) and \
|
2012-03-09 11:01:20 +01:00
|
|
|
vm.template and vm.template.qid == tvm.qid:
|
2010-09-21 15:59:22 +02:00
|
|
|
vms_to_display.append(vm)
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
assert len(vms_to_display) == no_vms
|
|
|
|
|
|
|
|
# First calculate the maximum width of each field we want to display
|
|
|
|
total_width = 0;
|
|
|
|
for f in fields_to_display:
|
|
|
|
fields[f]["max_width"] = len(f)
|
|
|
|
for vm in vms_to_display:
|
|
|
|
l = len(str(eval(fields[f]["func"])))
|
|
|
|
if l > fields[f]["max_width"]:
|
|
|
|
fields[f]["max_width"] = l
|
|
|
|
total_width += fields[f]["max_width"]
|
|
|
|
|
|
|
|
|
|
|
|
# Display the header
|
|
|
|
s = ""
|
|
|
|
for f in fields_to_display:
|
|
|
|
fmt="{{0:-^{0}}}-+".format(fields[f]["max_width"] + 1)
|
|
|
|
s += fmt.format('-')
|
|
|
|
print s
|
|
|
|
s = ""
|
|
|
|
for f in fields_to_display:
|
|
|
|
fmt="{{0:>{0}}} |".format(fields[f]["max_width"] + 1)
|
|
|
|
s += fmt.format(f)
|
|
|
|
print s
|
|
|
|
s = ""
|
|
|
|
for f in fields_to_display:
|
|
|
|
fmt="{{0:-^{0}}}-+".format(fields[f]["max_width"] + 1)
|
|
|
|
s += fmt.format('-')
|
|
|
|
print s
|
|
|
|
|
|
|
|
# ... and the actual data
|
|
|
|
for vm in vms_to_display:
|
|
|
|
s = ""
|
|
|
|
for f in fields_to_display:
|
|
|
|
fmt="{{0:>{0}}} |".format(fields[f]["max_width"] + 1)
|
|
|
|
s += fmt.format(eval(fields[f]["func"]))
|
|
|
|
print s
|
|
|
|
|
|
|
|
try:
|
|
|
|
vm.verify_files()
|
|
|
|
except QubesException as err:
|
2011-10-07 21:56:58 +02:00
|
|
|
print >> sys.stderr, "WARNING: VM '{0}' has corrupted files!".format(vm.name)
|
2010-04-05 20:58:57 +02:00
|
|
|
|
|
|
|
main()
|