-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparse.py
More file actions
61 lines (44 loc) · 1.47 KB
/
parse.py
File metadata and controls
61 lines (44 loc) · 1.47 KB
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
import time
import os
import argparse
parser = argparse.ArgumentParser(
prog="tailf",
description="Reads and prints the last 10 lines of a file line by line every 10 seconds",
)
parser.add_argument("logfile", help="Path to the log file")
args = parser.parse_args()
def clear_console():
os.system("cls" if os.name == "nt" else "clear")
def parse(input_line: str) -> dict:
columns = input_line.split()
parsed = {
"sec": int(columns[0]),
"host": columns[1],
"starttime": time.gmtime(float(columns[2])),
"jobRuntime": float(columns[3]),
"send": int(columns[4]),
"receive": int(columns[5]),
"exitval": int(columns[6]),
"signal": int(columns[7]),
"command": " ".join(columns[8:]),
}
return parsed
try:
with open(args.logfile, "r") as file:
lines = []
while True:
new_lines = file.readlines()
if new_lines:
lines.extend(new_lines)
last_10_lines = lines[-10:]
clear_console()
for line in last_10_lines:
parsed_line = parse(line.replace("\x00", ""))
for key, value in parsed_line.items():
print(f"Column {key}: {value}")
print("-----------")
time.sleep(10)
except FileNotFoundError:
print(f"Error: The file '{args.logfile}' was not found.")
except IOError as e:
print(f"Error reading the file '{args.logfile}': {e}")