forked from doublechiang/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_log.py
executable file
·153 lines (127 loc) · 4.97 KB
/
parse_log.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
import os
import sys
import logging
class Entry():
def addline(self, line):
token = line.split()
# if it's a sel,record the record number.
if token[0] == 'SEL':
self.record = token[4]
#logging.info('SEL logged, ID=%s', self.record)
if token[0] == 'FRU':
#logging.debug('FRU token being parsed')
self.fru =True
if token[0] == 'Timestamp':
self.timestamp = token[2]
if token[0] == 'Description':
# Some of the SEL do not have description, skip it.
# logging.debug("token=%s", token)
if len(token) < 3:
return self
if token[2] == 'Correctable' and token[3] == 'ECC':
# logging.debug("ECC logged=%s", line)
self.ECC = True
if token[2] == 'IERR':
self.ierr = True
if token[2] == 'Uncorrectable' and token[3] == 'machine':
self.umce = True
if token[2] == 'Uncorrectable' and token[3] == 'ECC':
self.uecc = True
if token[2] == 'Upper' and token[3] == 'Critical':
self.ucgh = True
if token[2] == 'Lower' and token[3] == 'Critical':
self.lcgl = True
if self.fru:
if token[0] == 'Chassis' and token[1] == 'Serial':
self.serial = token[3]
#logging.debug("FRU serial number %s is saved", self.serial)
if token[0] == 'Board' and token[1] == 'Serial':
self.board_serial = token[3]
return self
def __str__(self):
return "Entry ECC =%s" % self.ECC
def __init__(self):
self.record =None
self.fru=False
self.serial = None
self.board_serial = None
self.ECC=False
self.ierr=False
self.umce=False
self.uecc=False
self.ucgh=False
self.lcgl=False
self.timestamp=None
def parse_entry(sel):
system = {'ECC': False, 'serial': None, 'board_sn': None, 'ierr':False, 'umce':False, 'uecc': False, 'ucgh': False, 'lcgl': False,
'ecctime': None, 'ierrtime': None, 'umcetime': None, 'uecctime': None, 'ucghtime': None, 'lcgltime': None
}
for i in sel:
if i.fru:
if i.serial:
system['serial'] = i.serial
if i.board_serial:
system['board_sn'] = i.board_serial
if i.record:
if i.ECC:
system['ECC'] = True
system['ecctime'] = i.timestamp
if i.ierr:
system['ierr'] = True
system['ierrtime'] = i.timestamp
if i.umce:
system['umce'] = True
system['umcetime'] = i.timestamp
if i.uecc:
system['uecc'] = True
system['uecctime'] = i.timestamp
if i.ucgh:
system['ucgh'] = True
system['ucghtime'] = i.timestamp
if i.lcgl:
system['lcgl'] = True
system['lcgltime'] = i.timestamp
print("Chassis SN=%s, Board SN=%s, ECC=%s, ECCTime=%s, IERR=%s, IERRTime=%s, UMCE=%s, UMCETime=%s, UECC=%s, UECCTime=%s, UCGH=%s, UCGHTime=%s, LCGL=%s, LCGLTime=%s" % (system['serial'],
system['board_sn'], system['ECC'], system['ecctime'], system['ierr'], system['ierrtime'],
system['umce'], system['umcetime'], system['uecc'], system['uecctime'],
system['ucgh'], system['ucghtime'], system['lcgl'], system['lcgltime']))
def parse_file(filepath):
if not os.path.isfile(filepath):
logging.error("File path {} does not exist. Exiting...".format(filepath))
sys.exit()
sel = []
entry = None
with open(filepath) as fp:
for line in fp:
line = line.rstrip()
if len(line.split()) > 0:
# any keyword 'SEL' will create a new SEL
if line.split()[0] == 'SEL' or line.split()[0] == 'FRU':
if (entry == None):
entry = Entry().addline(line)
# any line with a space before
if line[0] == ' ':
if entry:
entry.addline(line)
else:
sel.append(entry)
entry = None
# any empty line will close the entry
# all information saved in
parse_entry(sel)
def main():
if len(sys.argv) < 2:
logging.error("a directory path must be included")
sys.exit()
# parse_file(sys.argv[1])
directory = sys.argv[1]
for filename in os.listdir(directory):
if (filename.endswith(".log")):
# logging.debug("processing %s", filename)
print(filename, end=', ')
parse_file(os.path.join(directory, filename))
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
# logging.basicConfig(level=logging.WARNING)
main()