Untitled
unknown
plain_text
2 years ago
7.0 kB
4
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) logging.error("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) logging.info("Content for nth line %s",last_line_output) except Exception as e: print("Exception while reading line from file:", e) logging.error("Exception while reading line from file:%s",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) logging.error("Exception while copying lines to file: %s",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) logging.error("Exception while updating bookmark file %s",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') output_file_path = f"{output_folder}{new_output_file_name}" os.rename(output_file_name, output_file_path) try: with open(output_file_path, 'rb') as input_file, gzip.open(output_file_path + '.gz', 'wb') as output_file_gz: input_data = input_file.read() output_file_gz.write(input_data) os.remove(output_file_path) # Set permissions on the output file os.chmod(output_file_gz.name, 0o777) except Exception as e: print("Exception while compressing file:", e) logging.error("Exception while compressing file %s",e) # Return the filtered filename return filtered_filename def filter_log_data(input_file): print(f"Filtering data in {input_file} file.") # Take a list of important words from the file with open(important_words_file, 'r') as f: important_words_str = f.read().strip() important_words = [word.strip() for word in important_words_str.split(',')] #print("Important Words: ",important_words) # Define the output filename output_file_name = f"{output_folder}{input_file.replace('.txt', '-postgre.log')}" try: # Open the input and output files with open(input_file, 'r') as input_file, open(output_file_name, 'w') as output_file: # Loop through each line in the input file for line in input_file: # Check if any of the important words appear in the line if any(word in line.lower() for word in important_words): # If yes, write the line to the output file output_file.write(line) # Compress the output file and remove the uncompressed file with open(output_file_name, 'rb') as input_file, gzip.open(output_file_name + '.gz', 'wb') as output_file_gz: input_data = input_file.read() output_file_gz.write(input_data) os.remove(output_file_name) # Set permissions on the output file os.chmod(output_file_gz.name, 0o777) # Return the output filename return output_file.name + '.gz' except Exception as e: print(f"Error occurred: {str(e)}") logging.error("Error occured %s",e) return None
Editor is loading...