Skip to content

Instantly share code, notes, and snippets.

@shojibMahabub
Created December 7, 2018 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shojibMahabub/0a7bf3e6a302e2006a92c0bab5e11eee to your computer and use it in GitHub Desktop.
Save shojibMahabub/0a7bf3e6a302e2006a92c0bab5e11eee to your computer and use it in GitHub Desktop.
Problem 2 : All requirements are met except requirement number 3.
"""
This programme is used to parse log files.
And sort them according to TOKENS. Also
show statistics about the log.
Author: Mahabub Elahi
Version: 1.0
"""
# read logs from log file
# ================================
f = open('log.txt')
lines = f.readlines()
f.close()
# default counter
# ================================
error_count = 0
warning_count = 0
total_messeges = len(lines)
# parsing
# =================================
for line in lines:
# error or warning
ticket = line.split()[1]
# host ID
host = line.split()[3]
# representing
if ticket == '[ERROR]':
data = line.split()[1:]
print(*data, sep=' ')
error_count += 1
if ticket == '[WARNING]':
data = line.split()[1:]
print(*data, sep=' ')
warning_count += 1
# statistics
# ==================================
warning_percentage = (warning_count * 100) // total_messeges
error_percentage = (error_count * 100) // total_messeges
total = warning_percentage + error_percentage
print('\ntotal: ', total, '% warning/error - ', total_messeges, 'messeges sent/receieved')
# Requirements
# Reading in the file, and parsing it into an internal model ==> done
# Sorting the error messages by server ==> done
# Sorting the error messages into buckets of similar error messages (within the server id) ==> not done
# Getting statistical information for each server ==> done
# Getting statistical information for the log as a whole ==> done
# (Optional) Formatting the output in a nice html page ==> not done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment