Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
5.2 kB
1
Indexable
Never
Code-

import os
import itertools
import datetime
import linecache
import re

agent_file = "/APP_PROD/Dev_App/jar/properties/agent_purging.properties"
bookmark_properties_file = "/App/jar/KPI/bookmark_log.properties"


##Caching the VMList so that even if subsequent calls, it returns the cached VM list without needing to read the file again.
vm_list_cache = None

def get_VMList():
    global vm_list_cache
    if vm_list_cache is None:
        try:
            with open(agent_file, 'rt' ,encoding='latin-1') as file:
                for line in file:
                    if 'VMList' in line:
                        vm_list = re.findall(r'\d+', line)
                        vm_list_cache = list(map(int, vm_list))
                        break
        except FileNotFoundError:
            print(f"{agent_file} not found")
        except Exception as e:
            print(f"An error occurred while reading the file: {str(e)}")
            vm_list_cache = []
    return vm_list_cache

def get_vm_details():
    VMList = get_VMList()
    All_VM_Details = []
    for vm in VMList:
        vm_id = vm
        try:
            vm_ip = None
            with open(agent_file, 'rt',encoding='latin-1') as file:
                for line in file:
                    if str(vm_id) + ".privatip" in line:
                        vm_ip = re.findall(r'[0-9]+(?:\.[0-9]+){3}', line)
                        vm_ip = vm_ip[0] if vm_ip else None
                        break
            VM_Details = {'vm_id': vm_id, 'vm_ip': vm_ip}
            All_VM_Details.append(VM_Details)
        except FileNotFoundError:
            print(f"{agent_file} not found")
        except Exception as e:
            print(f"An error occurred while reading the file: {str(e)}")

    return All_VM_Details



def get_syslog_file_location(vm):
    with open(agent_file, encoding='latin-1') as file:
        for line in file:
            if line.startswith(str(vm) + ".vmmetricskpi.log.filename"):
                return line.split("=")[1].strip()
    return None

def get_retention_period(vm):
    with open(agent_file, encoding='latin-1') as file:
        for line in file:
            if line.startswith(str(vm) + ".vmmetricskpi.syslog_retention_period_in_days"):
                return int(line.split("=")[1].strip())
    return 0

def get_bookmark_index(vm_ip):
    with open(bookmark_properties_file, encoding='latin-1') as file:
        for line in file:
            if line.startswith(str(vm_ip) + "_bookmark_log"):
                return int(line.split("=")[1].strip())
    return 0



def get_timestamp_from_line(line):
    # Extract the timestamp from the line
    timestamp = line.split()[0]
    return timestamp



def purge_logs():
    vm_list = get_VMList()
    
    for vm in vm_list:
        syslog_file = get_syslog_file_location(vm)
        retention_period = get_retention_period(vm)
    
    vm_details = get_vm_details()
    for vm in vm_details:
        vm_id = vm['vm_id']
        vm_ip = vm['vm_ip']
        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...")

        # Get the bookmark line using linecache
        bookmark_line = linecache.getline(syslog_file, bookmark_index)
        if not bookmark_line:
            print("Bookmark line not found.")
            continue

        bookmark_timestamp = get_timestamp_from_line(bookmark_line)

        # Calculate the purging date (7 days before the bookmark timestamp)
        bookmark_date = datetime.datetime.strptime(bookmark_timestamp, "%Y-%m-%dT%H:%M:%S+00:00")
        purging_date = bookmark_date - datetime.timedelta(days=7)

        # 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

        # Write the lines to keep back to the syslog file
        with open(syslog_file, "w") as file:
            file.writelines(lines_to_keep)

        print("Purging Completed")
        print(f"Lines Purged: {lines_purged}")
        print("Printing First Two Lines:")
        with open(syslog_file, "r") as file:
            print(file.readline())
            print(file.readline())
        print()


Output-

VM: 23
Syslog File Location: /var/log/syslog/LONSTBYRDEV02-10.72.209.183.log
Retention Period: 7
Bookmark Index: 4212032
Purging Logs...

Bookmark line not found.

Although there is a proper line at 4212032 , but still it is not able to find the bookmark line and do further purging.