def get_cpu_mem_used_in_dc(dc_id): ''' 获取指定机房下所有host的CPU和MEM的使用情况 :param dc_id: :return: ''' all_hosts_data = get_hosts_of_datacenter(dc_id) all_cpu_used = 0 all_cpu_unused = 0 all_cpu = 0 all_mem_used = 0 all_mem_unused = 0 all_mem = 0 for _host in all_hosts_data: _host_used = get_host_used(_host, expire=False) if _host_used: _cpu_used_per = float(_host_used.get( 'current_cpu_used', 0)) if _host_used.get('current_cpu_used') else 0.0 _cpu_core = float(_host_used.get('cpu_core', 0)) _cpu_used = int(_cpu_used_per / 100 * _cpu_core) all_cpu_used += _cpu_used all_cpu_unused += (_cpu_core - _cpu_used) all_cpu += _cpu_core _mem_used_per = float(_host_used.get('current_mem_used', 0)) _mem_size = float(_host_used.get('mem_size', 0)) _mem_used = int(_mem_used_per / 100 * _mem_size) all_mem_used += _mem_used all_mem_unused += (_mem_size - _mem_used) all_mem += _mem_size if all_cpu == 0: all_cpu_used_per = 0 all_cpu_unused_per = 0 else: all_cpu_used_per = int(float(all_cpu_used) / all_cpu * 100) all_cpu_unused_per = 100 - all_cpu_used_per if all_mem == 0: all_mem_used_per = 0 all_mem_unused_per = 0 else: all_mem_used_per = int(float(all_mem_used) / all_mem * 100) all_mem_unused_per = 100 - all_mem_used_per return { 'cpu_used': all_cpu_used, 'cpu_used_per': all_cpu_used_per, 'cpu_unused': all_cpu_unused, 'cpu_unused_per': all_cpu_unused_per, 'mem_used': all_mem_used, 'mem_used_per': all_mem_used_per, 'mem_unused': all_mem_unused, 'mem_unused_per': all_mem_unused_per }
def get_host_detail(host_id): ''' 获取host详情 :param host_id: :return: ''' if not host_id: logging.info('no host_id when get host detail') return json_helper.format_api_resp(code=ErrorCode.PARAM_ERR) host_db = host_s.HostService().get_host_info(host_id) if not host_db: logging.info('host %s no exist in db when get host detail', host_id) return json_helper.format_api_resp(code=ErrorCode.SYS_ERR, msg="不存在该物理机信息") host = host_info.HostInfo() host.displayname = host_db['displayname'] host.ipaddress = host_db['ipaddress'] host.manage_ip = host_db['manage_ip'] host.status = host_db['status'] host.hold_mem_gb = host_db['hold_mem_gb'] host.sn = host_db['sn'] host.ostype = host_db['ostype'] # host性能数据 host_perform_info = host_s_s.get_host_used(host_db, expire=False) if host_perform_info: host.cpu_core = host_perform_info.get('cpu_core', 'unknown') host.current_cpu_used = host_perform_info.get('current_cpu_used', 'unknown') host.mem_size = host_perform_info.get('mem_size', 'unknown') host.current_mem_used = host_perform_info.get('current_mem_used', 'unknown') host.disk_size = host_perform_info.get('disk_size', 'unknown') host.current_disk_used = host_perform_info.get('current_disk_used', 'unknown') host.collect_time = host_perform_info['collect_time'] host.start_time = host_perform_info['start_time'] host.images = host_perform_info.get('images', 'unknown') host.libvirt_status = host_perform_info.get('libvirt_status', 'unknown') host.libvirt_port = host_perform_info.get('libvirt_port', 'unknown') host_level_info = host_s.get_level_info(host_db['id']) if host_level_info: host.hostpool = host_level_info['hostpool_name'] host.net_area = host_level_info['net_area_name'] host.datacenter = host_level_info['datacenter_name'] host.instance_nums = ins_s.get_instances_nums_in_host(host_db['id']) # todo: libvirtd状态、服务端口,物理机开机时间、os版本、镜像数量、卷存储路径、序列号 return json_helper.format_api_resp(code=ErrorCode.SUCCESS, data=host.to_json())
def get_host_local_image(host_id): host_db = host_s.HostService().get_host_info(host_id) if not host_db: message = '该物理机不存在' return False,message host = host_info.HostInfo() host_perform_info = host_s_s.get_host_used(host_db, expire=False) if not host_perform_info: message = '获取镜像信息失败' return False,message host.collect_time = host_perform_info['collect_time'] host.start_time = host_perform_info['start_time'] host_images = host_perform_info['images'] if host_images == '': local_image_list = [] else: local_image_list = host_images.split(' ') return True,local_image_list
def _confirm_migrate_speed(speed_limit, host_s): ''' 确定源主机的迁移速度 :param speed_limit: :param host_s: :return: ''' # 目标主机的性能数据 host_used_d = host_s_s.get_host_used(host_s) # 迁移前限速,根据网络使用率调整迁移速率为(网络带宽-当前使用上传带宽)* 0.8 # 总带宽 - 已使用带宽 = 剩余带宽,然后只使用80%,这相当最大理论值 net_speed = (int(host_used_d["net_size"]) - int(host_used_d["current_net_tx_used"]) * int(host_used_d["net_size"])) * 0.8 # 取两者最小值 migrate_speed = int( net_speed) if int(speed_limit) > int(net_speed) else int(speed_limit) # 迁移速度最小确保20MByte = 160 Mbit migrate_speed = migrate_speed if migrate_speed > 160 else 160 return True, migrate_speed
def _check_mem_allocation(instance_id, new_mem, old_mem): ''' 物理机内存不超分(包括已经分配出去的内存) :param instance_id: :param new_mem: :param old_mem: :return: ''' host_data = ins_s.get_host_of_instance(instance_id) if not host_data: return False host_all_data = host_s_s.get_host_used(host_data) if host_all_data: # 保留内存GB -> MB hold_mem = host_data['hold_mem_gb'] * 1024 mem_size = host_all_data['mem_size'] allocate_mem = host_s.get_vm_assign_mem_of_host(host_data['id']) # 增量 < 总量 - 已分配(flavor的mem) - 保留 if long(new_mem) - long(old_mem) < long(mem_size) - long(allocate_mem) - long(hold_mem): return True return False
def get_cpu_mem_used(all_hosts_data, hostpool_id, req_mem_mb=4096, req_disk_gb=50, cpu_used=80, mem_used=95, disk_used=70): ''' 获取批量host的CPU和MEM的使用情况 :param all_hosts_data: :param hostpool_id: :param req_mem_mb: :param req_disk_gb: :param cpu_used: :param mem_used: :param disk_used: :return: ''' all_cpu_used = 0 all_cpu_unused = 0 all_cpu = 0 all_mem_used = 0 all_mem_unused = 0 all_mem = 0 all_mem_assign = 0 all_hold_mem = 0 all_available_create_vm_num = 0 all_host_available = 0 for _host in all_hosts_data: _host_used = get_host_used(_host, expire=False) if _host_used: _cpu_used_per = float(_host_used.get( 'current_cpu_used', 0)) if _host_used.get('current_cpu_used') else 0.0 _cpu_core = float(_host_used.get('cpu_core', 0)) _cpu_used = float(_cpu_used_per / 100 * _cpu_core) all_cpu_used += _cpu_used all_cpu_unused += (_cpu_core - _cpu_used) all_cpu += _cpu_core _mem_used_per = float(_host_used.get('current_mem_used', 0)) _mem_size = float(_host_used.get('mem_size', 0)) _mem_used = float(_mem_used_per / 100 * _mem_size) all_mem_used += _mem_used all_mem_unused += (_mem_size - _mem_used) all_mem += _mem_size all_mem_assign += int(_host_used.get('assign_mem', 0)) all_hold_mem += int(_host_used.get('hold_mem_gb', 0)) * 1024 # 以下为计算每台物理机可以创建的虚拟机数量 _available_mem, _available_disk, _available_create_vm = get_host_available_mem_disk( _host_used, int(req_mem_mb), int(req_disk_gb) + 80, max_disk=2000) # TODO 每台host最多创建50台vm _available_create_vm = _available_create_vm if _available_create_vm < 50 else 50 if _host_used.get('type_status', '0') == '0': if float(_host_used.get('current_cpu_used', 0)) < cpu_used and \ float(_host_used.get('current_mem_used', 0)) < mem_used and \ float(_host_used.get('current_disk_used', 0)) < disk_used: if _available_create_vm > 0: all_host_available += 1 all_available_create_vm_num += _available_create_vm __least_host_num = hostpool_s.HostPoolService().get_least_host_num( hostpool_id) if __least_host_num: if all_host_available < int(__least_host_num): all_available_create_vm_num = 0 if all_cpu == 0: all_cpu_used_per = 0 all_cpu_unused_per = 0 else: all_cpu_used_per = float('%.2f' % (float(all_cpu_used) / all_cpu * 100)) all_cpu_unused_per = 100 - all_cpu_used_per if all_mem == 0: all_mem_used_per = 0 all_mem_unused_per = 0 else: all_mem_used_per = float('%.2f' % (float(all_mem_used) / all_mem * 100)) all_mem_unused_per = 100 - all_mem_used_per if all_mem_assign == 0: all_mem_assign_per = 0 else: all_mem_assign_per = float('%.2f' % (float(all_mem_assign) / (all_mem - all_hold_mem) * 100)) return { 'cpu_all': all_cpu, 'cpu_used': all_cpu_used, 'cpu_used_per': all_cpu_used_per, 'cpu_unused': all_cpu_unused, 'cpu_unused_per': all_cpu_unused_per, 'mem_all': all_mem, 'mem_used': all_mem_used, 'mem_used_per': all_mem_used_per, 'mem_unused': all_mem_unused, 'mem_unused_per': all_mem_unused_per, 'assign_mem': all_mem_assign, 'assign_mem_per': all_mem_assign_per, 'available_create_vm_num': all_available_create_vm_num }
def get_host_mem_cpu_disk_used_multithreading(_host, req_mem_mb, req_disk_gb, all_hosts_data_after_filter): ''' 多线程获取每台物理机资源使用信息 :param _host: :param req_mem_mb: :param req_disk_gb: :param all_hosts_data_after_filter: :return: ''' global ALL_CPU_USED global ALL_CPU_UNUSED global ALL_CPU global ALL_MEM_USED global ALL_MEM_UNUSED global ALL_MEM global ALL_DISK_USED global ALL_DISK_UNUSED global ALL_DISK global ALL_CPU_ASSIGN global ALL_MEM_ASSIGN global ALL_DISK_ASSIGN global ALL_MEM_AVAILABLE global ALL_DISK_AVAILABLE global ALL_AVAILABLE_CREATE_VM global ALL_HOST_PERFORMANCE_DATAS global ALL_AVAILABLE_HOST_NUM global HOST_CAPACITY_CALCULATE_THREADINGLOCK _host_used = get_host_used(_host, expire=False) if _host_used: HOST_CAPACITY_CALCULATE_THREADINGLOCK.acquire() _cpu_used_per = float( _host_used.get('current_cpu_used', 0)) if _host_used.get('current_cpu_used') else 0.0 _cpu_core = float(_host_used.get('cpu_core', 0)) _cpu_used = float(_cpu_used_per / 100 * _cpu_core) ALL_CPU_USED += _cpu_used ALL_CPU_UNUSED += (_cpu_core - _cpu_used) ALL_CPU += _cpu_core _mem_used_per = float(_host_used.get('current_mem_used', 0)) _mem_size = float(_host_used.get('mem_size', 0)) _mem_used = float(_mem_used_per / 100 * _mem_size) ALL_MEM_USED += _mem_used ALL_MEM_UNUSED += (_mem_size - _mem_used) ALL_MEM += _mem_size _disk_used_per = float(_host_used.get('current_disk_used', 0)) _disk_size = float(_host_used.get('disk_size', 0)) _disk_used = float(_disk_used_per / 100 * _disk_size) ALL_DISK_USED += _disk_used ALL_DISK_UNUSED += (_disk_size - _disk_used) ALL_DISK += _disk_size # 已分配给虚拟机的内存、cpu和磁盘 ALL_MEM_ASSIGN += _host_used.get('assign_mem', 0) ALL_CPU_ASSIGN += _host_used.get('assign_vcpu', 0) ALL_DISK_ASSIGN += _host_used.get('assign_disk', 0) # 获取物理机可用的内存、磁盘大小,保证最大内存使用率不大于max_mem和最大磁盘使用率不大于max_disk _available_mem, _available_disk, _available_create_vm = get_host_available_mem_disk( _host_used, int(req_mem_mb), int(req_disk_gb) + 80, max_disk=2000) # TODO 每台host最多创建50台vm _available_create_vm = _available_create_vm if _available_create_vm < 50 else 50 ALL_MEM_AVAILABLE += _available_mem ALL_DISK_AVAILABLE += _available_disk # 物理机过滤,如果过滤后物理机数量为0或者小于集群最小物理机数量,则该集群可创建虚拟机数量为0 # hosts_after_filter = host_s_s.filter_hosts(all_hosts_data) host_after_filter_available = False for host_after_filter in all_hosts_data_after_filter: if host_after_filter['ipaddress'] == _host_used['ipaddress']: host_after_filter_available = True break if host_after_filter_available: _host_list = [] _host_list.append(_host_used) get_available_vm_number = host_s_s.count_host_available_vm_number( int(req_mem_mb), int(req_disk_gb) + 80, max_disk=2000) ret_hosts_1 = filter(get_available_vm_number, _host_list) hosts_vm_number_filter = host_s_s.host_available_create_vm_number_filter( ) ret_hosts_2 = filter(hosts_vm_number_filter, ret_hosts_1) if _host_used.get('type_status', '0') == '0' and len(ret_hosts_2) > 0: ALL_AVAILABLE_HOST_NUM += 1 ALL_AVAILABLE_CREATE_VM += _available_create_vm # 拼装每一台物理机信息 _host_data = { 'hostname': _host_used.get('hostname', ''), 'ip': _host_used.get('ipaddress', ''), 'host_total_capacity': { 'vcpu': int(_host_used.get('cpu_core', 0)), 'mem_mb': int(_host_used.get('mem_size', 0)), 'disk_gb': int(_host_used.get('disk_size', 0)) }, 'host_assign_capacity': { 'mem_mb': int(_host_used.get('assign_mem', 0)), 'vcpu': int(_host_used.get('assign_vcpu', 0)), 'disk_gb': int(_host_used.get('assign_disk', 0)) }, 'host_available_capacity': { 'mem_mb': _available_mem, 'vcpu': int(_cpu_core - _cpu_used), 'disk_gb': _available_disk, 'vm_count': int(_available_create_vm) }, 'host_performance': { 'vcpu_usage': int(_host_used.get('current_cpu_used', 0)), 'mem_usage': int(_host_used.get('current_mem_used', 0)), 'disk_usage': int(_host_used.get('current_disk_used', 0)) }, 'net_speed': int(_host_used.get('net_size', 0)), 'state': '' } ALL_HOST_PERFORMANCE_DATAS.append(_host_data) HOST_CAPACITY_CALCULATE_THREADINGLOCK.release()