Untitled

 avatar
unknown
plain_text
2 years ago
4.4 kB
5
Indexable
vm_list_cache = None

def get_VMList():
    global vm_list_cache
    if vm_list_cache is None:
        try:
            with open(agent_file,'rt') as file:
                for line in file:
                    if 'VMList' in line:
                        vm_list = str(line.split('='))
                        vm_list = re.findall(r'\d+', vm_list)
                        vm_list_cache = list(map(int, vm_list))
                        break
        except FileNotFoundError:
            print(f"{agent_file} not found")                
        except Exception as e:
            print(f"An error occurred while reading the file: {str(e)}")
            vm_list_cache = []
    return vm_list_cache

print("The VM List is : ",get_VMList())


#Get number of lines to be copied
def get_log_block_size():
    try:
        with open(agent_file,'rt') as file:
            for line in file:
                if 'vmmetricskpi.log_block' in line:
                    print("Getting the Log Block size")
                    log_block_size=line.split('=')
                    #print(log_block_size,type(log_block_size))
                    no_of_lines=log_block_size[1]
                    break
    except Exception as e:
        print(f"An error occurred while reading the file: {str(e)}")                
                    
    return no_of_lines

print("Number of Lines : ",get_log_block_size())


# Checking Current Timestamp
print("Getting Current Timestamp")
current_time = time.strftime("%Y-%m-%d-%H-%M-%S")
print("Current Time: ",current_time)

def get_vm_details():
    All_VM_Details = []
    for vm in VMList:
        vm_id=vm
        try:
            VM_Details = {vm_id:{'vm_ip':[],'vm_output_log_filename':[],'vm_log_enable':[],'vm_log_filename':[],'bookmark':[],'bookmark_ip':[],'bookmark_log':[]}};
            with open(agent_file,'rt') as file:
                for line in file:
                    if str(vm) + ".privatip" in line:
                        vm_ip = str(line.split('='))
                        vm_ip = re.findall( r'[0-9]+(?:\.[0-9]+){3}', vm_ip)
                        vm_ip = list(map(str, vm_ip))
                        vm_ip = vm_ip[0]
                        VM_Details[vm_id]['vm_ip'].append(vm_ip)
                        bookmark_ip = vm_ip
                        VM_Details[vm_id]['bookmark_ip'].append(bookmark_ip)
                        bookmark_log=bookmark_ip + "_bookmark_log="
                        VM_Details[vm_id]['bookmark_log'].append(bookmark_log)

                    if str(vm)+".vmmetricskpi.output_log.filename" in line:
                        vm_output_log_filename =line.split('=')
                        vm_output_log_filename= list(map(str,vm_output_log_filename))
                        vm_output_log_filename = vm_output_log_filename[1].strip()
                        VM_Details[vm_id]['vm_output_log_filename'].append(vm_output_log_filename)

                    if str(vm)+".vmmetricskpi.log_enable" in line:
                        vm_log_enable =line.split('=')
                        vm_log_enable= list(map(str,vm_log_enable))
                        vm_log_enable = vm_log_enable[1].strip()
                        VM_Details[vm_id]['vm_log_enable'].append(vm_log_enable)

                    if str(vm)+".vmmetricskpi.log.filename" in line:
                        vm_log_filename =line.split('=')
                        vm_log_filename= list(map(str,vm_log_filename))
                        vm_log_filename = vm_log_filename[1].strip()
                        bookmark=vm_log_filename.split("-")
                        bookmark= list(map(str,bookmark))
                        bookmark = bookmark[1].strip()
                        VM_Details[vm_id]['vm_log_filename'].append(vm_log_filename)
                        VM_Details[vm_id]['bookmark'].append(bookmark)
        except FileNotFoundError:
            print(f"{agent_file} not found")
        except Exception as e:
            print(f"Failed to get details for VM {vm}: {e}")
            continue
                
        All_VM_Details.append(VM_Details)
        all_vm_details =dict()
        all_vm_details = zip(VMList, All_VM_Details)
        all_vm_details = dict(all_vm_details)
    return all_vm_details

print("VM Details : \n ",get_vm_details())  
Editor is loading...