-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_logs.py
executable file
·189 lines (151 loc) · 5.06 KB
/
parse_logs.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2016 Matthew Stone <[email protected]>
# Distributed under terms of the MIT license.
"""
Parse LSF log files
"""
import sys
import argparse
import re
from datetime import datetime
import pandas as pd
class LSFLog(object):
def __init__(self, jobID, jobname, host, queue, start, end, walltime,
cmd, status, cpu, mem, swap, output_lines=[]):
self.jobID = jobID
self.jobname = jobname
self.host = host
self.queue = queue
self.start = start
self.end = end
self.walltime = walltime
self.cmd = cmd
self.status = status
self.cpu = cpu
self.mem = mem
self.swap = swap
self.output_lines = output_lines
@property
def output(self):
return ''.join(self.output_lines)
@property
def series(self):
return pd.Series(self.__dict__).drop(['output_lines', 'cmd'])
def parse_logs(logfile):
"""
Convert logfile entries to LSFLog objects
Parameters
---------
logfile : file
Open LSF log file handle
Yields
------
log : LSFLog
"""
jobID_exp = re.compile(r'Job (\d+):')
jobname_exp = re.compile(r'<(.*)>')
host_exp = re.compile(r'host\(s\) <([a-z]+[0-9]+)>')
queue_exp = re.compile(r'queue <([a-z]+)>')
cpu_exp = re.compile(r'(\d+\.\d+)')
mem_exp = re.compile(r'(\d+)')
log = None
for line in logfile:
if line.startswith('Sender'):
if log is not None:
if len(log.output_lines) == 0:
log.output_lines = ['']
yield log
subject = next(logfile)
jobID = re.search(jobID_exp, subject).group(1)
jobname = re.search(jobname_exp, subject).group(1)
next(logfile)
next(logfile)
execline = next(logfile)
try:
host = re.search(host_exp, execline).group(1)
except:
sys.stderr.write('Malformed job log: {0}\n'.format(jobID))
continue
queue = re.search(queue_exp, execline).group(1)
next(logfile)
next(logfile)
startline = next(logfile)
endline = next(logfile)
start = startline.strip().split(' at ')[1]
end = endline.strip().split(' at ')[1]
start = datetime.strptime(start, '%a %b %d %X %Y')
end = datetime.strptime(end, '%a %b %d %X %Y')
walltime = (end - start).total_seconds()
for i in range(5):
next(logfile)
cmd = ''
for line in logfile:
if line.startswith('--------'):
cmd = cmd.strip()
break
cmd = cmd + ' ' + line.strip()
next(logfile)
statusline = next(logfile)
if statusline.startswith('Successfully completed'):
status = 'DONE'
else:
status = 'EXIT'
cpuline = next(logfile)
while re.search(cpu_exp, cpuline) is None:
cpuline = next(logfile)
cpu = float(re.search(cpu_exp, cpuline).group(1))
memline = next(logfile)
try:
mem = int(re.search(mem_exp, memline).group(1))
except AttributeError:
mem = 0
swapline = next(logfile)
try:
swap = int(re.search(mem_exp, swapline).group(1))
except AttributeError:
swap = 0
log = LSFLog(jobID, jobname, host, queue, start, end, walltime,
cmd, status, cpu, mem, swap, [])
for i in range(6):
next(logfile)
elif log is not None:
log.output_lines.append(line.strip())
if len(log.output_lines) == 0:
log.output_lines = ['']
yield log
def job_stats(logs):
"""
Return numeric job stats as dataframe
Parameters
----------
logs : list of LSFLog
LSF logs
Returns
-------
stats : pd.DataFrame
"""
logs = [log.series for log in logs]
stats = pd.concat(logs, axis=1).transpose()
stats = stats.set_index('jobID')
stats['walltime_hr'] = stats['walltime'] / 3600
return stats
def main():
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('logfile', type=argparse.FileType('r'),
help='LSF log file to parse')
parser.add_argument('logstats',
help='Tidied LSF log statistics')
parser.add_argument('--output', type=argparse.FileType('w'),
help='stdout/stderr of each job')
args = parser.parse_args()
logs = list(parse_logs(args.logfile))
stats = job_stats(logs)
stats.to_csv(args.logstats, sep='\t')
for log in logs:
args.output.write('{0}\t{1}\t{2}\t{3}\n'.format(log.jobID, 'A', log.output_lines[0], 'B'))
if __name__ == '__main__':
main()