Created
August 4, 2023 23:14
-
-
Save wiljdaws/2382db2b0930d5046d87dfdc36ffbb6b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import re | |
import csv | |
error_count = {} | |
user_statistics = {} | |
with open('syslog.log') as file: | |
for line in file: | |
match = re.search(r"ticky: ([A-Z]+) ([\w\s']+)\(([\w\.]+)\)", line) | |
if match: | |
status, ticket, user = match.groups() | |
if status == "ERROR": | |
if ticket in error_count: | |
error_count[ticket] += 1 | |
else: | |
error_count[ticket] = 1 | |
if user in user_statistics: | |
if status == "INFO": | |
user_statistics[user][0] += 1 | |
elif status == "ERROR": | |
user_statistics[user][1] += 1 | |
else: | |
user_statistics[user] = [1 if status == "INFO" else 0, 1 if status == "ERROR" else 0] | |
sorted_errors = dict(sorted(error_count.items(), key=lambda x: (-x[1], x[0]))) | |
sorted_users = dict(sorted(user_statistics.items())) | |
with open('error_message.csv', 'w', newline='') as error_file: | |
writer = csv.writer(error_file) | |
writer.writerow(['Error', 'Count']) | |
for error, count in sorted_errors.items(): | |
writer.writerow([error, count]) | |
with open('user_statistics.csv', 'w', newline='') as user_file: | |
writer = csv.writer(user_file) | |
writer.writerow(['Username', 'INFO', 'ERROR']) | |
for user, stats in sorted_users.items(): | |
writer.writerow([user, stats[0], stats[1]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment