diff --git a/vm-placement.py b/vm-placement.py index be60b93..f4cace6 100755 --- a/vm-placement.py +++ b/vm-placement.py @@ -16,7 +16,9 @@ from novaclient.v1_1 import client import neat.common as common +import neat.globals.manager as manager from neat.config import * +import sys def vm_hostname(vm): @@ -25,23 +27,42 @@ def vm_hostname(vm): config = read_and_validate_config([DEFAILT_CONFIG_PATH, CONFIG_PATH], REQUIRED_FIELDS) -hosts = common.parse_compute_hosts(config['compute_hosts']) +db = manager.init_db(config['sql_connection']) nova = client.Client(config['os_admin_user'], config['os_admin_password'], config['os_admin_tenant_name'], config['os_auth_url'], service_type="compute") +hosts = common.parse_compute_hosts(config['compute_hosts']) +hosts_cpu_total, hosts_ram_total = db.select_host_characteristics() +hosts_to_vms = manager.vms_by_hosts(nova, hosts) +vms = [item for sublist in hosts_to_vms.values() for item in sublist] +vms_names = sorted((str(nova.servers.get(vm).human_id), vm) for vm in vms) +vms_cpu_usage = db.select_last_cpu_mhz_for_vms() +for vm in vms: + if not vm in vms_cpu_usage: + vms_cpu_usage[vm] = 0 +vms_ram_usage = manager.vms_ram_limit(nova, vms) -placement = dict((str(vm.human_id), - (vm.status, - vm_hostname(vm), - nova.hosts.get(vm_hostname(vm))[2].memory_mb)) - for vm in nova.servers.list()) +hosts_cpu_usage = {} +hosts_ram_usage = {} +for host, vms in hosts_to_vms.items(): + hosts_cpu_usage[host] = sum(vms_cpu_usage[x] for x in vms) + hosts_ram_usage[host] = manager.host_used_ram(nova, host) + -for vm in sorted(placement.keys()): - print '{0:10} {1:10} {2} [{3} MB used]'.format(vm, - placement[vm][0], - placement[vm][1], - placement[vm][2]) +for host, vms in hosts_to_vms.items(): + print '{0:10} {1:5d}/{2:5d} MHz {3:5d}/{4:5d} MB'.format( + host, + hosts_cpu_usage[host], + hosts_cpu_total[host], + hosts_ram_usage[host], + hosts_ram_total[host]) + for vm, uuid in vms_names: + if uuid in vms: + print ' {0:12} {1:5d} MHz {2:11d} MB'.format( + vm, + vms_cpu_usage[uuid], + vms_ram_usage[uuid])