-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjcollect-slicer.py
More file actions
111 lines (87 loc) · 4.25 KB
/
Copy pathjcollect-slicer.py
File metadata and controls
111 lines (87 loc) · 4.25 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import os
import sys
import re
import argparse
def slice(jcollect_file, hostname_overwrite):
time_line_pattern = r'^=== \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} \w+ \[\+\d{4}\] \| \d+$'
print(jcollect_file)
with open(jcollect_file, 'r', errors='replace') as inflie:
for line in inflie.readlines():
if line.startswith(">===="):
new_section = 0
hostname = None
command_date = None
full_line_date = None
full_line_command = None
file_name = None
continue
if line.startswith("=== "):
if '@' in line:
# Find the position of "@" and ":"
at_position = line.find("@")
colon_position = line.find(":")
# Extract the substring between "@" and ":"
if at_position != -1 and colon_position != -1 and at_position < colon_position:
hostname = line[at_position + 1 : colon_position]
else:
hostname = "unknown-hostname"
if '# cli -c show ' in line:
#f.close()
pattern = r'-c\s(.*?)\s\|'
match = re.search(pattern, line)
extracted_command = match.group(1)
file_name = extracted_command.replace(' ', '_')
#file_name = '_'.join(line.split('"')[1].split('|')[0].split())
#f = open(os.path.join(dev_name, file_name), 'w')
if '# cprod -A fpc0 -c show' in line:
pattern = r"cprod -A (\w+) -c (.*)"
match = re.search(pattern, line)
fpc = match.group(1)
component1 = match.group(2)
extracted_command = f"{component1}-{fpc}"
file_name = extracted_command.replace(' ', '_')
pass
if '# cprod -A fpc0 -c set' in line:
pattern = r"cprod -A (\w+) -c set (.*)"
match = re.search(pattern, line)
fpc = match.group(1)
bcm_command = match.group(2).strip().replace('"', '')
extracted_command = f"{bcm_command}-{fpc}"
file_name = extracted_command.replace(' ', '_')
pass
if re.match(time_line_pattern, line):
full_line_date = line
pattern = r'=== (\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})'
match = re.search(pattern, line)
year, month, day, hour, minute, second = match.groups()
command_date = f"{year}{month}{day}-{hour}{minute}{second}-"
continue
full_line_command = line
if line.startswith("============================================="):
if hostname_overwrite is not None:
hostname = hostname_overwrite
directory_path = os.path.join(os.getcwd(), hostname)
if not os.path.exists(directory_path):
os.makedirs(directory_path)
print("hostname:", hostname)
filename = f"{command_date}{file_name}.log"
#print(command_date)
print(filename)
print(line)
f = open(os.path.join(directory_path, filename), 'w')
f.write(line)
f.write(full_line_command)
f.write(full_line_date)
new_section = 1
pass
if new_section == 1:
f.write(line)
if __name__ == '__main__':
# Create the argument parser
parser = argparse.ArgumentParser(description='JCollect Argument Parser')
# Add the arguments
parser.add_argument('-f', '--file', help='File argument', required=True)
parser.add_argument('-n', '--name', help='Name argument', default=None)
# Parse the arguments
args = parser.parse_args()
slice(args.file, args.name)