Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.5 kB
1
Indexable
Never
def filter_log_data(input_file):
    print(f"Filtering data in {input_file}")
    # Define the list of important words
    important_words = ['unauthorized', 'error', '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', '-filtered.txt')}"

    # 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)
    #os.remove(input_file)
    
    # Return the output filename
    return output_file.name + '.gz'