-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_report.py
executable file
·71 lines (61 loc) · 2.1 KB
/
user_report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
import re
import os
import csv
import subprocess
import pandas as pd
class UserReport:
def __init__(self, filename):
self.filename = filename
def get_user_msg(self):
"""find all the username in syslog.log and store them in a list"""
pattern = r"\(([a-z.]+)\)"
user_list = []
with open(self.filename) as file:
for line in file:
usr_line = re.search(pattern, line)
if not usr_line[1] in user_list:
user_list.append(usr_line[1])
return user_list
def count_msg(self):
"""
count INFO and ERROR for each user in syslog.log,
put all the count data into a list dict format
"""
user_dict = []
each_user = {}
userlist = self.get_user_msg()
for user in userlist:
each_user["USERNAME"] = user
each_user["INFO"] = 0
each_user["ERROR"] = 0
with open(self.filename) as file:
for line in file:
if user in line and "INFO" in line:
each_user["INFO"] += 1
elif user in line and "ERROR" in line:
each_user["ERROR"] += 1
continue
user_dict.append(each_user.copy())
return user_dict
def create_report(self):
"""create the csv file and print it out"""
keys = ["USERNAME", "INFO", "ERROR"]
csv_data = self.count_msg()
with open("user_report.csv", "w") as user:
writer = csv.DictWriter(user, fieldnames=keys)
writer.writeheader()
writer.writerows(csv_data)
subprocess.run(['cat', 'user_report.csv'])
def create_html(self):
if os.path.exists("user_report.csv"):
df = pd.read_csv("user_report.csv")
df.to_html("user_report.html")
else:
print("Please generate the csv file first")
def main():
report = UserReport("syslog.log")
report.create_report()
report.create_html()
if __name__ == "__main__":
main()