2016-04-04 15:48:40 +02:00
|
|
|
#!/usr/bin/python2
|
|
|
|
# -*- encoding: utf8 -*-
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
2016-04-04 15:48:40 +02:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2016-04-04 15:48:40 +02:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2017-10-12 00:11:50 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
2016-04-04 15:48:40 +02:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
|
2016-04-04 15:48:40 +02:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
from qubes.qubes import QubesVmCollection
|
|
|
|
from qubes.qubes import QubesHost
|
|
|
|
from qubes.qubes import QubesException
|
|
|
|
from optparse import OptionParser
|
|
|
|
import sys
|
|
|
|
|
|
|
|
def main():
|
|
|
|
usage = "usage: %prog [options]"
|
|
|
|
parser = OptionParser (usage)
|
|
|
|
|
|
|
|
parser.add_option("--list", dest="list_top",
|
|
|
|
action="store_true", default=False,
|
|
|
|
help="n m : One line summary of top n vms with more than m cpu_time %")
|
|
|
|
|
|
|
|
(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", "cpu", "mem"]
|
|
|
|
|
|
|
|
cpu_usages = None
|
|
|
|
qhost = QubesHost()
|
|
|
|
(measure_time, cpu_usages) = qhost.measure_cpu_usage(qvm_collection)
|
|
|
|
|
|
|
|
vms_list = [vm for vm in qvm_collection.values() if vm.is_running()]
|
|
|
|
vms_list = sorted(vms_list, key= lambda vm: 1-cpu_usages[vm.get_xid()]['cpu_usage'])
|
|
|
|
|
|
|
|
no_vms = len (vms_list)
|
|
|
|
vms_to_display = vms_list
|
|
|
|
|
|
|
|
if options.list_top:
|
|
|
|
any_shown = False
|
|
|
|
ndisp = 3
|
|
|
|
cputh = 0
|
2016-04-04 15:57:29 +02:00
|
|
|
if len(args) > 0:
|
|
|
|
ndisp = int(args[0])
|
|
|
|
if len(args) > 1:
|
|
|
|
cputh = int(args[1])
|
2016-04-04 15:48:40 +02:00
|
|
|
|
|
|
|
for vm in vms_to_display[:ndisp]:
|
|
|
|
cpu = cpu_usages[vm.get_xid()]['cpu_usage']
|
|
|
|
if cpu > cputh:
|
|
|
|
any_shown = True
|
2016-04-04 15:57:29 +02:00
|
|
|
sys.stdout.write("%d %s, " % (cpu, vm.name))
|
2016-04-04 15:48:40 +02:00
|
|
|
|
2016-04-04 15:57:29 +02:00
|
|
|
if any_shown:
|
2016-04-04 15:48:40 +02:00
|
|
|
sys.stdout.write(" ... | ")
|
|
|
|
|
|
|
|
totalMem = 0
|
|
|
|
dom0mem = 0
|
2016-04-04 15:57:29 +02:00
|
|
|
for vm in vms_to_display:
|
2016-04-04 15:48:40 +02:00
|
|
|
if not vm.name == "dom0":
|
2016-04-04 15:57:29 +02:00
|
|
|
totalMem += vm.get_mem()
|
2016-04-04 15:48:40 +02:00
|
|
|
else:
|
|
|
|
dom0mem = vm.get_mem()
|
|
|
|
totalMem /= 1024.0 * 1024.0
|
|
|
|
dom0mem /= 1024.0 * 1024.0
|
|
|
|
sys.stdout.write("%.1f G + %.1f G" % (totalMem, dom0mem))
|
2016-04-04 15:57:29 +02:00
|
|
|
return
|
2016-04-04 15:48:40 +02:00
|
|
|
|
2016-04-04 15:57:29 +02:00
|
|
|
max_width = { 'name': 0, 'cpu': 0, 'mem': 0 }
|
2016-04-04 15:48:40 +02:00
|
|
|
data_to_display = []
|
|
|
|
for vm in vms_to_display:
|
|
|
|
data_row = {}
|
|
|
|
data_row['name'] = vm.name
|
|
|
|
max_width['name'] = max(max_width['name'], len(data_row['name']))
|
|
|
|
data_row['cpu'] = "%.1f" % (cpu_usages[vm.get_xid()]['cpu_usage'])
|
|
|
|
max_width['cpu'] = max(max_width['cpu'], len(data_row['cpu']))
|
|
|
|
data_row['mem'] = "%d" % (vm.get_mem() / (1024.0))
|
|
|
|
max_width['mem'] = max(max_width['mem'], len(data_row['mem']))
|
|
|
|
data_to_display.append(data_row)
|
2016-04-04 15:57:29 +02:00
|
|
|
|
2016-04-04 15:48:40 +02:00
|
|
|
# Display the header
|
|
|
|
s = ""
|
|
|
|
for f in fields_to_display:
|
|
|
|
fmt="{{0:-^{0}}}-+".format(max_width[f] + 1)
|
|
|
|
s += fmt.format('-')
|
|
|
|
print s
|
|
|
|
s = ""
|
|
|
|
for f in fields_to_display:
|
|
|
|
fmt="{{0:>{0}}} |".format(max_width[f] + 1)
|
2016-04-04 15:57:29 +02:00
|
|
|
s += fmt.format(f)
|
2016-04-04 15:48:40 +02:00
|
|
|
print s
|
|
|
|
s = ""
|
|
|
|
for f in fields_to_display:
|
|
|
|
fmt="{{0:-^{0}}}-+".format(max_width[f] + 1)
|
|
|
|
s += fmt.format('-')
|
|
|
|
print s
|
|
|
|
|
|
|
|
# ... and the actual data
|
|
|
|
for row in data_to_display:
|
|
|
|
s = ""
|
|
|
|
for f in fields_to_display:
|
|
|
|
fmt="{{0:>{0}}} |".format(max_width[f] + 1)
|
2016-04-04 15:57:29 +02:00
|
|
|
s += fmt.format(row[f])
|
2016-04-04 15:48:40 +02:00
|
|
|
print s
|
|
|
|
|
|
|
|
main()
|