stefbrad15

Log file CLI analyzer Python app example

Apr 13th, 2022 (edited)
72
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # log lines
  2. # ip,site,status_code
  3.  
  4. # Store some data
  5. log_lines = []
  6.  
  7.  
  8. # Create the menu
  9. #
  10. # Menu should be able to load a log file and display some stats
  11. # if there is nothing laoded show an error and reprompt
  12.  
  13. exit = False
  14. while not exit:    
  15.     print("1: Load file")
  16.     print("2: Show num lines")
  17.     print("3: Show errors by status message")
  18.     print("4: Exit")
  19.     opt = input("Enter option from above")
  20.  
  21.     if opt == 1:
  22.         file_name = input("Enter file name:")
  23.         # can do some check if file is valid or not
  24.         with open(file_name) as f:
  25.             log_lines = f.readlines()
  26.     elif opt == 2:
  27.         print(f"num log lines={len(log_lines)}")
  28.     elif opt == 3:
  29.         status_codes = {}
  30.         for line in log_lines:
  31.             line_data = line.split(",")
  32.             if line_data[2] not in status_codes:
  33.                 status_codes[line_data[2]] = 1
  34.             else:
  35.                 status_codes[line_data[2]] += 1
  36.         print("Status codes counts")
  37.         for s_code in status_codes:
  38.             print(f"Code={s_code} Count={status_codes[s_code]}")            
  39.     elif opt == 4:
  40.         exit = True
  41.     else:
  42.         print("Invalid option, please ree-nter\n")
RAW Paste Data Copied