Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.9 kB
3
Indexable
def perform_functions_with_bookmark_record(bookmark_line, i):
    try:
        print("The function with perform_functions_with_bookmark_record is getting called.")
        vm_id = i
        print("VM ID :", vm_id)
        bookmark_record = bookmark_line
        number_of_lines = get_log_block_size()
        print("No of Lines to copy : ", number_of_lines)
        print("Bookmark Record", bookmark_record)
        all_vm_details = get_vm_details()
        vm_ip = all_vm_details[vm_id][vm_id]['vm_ip'][0]
        vm_log_filename = all_vm_details[vm_id][vm_id]['vm_log_filename'][0]
        print("VM Log FIle : ", vm_log_filename)
        vm_output_directory_loc = all_vm_details[vm_id][vm_id]['vm_output_log_filename'][0]
        print("VM Output Directory : ", vm_output_directory_loc)
        bookmark_log = all_vm_details[vm_id][vm_id]['bookmark_log'][0]
        syslog_record = bookmark_record.split(str(vm_ip) + '_bookmark_log=')
        syslog_record_no = syslog_record[1]
        print("Syslog Record number for VM {} is {}".format(i, syslog_record_no))
        copying_loglines_and_updating_bookmark(bookmark_record, syslog_record_no, i, number_of_lines)
    except Exception as e:
        print("Error in perform_functions_with_bookmark_record function:", e)
        

def copying_loglines_and_updating_bookmark(bookmark_record, syslog_record_no, i, log_block_size):
    print("The function with searching_copying_logs is getting called.")
    vm_id = i
    print("VM ID :", vm_id)
    bookmark_line = bookmark_record
    number_of_lines = log_block_size
    print("No of Lines to copy : ", number_of_lines)
    print("Bookmark Record", bookmark_record)
    old_line_number = syslog_record_no
    print('Old line number from bookmark file is : ', old_line_number)
    last_line_no = int(old_line_number) + int(number_of_lines)
    print('Value of New Line Number is :', last_line_no)
    all_vm_details = get_vm_details()
    vm_ip = all_vm_details[vm_id][vm_id]['vm_ip'][0]
    vm_log_filename = all_vm_details[vm_id][vm_id]['vm_log_filename'][0]
    syslog_record = str(vm_ip) + "_bookmark_log="
    print(syslog_record)
    print("VM Log FIle : ", vm_log_filename)
    
    # Finding the line with index as line number
    try:
        last_line_output = linecache.getline(vm_log_filename, last_line_no)
        print("Content for nth line :", last_line_output)
    except Exception as e:
        print("Exception while reading line from file:", e)
        return None
    
    filename = f"{vm_ip}-T{current_time}.txt"
    print(filename)
    
    X = int(old_line_number)  # Starting line number
    #print(f"Old Value is {X}")
    Y = int(last_line_no)  # Ending line number
    print(f'New Value is {Y}')
    
    try:
        with open(vm_log_filename, 'r') as input_file, open(filename, 'w') as output_file:
            # Using itertools.islice to copy lines between start and end indexes
            output_file.writelines(itertools.islice(input_file, X - 1, Y))
    except Exception as e:
        print("Exception while copying lines to file:", e)
        return None
    
    # Updating the last index in a Bookmark File
    print("Updating the last index in a Bookmark File")
    bookmark_record = syslog_record
    print(f"Bookmark Record for {i} is {bookmark_record}")
    print(f"Updating Bookmark record for {bookmark_record}")
    updated_bookmark_rec = f"{bookmark_record}{last_line_no}\n"
    print(updated_bookmark_rec)
    
    try:
        with open(bookmark_file, 'r+') as file:
            lines = file.readlines()
            file.seek(0)
            for line_no, line in enumerate(lines):
                if bookmark_record in line:
                    lines[line_no] = updated_bookmark_rec
            file.seek(0)
            file.writelines(lines)
            file.truncate()
    except Exception as e:
        print("Exception while updating bookmark file:", e)
        return None

    # Call filter_log_data to filter the copied data
    filtered_filename = filter_log_data(filename)
    
    # Compress the output file and remove the uncompressed file
    output_file_name = output_file.name
    new_output_file_name = output_file_name.replace('.txt', '-cassandra.log')
    os.rename(output_file_name, new_output_file_name)
    try:
        with open(new_output_file_name, 'rb') as input_file, gzip.open(new_output_file_name + '.gz', 'wb') as output_file_gz: 
            input_data = input_file.read()
            output_file_gz.write(input_data)
            os.remove(new_output_file_name)
            # Set permissions on the output file
            os.chmod(output_file_gz.name, 0o777)
            
    except Exception as e:
        print("Exception while compressing file:", e)
    
    # Return the filtered filename
    return filtered_filename