Untitled

 avatar
unknown
plain_text
2 years ago
1.7 kB
3
Indexable
def filter_log_data(input_file):
    print(f"Filtering data in {input_file} file.")
    
    # Define the list of important words
    important_words = ['unauthorized', 'error','cron','kernel error', 'OS error', 'rejected', 'warning', "error", "fail", "exception", "critical",
            "security", "authentication", "intrusion", "attack", "status", "performance", "uptime", "load", "config", "setting", "permission", "firewall",
            "debug", "trace", "stack"]
    
    # Define the output filename
    output_file = f"{input_file.replace('.txt', '-postgre.txt')}"
    
    try:
        # Open the input and output files
        with open(input_file, 'r') as input_file, open(output_file, '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)
                    
        # Set permissions on the output file
        os.chmod(output_file.name, 0o777)
        
        # 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)
        
        # Return the output filename
        return output_file.name + '.gz'
    
    except Exception as e:
        print(f"Error occurred: {str(e)}")
        return None
    
Editor is loading...