Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
5.7 kB
3
Indexable
Never
import itertools
import logging

## Logging some confirgurations ##############

# Set the logging level (options: DEBUG, INFO, WARNING, ERROR, CRITICAL)
logging.basicConfig(level=logging.INFO)
logging.basicConfig(level=logging.DEBUG)
logging.basicConfig(level=logging.WARNING)
logging.basicConfig(level=logging.ERROR)
logging.basicConfig(level=logging.CRITICAL)

# Define the log file path
log_file = '/home/CAPEUSER/Python/logs/dataflow_syslog_transfer_script.csv'

# Configure the logging format
logging.basicConfig(filename=log_file, format='%(asctime)s - %(levelname)s - %(message)s')


# Declaring Files
agent_file='/APP_PROD/Dev_App/jar/properties/agent.properties'
bookmark_file='/App/jar/KPI/bookmark_log.properties'
important_words_file='/home/CAPEUSER/Python/CAPE_SYSLOG_INTEGRATION/Code/important_words.txt'
output_folder='/var/log/syslog/'

#Program Execution
import time
start_time = time.time()


##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 = str(line.split('='))
                        vm_list = re.findall(r'\d+', vm_list)
                        vm_list_cache = list(map(int, vm_list))
                        break
        except FileNotFoundError:
            print(f"{agent_file} not found")
            logging.error("Agent file not found %s", agent_file)
        except Exception as e:
            print(f"An error occurred while reading the file: {str(e)}")
            logging.error("Error occured while reading the file %s", e)
            vm_list_cache = []
    return vm_list_cache

print("The VM List is : ",get_VMList())


#Get number of lines to be copied
def get_log_block_size():
    try:
        with open(agent_file,'rt',encoding='latin-1') as file:
            for line in file:
                if 'vmmetricskpi.log_block' in line:
                    print("Getting the Log Block size")
                    log_block_size=line.split('=')
                    #print(log_block_size,type(log_block_size))
                    no_of_lines=log_block_size[1]
                    break
    except Exception as e:
        print(f"An error occurred while reading the file: {str(e)}")

    return no_of_lines

print("Number of Lines : ",get_log_block_size())

# Checking Current Timestamp
print("Getting Current Timestamp")
current_time = time.strftime("%Y-%m-%d-%H-%M-%S")
print("Current Time: ",current_time)


def get_vm_details():
    VMList=get_VMList()
    All_VM_Details = []
    for vm in VMList:
        vm_id=vm
        try:
            VM_Details = {vm_id:{'vm_ip':[],'vm_output_log_filename':[],'vm_log_enable':[],'vm_log_filename':[],'bookmark':[],'bookmark_ip':[],'bookmark_log':[]}};
            with open(agent_file,'rt',encoding='latin-1') as file:
                for line in file:
                    if str(vm) + ".privatip" in line:
                        vm_ip = str(line.split('='))
                        vm_ip = re.findall( r'[0-9]+(?:\.[0-9]+){3}', vm_ip)
                        vm_ip = list(map(str, vm_ip))
                        vm_ip = vm_ip[0]
                        VM_Details[vm_id]['vm_ip'].append(vm_ip)
                        bookmark_ip = vm_ip
                        VM_Details[vm_id]['bookmark_ip'].append(bookmark_ip)
                        bookmark_log=bookmark_ip + "_bookmark_log="
                        VM_Details[vm_id]['bookmark_log'].append(bookmark_log)

                    if str(vm)+".vmmetricskpi.output_log.filename" in line:
                        vm_output_log_filename =line.split('=')
                        vm_output_log_filename= list(map(str,vm_output_log_filename))
                        vm_output_log_filename = vm_output_log_filename[1].strip()
                        VM_Details[vm_id]['vm_output_log_filename'].append(vm_output_log_filename)

                    if str(vm)+".vmmetricskpi.log_enable" in line:
                        vm_log_enable =line.split('=')
                        vm_log_enable= list(map(str,vm_log_enable))
                        vm_log_enable = vm_log_enable[1].strip()
                        VM_Details[vm_id]['vm_log_enable'].append(vm_log_enable)

                    if str(vm)+".vmmetricskpi.log.filename" in line:
                        vm_log_filename =line.split('=')
                        vm_log_filename= list(map(str,vm_log_filename))
                        vm_log_filename = vm_log_filename[1].strip()
                        bookmark=vm_log_filename.split("-")
                        bookmark= list(map(str,bookmark))
                        bookmark = bookmark[1].strip()
                        VM_Details[vm_id]['vm_log_filename'].append(vm_log_filename)
                        VM_Details[vm_id]['bookmark'].append(bookmark)
        except FileNotFoundError:
            print(f"{agent_file} not found")
        except Exception as e:
            print(f"Failed to get details for VM {vm}: {e}")
            continue

        All_VM_Details.append(VM_Details)
        all_vm_details =dict()
        all_vm_details = zip(VMList, All_VM_Details)
        all_vm_details = dict(all_vm_details)
    return all_vm_details

print("VM Details : \n ",get_vm_details())
logging.info("The VM List is: %s", get_vm_details())