Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 1 | #!/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 | |
| 19 | import gzip |
| 20 | import re |
| 21 | import StringIO |
| 22 | import sys |
| 23 | import urllib2 |
| 24 | |
| 25 | |
| 26 | def 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 | |
| 39 | def log_url(url, log): |
| 40 | return "%s/%s" % (url, log) |
| 41 | |
| 42 | |
| 43 | def 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 | |
| 50 | def usage(): |
| 51 | print """ |
| 52 | Usage: find_stack_traces.py <logurl> |
| 53 | |
| 54 | Hunts for stack traces in a devstack run. Must provide it a base log url |
| 55 | from a tempest devstack run. Should start with http and end with /logs/. |
| 56 | |
| 57 | Returns a report listing stack traces out of the various files where |
| 58 | they are found. |
| 59 | """ |
| 60 | sys.exit(0) |
| 61 | |
| 62 | |
| 63 | def 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 | |
| 82 | if __name__ == '__main__': |
| 83 | main() |