Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
5.5 kB
1
Indexable
Never
def purge_logs():
    vm_details = get_vm_details()

    for vm in vm_details:
        vm_id = vm['vm_id']
        vm_ip = vm['vm_ip']
        syslog_file = vm['syslog_file_location']
        retention_period = get_retention_period(vm_id)
        bookmark_index = get_bookmark_index(vm_ip)

        print("VM:", vm_id)
        print("VM_IP:", vm_ip)
        bookmark_record=str(vm_ip) + "_bookmark_log"
        print("Bookmark record ",bookmark_record)
        print("Syslog File Location:", syslog_file)
        print("Retention Period:", retention_period)
        print("Bookmark Index:", bookmark_index)
        #print("Purging Logs...")

        try:
            bookmark_line = linecache.getline(syslog_file, int(bookmark_index)).strip()
            print("Bookmark Line:", bookmark_line)
        except Exception as e:
            print("Exception while reading line from file:", e)
            logging.error("Exception while reading line from file %s ",e)
            return None

        if not bookmark_line:
            print("Bookmark line not found.")
            logging.error("Bookmark line not found.")
            continue
            
        bookmark_timestamp = get_timestamp_from_line(bookmark_line)
        print("Bookmark Timestamp:", bookmark_timestamp)
        logging.info("Bookmark Timestamp %s ", bookmark_timestamp)

        # Calculate the purging date (retention_period days before the bookmark timestamp)
        bookmark_date = datetime.datetime.strptime(bookmark_timestamp, "%Y-%m-%dT%H:%M:%S+00:00").date()
        print("Bookmark Date:", bookmark_date)
        logging.info("Bookmark Date %s ", bookmark_date)

        current_date = datetime.datetime.now().date()
        print("Current Date:", current_date)
        logging.info("Current Date %s ", current_date)

        purging_date = current_date - datetime.timedelta(days=retention_period)
        print("Purging Date:", purging_date)
        logging.info("Purging Date : %s ", purging_date)
        
        if purging_date < bookmark_date:
            print("Performing Purging..")
            ## Function call to perform purging
            perform_purging(syslog_file, purging_date,bookmark_record)
        else:
            print("Purging date greater than Bookmark Date, no purging will be done")
            logging.info("Purging date greater than Bookmark Date, no purging will be done")

            
def perform_purging(syslog_file, purging_date, bookmark_record):
    # Perform the log purging
    temp_file = syslog_file + ".tmp"  # Create a temporary file

    # Search for the purging date in the syslog file
    purging_line_number = None
    with open(syslog_file, "r") as file:
        print("Opening Syslog File...")
        for line_number, line in enumerate(file, 1):
            if str(purging_date) in line:
                purging_line_number = line_number
                print("Purging Line Number:", purging_line_number)
                logging.info("Purging Line Number %s ",purging_line_number )
                break

    if purging_line_number is not None:
        # Delete lines before the purging date
        with open(syslog_file, "r") as input_file, open(temp_file, "w") as output_file:
            for line_number, line in enumerate(input_file, 1):
                if line_number >= purging_line_number:
                    output_file.write(line)

        # Replace the original file with the temporary file
        shutil.move(temp_file, syslog_file)

        lines_purged = purging_line_number - 1
        print("Purging Completed")
        logging.info("Purging Completed")
        print("Lines Purged:", lines_purged)
        logging.info("Lines Purged: %s ", lines_purged)
        print("Printing First Two Lines:")
        with open(syslog_file, "r") as file:
            print(file.readline())
            print(file.readline())

        # Update the bookmark index in the bookmark file
        update_bookmark_index(bookmark_record, lines_purged)
    else:
        print("Purging is not needed.")
        logging.info("Purging is not needed.")

def update_bookmark_index(bookmark_record, lines_purged):

    # Read the current bookmark index from the bookmark file
    bookmark_index = None
    lines = []

    with open(bookmark_properties_file, "r") as file:
        for line in file:
            if bookmark_record in line:
                bookmark_index = int(line.split("=")[1].strip())
            else:
                lines.append(line.strip())

    if bookmark_index is not None:
        # Calculate the updated bookmark index
        updated_index = bookmark_index - lines_purged

        # Update the bookmark file with the new index
        updated_bookmark_rec = f"{bookmark_record}={updated_index}"
        with open(bookmark_properties_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()

        print("Bookmark index updated:", updated_index)
    else:
        print(f"Failed to update bookmark index for {bookmark_record}. Make sure the record exists in the bookmark file.")
        
        

purge_logs()