blob: e6c1990bc40e5ae2cb61313e240cc8d7ac0af605 [file] [log] [blame]
Sean Daguebcdba082013-03-12 15:14:16 -04001#!/usr/bin/env python
2# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
4# Copyright 2013 IBM Corp.
5# All Rights Reserved.
6#
7# Licensed under the Apache License, Version 2.0 (the "License"); you may
8# not use this file except in compliance with the License. You may obtain
9# a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16# License for the specific language governing permissions and limitations
17# under the License.
18
19import gzip
20import re
21import StringIO
22import sys
23import urllib2
24
25
26def hunt_for_stacktrace(url):
27 """Return TRACE or ERROR lines out of logs."""
28 page = urllib2.urlopen(url)
29 buf = StringIO.StringIO(page.read())
30 f = gzip.GzipFile(fileobj=buf)
31 content = f.read()
32 traces = re.findall('^(.*? (TRACE|ERROR) .*?)$', content, re.MULTILINE)
33 tracelist = map(lambda x: x[0], traces)
34 # filter out log definitions as false possitives
35 return filter(lambda x: not re.search('logging_exception_prefix', x),
36 tracelist)
37
38
39def log_url(url, log):
40 return "%s/%s" % (url, log)
41
42
43def collect_logs(url):
44 page = urllib2.urlopen(url)
45 content = page.read()
46 logs = re.findall('(screen-[\w-]+\.txt\.gz)</a>', content)
47 return logs
48
49
50def usage():
51 print """
52Usage: find_stack_traces.py <logurl>
53
54Hunts for stack traces in a devstack run. Must provide it a base log url
55from a tempest devstack run. Should start with http and end with /logs/.
56
57Returns a report listing stack traces out of the various files where
58they are found.
59"""
60 sys.exit(0)
61
62
63def main():
64 if len(sys.argv) == 2:
65 url = sys.argv[1]
66 loglist = collect_logs(url)
67
68 # probably wrong base url
69 if not loglist:
70 usage()
71
72 for log in loglist:
73 logurl = log_url(url, log)
74 traces = hunt_for_stacktrace(logurl)
75 if traces:
76 print "\n\nTRACES found in %s\n" % log
77 for line in traces:
78 print line
79 else:
80 usage()
81
82if __name__ == '__main__':
83 main()