Untitled
unknown
plain_text
2 years ago
2.5 kB
5
Indexable
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("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) return None if not bookmark_line: print("Bookmark line not found.") continue bookmark_timestamp = get_timestamp_from_line(bookmark_line) print("Bookmark TImestamp : ",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") print("Bookmark Date : ",bookmark_date) purging_date = bookmark_date - datetime.timedelta(days=retention_period) print("Purging Date : ",purging_date) here in this line - # 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") print("Bookmark Date : ",bookmark_date) we are already getting this bookmark date then again below in the code- # Perform the log purging lines_to_keep = [] lines_purged = 0 # Counter for purged lines with open(syslog_file, "r") as file: for line in file: timestamp = get_timestamp_from_line(line) line_date = datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S+00:00") if line_date > purging_date: lines_to_keep.append(line) else: lines_purged += 1 why are we again opening syslog file and getting timestamp here in these lines- timestamp = get_timestamp_from_line(line) line_date = datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S+00:00")
Editor is loading...