Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 2 | |
| 3 | # Copyright 2013 IBM Corp. |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
| 18 | import gzip |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 19 | import pprint |
Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 20 | import re |
| 21 | import StringIO |
| 22 | import sys |
| 23 | import urllib2 |
| 24 | |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 25 | |
Sean Dague | 70eef03 | 2013-03-20 13:41:15 -0400 | [diff] [blame] | 26 | pp = pprint.PrettyPrinter() |
| 27 | |
| 28 | NOVA_TIMESTAMP = r"\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d" |
| 29 | |
| 30 | NOVA_REGEX = r"(?P<timestamp>%s) (?P<pid>\d+ )?(?P<level>(ERROR|TRACE)) " \ |
| 31 | "(?P<module>[\w\.]+) (?P<msg>.*)" % (NOVA_TIMESTAMP) |
| 32 | |
| 33 | |
| 34 | class StackTrace(object): |
| 35 | timestamp = None |
| 36 | pid = None |
| 37 | level = "" |
| 38 | module = "" |
| 39 | msg = "" |
| 40 | |
| 41 | def __init__(self, timestamp=None, pid=None, level="", module="", |
| 42 | msg=""): |
| 43 | self.timestamp = timestamp |
| 44 | self.pid = pid |
| 45 | self.level = level |
| 46 | self.module = module |
| 47 | self.msg = msg |
| 48 | |
| 49 | def append(self, msg): |
| 50 | self.msg = self.msg + msg |
| 51 | |
| 52 | def is_same(self, data): |
| 53 | return (data['timestamp'] == self.timestamp and |
| 54 | data['level'] == self.level) |
| 55 | |
| 56 | def not_none(self): |
| 57 | return self.timestamp is not None |
| 58 | |
| 59 | def __str__(self): |
| 60 | buff = "<%s %s %s>\n" % (self.timestamp, self.level, self.module) |
| 61 | for line in self.msg.splitlines(): |
| 62 | buff = buff + line + "\n" |
| 63 | return buff |
| 64 | |
Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 65 | |
| 66 | def hunt_for_stacktrace(url): |
| 67 | """Return TRACE or ERROR lines out of logs.""" |
David Kranz | 9e3c717 | 2013-10-09 21:45:31 -0400 | [diff] [blame] | 68 | req = urllib2.Request(url) |
| 69 | req.add_header('Accept-Encoding', 'gzip') |
| 70 | page = urllib2.urlopen(req) |
Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 71 | buf = StringIO.StringIO(page.read()) |
| 72 | f = gzip.GzipFile(fileobj=buf) |
| 73 | content = f.read() |
Sean Dague | 70eef03 | 2013-03-20 13:41:15 -0400 | [diff] [blame] | 74 | |
| 75 | traces = [] |
| 76 | trace = StackTrace() |
| 77 | for line in content.splitlines(): |
| 78 | m = re.match(NOVA_REGEX, line) |
| 79 | if m: |
| 80 | data = m.groupdict() |
| 81 | if trace.not_none() and trace.is_same(data): |
| 82 | trace.append(data['msg'] + "\n") |
| 83 | else: |
| 84 | trace = StackTrace( |
| 85 | timestamp=data.get('timestamp'), |
| 86 | pid=data.get('pid'), |
| 87 | level=data.get('level'), |
| 88 | module=data.get('module'), |
| 89 | msg=data.get('msg')) |
| 90 | |
| 91 | else: |
| 92 | if trace.not_none(): |
| 93 | traces.append(trace) |
| 94 | trace = StackTrace() |
| 95 | |
| 96 | # once more at the end to pick up any stragglers |
| 97 | if trace.not_none(): |
| 98 | traces.append(trace) |
| 99 | |
| 100 | return traces |
Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 101 | |
| 102 | |
| 103 | def log_url(url, log): |
| 104 | return "%s/%s" % (url, log) |
| 105 | |
| 106 | |
| 107 | def collect_logs(url): |
| 108 | page = urllib2.urlopen(url) |
| 109 | content = page.read() |
| 110 | logs = re.findall('(screen-[\w-]+\.txt\.gz)</a>', content) |
| 111 | return logs |
| 112 | |
| 113 | |
| 114 | def usage(): |
Dirk Mueller | 1db5db2 | 2013-06-23 20:21:32 +0200 | [diff] [blame] | 115 | print(""" |
Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 116 | Usage: find_stack_traces.py <logurl> |
| 117 | |
| 118 | Hunts for stack traces in a devstack run. Must provide it a base log url |
| 119 | from a tempest devstack run. Should start with http and end with /logs/. |
| 120 | |
| 121 | Returns a report listing stack traces out of the various files where |
| 122 | they are found. |
Dirk Mueller | 1db5db2 | 2013-06-23 20:21:32 +0200 | [diff] [blame] | 123 | """) |
Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 124 | sys.exit(0) |
| 125 | |
| 126 | |
Sean Dague | 70eef03 | 2013-03-20 13:41:15 -0400 | [diff] [blame] | 127 | def print_stats(items, fname, verbose=False): |
| 128 | errors = len(filter(lambda x: x.level == "ERROR", items)) |
| 129 | traces = len(filter(lambda x: x.level == "TRACE", items)) |
Dirk Mueller | 1db5db2 | 2013-06-23 20:21:32 +0200 | [diff] [blame] | 130 | print("%d ERRORS found in %s" % (errors, fname)) |
| 131 | print("%d TRACES found in %s" % (traces, fname)) |
Sean Dague | 70eef03 | 2013-03-20 13:41:15 -0400 | [diff] [blame] | 132 | |
| 133 | if verbose: |
| 134 | for item in items: |
Dirk Mueller | 1db5db2 | 2013-06-23 20:21:32 +0200 | [diff] [blame] | 135 | print(item) |
| 136 | print("\n\n") |
Sean Dague | 70eef03 | 2013-03-20 13:41:15 -0400 | [diff] [blame] | 137 | |
| 138 | |
Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 139 | def main(): |
| 140 | if len(sys.argv) == 2: |
| 141 | url = sys.argv[1] |
| 142 | loglist = collect_logs(url) |
| 143 | |
| 144 | # probably wrong base url |
| 145 | if not loglist: |
| 146 | usage() |
| 147 | |
| 148 | for log in loglist: |
| 149 | logurl = log_url(url, log) |
| 150 | traces = hunt_for_stacktrace(logurl) |
Sean Dague | 70eef03 | 2013-03-20 13:41:15 -0400 | [diff] [blame] | 151 | |
Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 152 | if traces: |
Sean Dague | 70eef03 | 2013-03-20 13:41:15 -0400 | [diff] [blame] | 153 | print_stats(traces, log, verbose=True) |
| 154 | |
Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 155 | else: |
| 156 | usage() |
| 157 | |
| 158 | if __name__ == '__main__': |
| 159 | main() |