-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
68 lines (55 loc) · 1.68 KB
/
Copy pathcli.py
File metadata and controls
68 lines (55 loc) · 1.68 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
62
63
64
65
66
67
68
import argparse
import json
import sys
from reader import JournalReader
from parser import LogParser, LogEntry
def main():
parser = argparse.ArgumentParser(
description='Analyze systemd logs and generate a chronological timeline of errors and service failures.'
)
parser.add_argument(
'--boot',
type=int,
default=0,
help='Boot number to analyze (0=current, -1=previous boot)'
)
parser.add_argument(
'--service',
type=str,
help='Filter by service name (e.g., nginx)'
)
parser.add_argument(
'--json',
action='store_true',
help='Output in JSON format'
)
args = parser.parse_args()
try:
reader = JournalReader()
log_parser = LogParser()
lines = reader.read_logs(boot=args.boot, service=args.service)
if not lines:
print("No errors detected in the specified boot.")
return 0
entries = log_parser.parse(lines)
if not entries:
print("No errors detected in the specified boot.")
return 0
if args.json:
output = [entry.to_dict() for entry in entries]
print(json.dumps(output, indent=2))
else:
for entry in entries:
print(entry.to_text())
return 0
except RuntimeError as e:
print(f"Error: {str(e)}", file=sys.stderr)
return 1
except KeyboardInterrupt:
print("\nInterrupted by user", file=sys.stderr)
return 130
except Exception as e:
print(f"Unexpected error: {str(e)}", file=sys.stderr)
return 1
if __name__ == '__main__':
sys.exit(main())