Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
12 kB
1
Indexable
Never
#Necessary imports
import string
import pandas as pd
import regex as re
import datetime
import datefinder
import sys
import subprocess
import linecache
from itertools import islice
import time
import gzip
import os

# Declaring Files
agent_file='agent.properties'
bookmark_file='runtime_log.properties'

# Get VM List from agent.properties file
def get_VMList():
    with open(agent_file,'rt') as file:
        for line in file:
            if 'VMList' in line:
                vm_list = str(line.split('='))
                vm_list = re.findall(r'\d+', vm_list)
                VMList = list(map(int, vm_list))
    return VMList
print("The VM List is : ",get_VMList())

VMList = get_VMList()


#Get number of lines to be copied
def get_log_block_size():
    
    with open(agent_file,'rt') 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]

    return no_of_lines

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


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

# Getting All VM Details
def get_vm_details():
    All_VM_Details = []
    for vm in VMList:
        vm_id=vm
        with open(agent_file,'rt') as file:
            for line in file:
                if str(vm) + ".privatip" in line:
                    VM_Details = {vm_id:{'vm_ip':[],'vm_output_log_filename':[],'vm_log_enable':[],'vm_log_filename':[],'bookmark':[],'bookmark_ip':[],'bookmark_log':[]}};
                    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)
                    

        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())  

#Searching and Replacing if Bookmark Record is found
def search_replace_with_bookmark_record(bookmark_line,i):
    print("The function with bookmark_record is getting called.")
    vm_id=i
    print("VM ID :",vm_id)
    bookmark_record=bookmark_line
    number_of_lines=get_log_block_size()
    print("No of Lines to copy : ",number_of_lines)
    print("Bookmark Record",bookmark_record)
    all_vm_details = get_vm_details()
    vm_ip = all_vm_details[vm_id][vm_id]['vm_ip'][0]
    vm_log_filename=all_vm_details[vm_id][vm_id]['vm_log_filename'][0]
    print("VM Log FIle : ",vm_log_filename)
    vm_output_directory_loc=all_vm_details[vm_id][vm_id]['vm_output_log_filename'][0]
    print("VM Output Directory : ",vm_output_directory_loc)
    bookmark_log=all_vm_details[vm_id][vm_id]['bookmark_log'][0]
    syslog_record = bookmark_record.split(str(vm_ip)+'_bookmark_log=')
    print("Syslog Record for VM {} is {}" .format(i,syslog_record[1]))
    print("Bookmark log with =",bookmark_log)
    with open(bookmark_file, "rt") as file:
        line_to_search = str(syslog_record[1])
        print("Line to search is : ",str(line_to_search))
        print("Searching in SYslog File")
    with open('LONSTBYRDEV02-10.72.209.183.log', 'r') as file:
        for i, line in enumerate(file, 1):
            if line_to_search in line:
                print(f"SYslog Record {line_to_search} Found at Line number:{i}")    
                break
        global old_line_number,last_line_no
        old_line_number=i +1
        print(f"Old Line Number is : {old_line_number}")
        last_line_no=int(old_line_number)+int(number_of_lines)
        print(f"Last Line Number is : {last_line_no} ")
        #filename=str(vm_ip)+"-"+str(current_time) +".log"
        filename=str(vm_output_directory_loc)+str(vm_ip)+"-T" +str(current_time)
        print(filename)
        import itertools
        X = old_line_number  # Starting line number
        Y = last_line_no  # Ending line number
        with open('LONSTBYRDEV02-10.72.209.183.log', 'r') as input_file, open(filename, 'w') as output_file:
            output_file.writelines(itertools.islice(input_file, X - 1, Y))
        line_to_write=linecache.getline('LONSTBYRDEV02-10.72.209.183.log', last_line_no) ##Change here for dynamic File
        print("Content for nth line",line_to_write)
        global final_line
        final_line=str(bookmark_log)+str(line_to_write)
        print("Final Line :",final_line)
        # Code to Update in Bookmark File
        import fileinput
        with open(bookmark_file, 'r+') as file:
            lines = file.readlines()
            file.seek(0)
            for line in lines:
                if bookmark_record in line:
                    file.write(line.replace(bookmark_record, final_line))
                else:
                    file.write(line)
            file.truncate()
    ## Zipping the files and Chmod 777 Code
    os.chmod(filename, 777)
    in_file = filename
    in_data = open(in_file, "rb").read()
    out_gz = filename +".gz"
    gzf = gzip.open(out_gz, "wb")
    gzf.write(in_data)
    gzf.close()
    os.remove(filename) 

    import gzip
    import os
    # Open a file for writing and compress it using gzip
    with open(filename, 'rb') as f_in:
        with gzip.open(filename+".gz", 'wb') as f_out:
            f_out.writelines(f_in)
    # Set the permissions of the gz file to 777
    os.chmod(filename+".gz", 0o777)       


#Searching and Replacing if Bookmark Record is not found
def search_replace_without_bookmark_record(i):
    print("The function without bookmark record is getting called.")
    vm_id=i
    print("VM ID :",vm_id)
    new_line_number=get_log_block_size()
    print("No of Lines to Read : ",new_line_number)
    all_vm_details = get_vm_details()
    vm_ip = all_vm_details[vm_id][vm_id]['vm_ip'][0] ##Access List Element without Quotes
    print(vm_ip)
    vm_log_filename=all_vm_details[vm_id][vm_id]['vm_log_filename'][0]
    print("VM Log FIle : ",vm_log_filename)
    vm_output_directory_loc=all_vm_details[vm_id][vm_id]['vm_output_log_filename'][0]
    print("VM Output Directory : ",vm_output_directory_loc)
    line_to_write_1=str(vm_ip)+"_bookmark_log="
    print("Line to write 1:",line_to_write_1)
    bookmark_line2=line_to_write_1
    print("Bookmark Line2 : ",bookmark_line2)
    print("Searching in SYslog File")
    old_line_no=1
    old_line_no=int(old_line_no)
    print(type(old_line_no))
    print(f"Old Line Number is : {old_line_no}")
    last_line_number=int(old_line_no)+int(new_line_number)
    print(f"Last Line Number is : {last_line_number} ")
    with open(vm_log_filename, 'r') as f:
    # Extract the lines from the start line to the end line
        extracted_lines = list(islice(f, old_line_no-1, last_line_number))
        # Store the extracted lines in a variable
        lines_list = extracted_lines
        filename=str(vm_ip)+"-"+str(current_time) +".log"
        # Access the nth line in the list
        last_line = lines_list[last_line_number-1]
        # Print the nth line
        print("Last Line : ",last_line)
        global final_line
        final_line=bookmark_line2+last_line
        print("Final Line :",final_line)
        with open(filename, 'w') as f:
            f.writelines(line for line in lines_list)
        
    print("FInal Line :",final_line)
    ## Appending the record in the Bookmark File
    with open(bookmark_file, 'a') as file:
        file.write("\n")
        file.write(final_line)

    ## Zipping and CHmod 777 Code
    os.chmod(filename, 777)
    in_file = filename
    in_data = open(in_file, "rb").read()
    out_gz = filename +".gz"
    gzf = gzip.open(out_gz, "wb")
    gzf.write(in_data)
    gzf.close()
    os.remove(filename)


  # Checking if Bookmark Record is found in RUntime_log.properties
def check_bookmark_record(i):
    vm_id=i
    all_vm_details = get_vm_details()
    vm_ip = all_vm_details[vm_id][vm_id]['vm_ip'][0] ##Access List Element without Quotes
    print(vm_ip) 
    line_to_search = str(vm_ip)+"_bookmark_log="
    print("Bookmark Log : ",line_to_search)
    with open(bookmark_file, "r") as f:
        lines = f.readlines()
        filtered_lines = filter(lambda x: line_to_search in x, lines)
        filtered_lines = [line for line in filtered_lines]
        if filtered_lines:
            for bookmark_line in filtered_lines:
                print(bookmark_line)
                print(f"Bookmark Log Record found: {bookmark_line}")
                print("Redirecting to search_replace_with_bookmark function") 
                search_replace_with_bookmark_record(bookmark_line,i)
        else:
            print("No line found containing the specified string: ")
            print("Bookmark Log Record not found.Creating a record and redirecting to syslog File Location")
            search_replace_without_bookmark_record(i)       

    return None     


#Checking log enable condition is Yes or No                   

def check_log_enable():

    VMList = get_VMList()

    #print(VMList)
    #print("VM Details : ",get_vm_details())

    for i in VMList:

        vm_id=i

        all_vm_details = get_vm_details()

        if all_vm_details[vm_id][vm_id]['vm_log_enable']==['y']:

            print(f"Log enable condition is yes.Lets proceed further to check Bookmark Function",vm_id)
            check_bookmark_record(vm_id)      ## Call Function


        else:

            print(f"Log enable condition is no. No action Needed." ,vm_id)
            continue

check_log_enable()