def check_vm_sanity(host_id=0): vmcheck = [] vm_list = [] if host_id == 0: hosts = db(db.host.status == HOST_STATUS_UP).select() else: hosts = db(db.host.id == host_id).select() for host in hosts: try: logger.info('Starting sanity check for host %s' % (host.host_name)) #Get list of the domains(running and not running) on the hypervisor domains = get_host_domains(host.host_ip.private_ip) for dom in domains: try: domain_name = dom.name() vm = db((db.vm_data.vm_identity == domain_name) & (db.vm_data.status.belongs( VM_STATUS_RUNNING, VM_STATUS_SUSPENDED, VM_STATUS_SHUTDOWN))).select().first() vm_state = dom.info()[0] status = vminfo_to_state(vm_state) if (vm): if (vm.host_id != host.id): vmcheck.append({ 'vm_id': vm.id, 'host': host.host_name, 'host_id': host.id, 'vmname': vm.vm_name, 'status': status, 'message': 'Moved from ' + vm.host_id.host_name + ' to ' + host.host_name, 'operation': 'None' }) #Bad VMs #If VM has been migrated to another host; Host information updated db(db.vm_data.vm_identity == domain_name).update( host_id=host.id) else: vmcheck.append({ 'vm_id': vm.id, 'host': host.host_name, 'host_id': host.id, 'vmname': vm.vm_name, 'status': status, 'message': 'VM is on expected host ' + vm.host_id.host_name, 'operation': 'None' }) #Good VMs if vm_state_map[vm_state] != vm.status: #If not in sync with actual state of VM; status of VM updated in DB db(db.vm_data.vm_identity == domain_name).update( status=vm_state_map[vm_state]) #Adding into vm_event_log about the vm details db.vm_event_log.insert(vm_id=vm.id, attribute='VM Status', requester_id=SYSTEM_USER, old_value=get_vm_status( vm.status), new_value=get_vm_status( vm_state_map[vm_state])) vm_list.append(vm.vm_identity) elif vm_state != VIR_DOMAIN_CRASHED: vmcheck.append({ 'host': host.host_name, 'host_id': host.id, 'vmname': dom.name(), 'status': status, 'message': 'Orphan, VM is not in database', 'operation': 'Orphan' }) #Orphan VMs except Exception: log_exception() if (vm): vmcheck.append({ 'vmname': vm.vm_name, 'host': 'Unknown', 'host_id': '0', 'status': 'Unknown', 'message': 'Some Error Occurred', 'operation': 'Error' }) except: pass db.commit() if host_id == 0: db_vms = db( db.vm_data.status.belongs(VM_STATUS_RUNNING, VM_STATUS_SUSPENDED, VM_STATUS_SHUTDOWN)).select() for db_vm in db_vms: if (db_vm.vm_identity not in vm_list): vmcheck.append({ 'vmname': db_vm.vm_identity, 'host': db_vm.host_id.host_name, 'host_id': db_vm.host_id, 'status': 'Undefined', 'message': 'VM not found', 'operation': 'Undefined' }) return vmcheck
def check_vm_sanity(host_id = 0): """ Checks if database information of VM status is in sync with ground reality. In case of discrepancy database is updated. - For each host, get list of the domains(running and not running). - If VM has migrated to another host; host information updated in database. - If VM not in sync with actual state of VM; status of VM updated in DB. - If VM is not present in database, it is marked Orphan. - If VM is not found on any of the hosts, it is marked Undefined. """ vmcheck=[] vm_list = [] if host_id == 0: hosts=db(db.host.status == HOST_STATUS_UP).select() else: hosts=db(db.host.id == host_id).select() for host in hosts: try: logger.info('Starting sanity check for host %s' %(host.host_name)) #Get list of the domains(running and not running) on the hypervisor domains = get_host_domains(host.host_ip.private_ip) for dom in domains: try: domain_name = dom.name() vm = db((db.vm_data.vm_identity == domain_name) & (db.vm_data.status.belongs(VM_STATUS_RUNNING, VM_STATUS_SUSPENDED, VM_STATUS_SHUTDOWN))).select().first() vm_state = dom.info()[0] status = vminfo_to_state(vm_state) if(vm): if(vm.host_id != host.id): vmcheck.append({'vm_id':vm.id, 'host':host.host_name, 'host_id':host.id, 'vmname':vm.vm_name, 'status':status, 'message':'Moved from '+vm.host_id.host_name+' to '+host.host_name, 'operation':'None'})#Bad VMs #If VM has been migrated to another host; Host information updated db(db.vm_data.vm_identity == domain_name).update(host_id = host.id) else: vmcheck.append({'vm_id':vm.id, 'host':host.host_name, 'host_id':host.id, 'vmname':vm.vm_name, 'status':status, 'message':'VM is on expected host '+vm.host_id.host_name, 'operation':'None'})#Good VMs if vm_state_map[vm_state] != vm.status: logger.info("vm_identity="+str(domain_name)+" vm_state_map[vm_state]="+str(vm_state_map[vm_state])+"and vm.status is" + str(vm.status)) #If not in sync with actual state of VM; status of VM updated in DB db(db.vm_data.vm_identity == domain_name).update(status = vm_state_map[vm_state]) #Adding into vm_event_log about the vm details db.vm_event_log.insert(vm_id = vm.id, attribute = 'VM Status', requester_id = SYSTEM_USER, old_value = get_vm_status(vm.status), new_value = get_vm_status(vm_state_map[vm_state])) vm_list.append(vm.vm_identity) elif vm_state != VIR_DOMAIN_CRASHED: vmcheck.append({'host':host.host_name, 'host_id':host.id, 'vmname':dom.name(), 'status':status, 'message':'Orphan, VM is not in database', 'operation':'Orphan'})#Orphan VMs except Exception: log_exception() if(vm): vmcheck.append({'vmname':vm.vm_name, 'host':'Unknown', 'host_id':'0', 'status':'Unknown', 'message':'Some Error Occurred', 'operation':'Error'}) except:pass db.commit() if host_id == 0: db_vms=db(db.vm_data.status.belongs(VM_STATUS_RUNNING, VM_STATUS_SUSPENDED, VM_STATUS_SHUTDOWN)).select() for db_vm in db_vms: if(db_vm.vm_identity not in vm_list): vmcheck.append({'vmname':db_vm.vm_identity, 'host':db_vm.host_id.host_name, 'host_id':db_vm.host_id, 'status':'Undefined', 'message':'VM not found', 'operation':'Undefined'}) return vmcheck