Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mozarik
Created March 2, 2021 13:15
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 mozarik/899f0ceed124f78229e3d468d0260fde to your computer and use it in GitHub Desktop.
Save mozarik/899f0ceed124f78229e3d468d0260fde to your computer and use it in GitHub Desktop.
Coursera Week 7 Grade Assesment
#!/usr/bin/python3
import re
import operator
import pprint
import operator
import collections
import csv
# 1.The rankking of errors generated by the system
# Sorted by the most common error
# 2.The user usage statistics for the service
# A list of user in log + how many info message + how many error message each user generated.
# sorted by username
per_error = {}
per_user = {}
extract_error_re = r"ticky: (ERROR|INFO) ([\w' ]*).* \(([\w.]*)\)"
with open("./syslog.log", 'r') as file:
for line in file:
found = re.search(extract_error_re, line)
if found == None:
continue
elif found.group(1) == "ERROR":
error_string = found.group(2)
user_string = found.group(3)
# Processing per_error
per_error[error_string] = per_error.get(error_string, 0) + 1
# Processing per_user`
if user_string not in per_user:
per_user[user_string] = [0, 1]
else:
per_user[user_string][1] += 1
elif found.group(1) == "INFO":
# We are not processing per_error here
user_string = found.group(3)
if user_string not in per_user:
per_user[user_string] = [1, 0]
else:
per_user[user_string][0] += 1
file.close()
# Sort the per_error by the value
sorted_error_tuples = sorted(
per_error.items(), key=operator.itemgetter(1), reverse=True)
# Sort the per_user by the key
sorted_user_tuples = collections.OrderedDict(sorted(per_user.items()))
# sorted_user_dict = {k: v for k, v in sorted_user_tuples.}
# Process csv for error
csv_error_file = "error_message.csv"
error_fieldnames = ["Error", "Count"]
with open(csv_error_file, 'w') as file:
writer = csv.writer(file)
writer.writerow(error_fieldnames)
writer.writerows(sorted_error_tuples)
file.close()
# Process csv for user
csv_user_file = "user_statistics.csv"
user_fieldnames = ["Username", "INFO", "ERROR"]
with open(csv_user_file, 'w') as file:
writer = csv.writer(file)
writer.writerow(user_fieldnames)
for user, value in sorted_user_tuples.items():
user = user
info_count = value[0]
error_count = value[1]
list_to_write = [user, info_count, error_count]
writer.writerow(list_to_write)
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment