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 |
Masayuki Igawa | 134d9f7 | 2017-02-10 18:05:26 +0900 | [diff] [blame] | 21 | import sys |
| 22 | |
Harshada Mangesh Kakad | b3ecf65 | 2015-12-22 09:24:26 -0800 | [diff] [blame] | 23 | import six |
Yatin Kumbhare | 2e2c83a | 2016-05-30 22:45:58 +0530 | [diff] [blame] | 24 | import six.moves.urllib.request as urlreq |
Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 25 | |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 26 | |
Sean Dague | 70eef03 | 2013-03-20 13:41:15 -0400 | [diff] [blame] | 27 | pp = pprint.PrettyPrinter() |
| 28 | |
| 29 | NOVA_TIMESTAMP = r"\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d" |
| 30 | |
| 31 | NOVA_REGEX = r"(?P<timestamp>%s) (?P<pid>\d+ )?(?P<level>(ERROR|TRACE)) " \ |
| 32 | "(?P<module>[\w\.]+) (?P<msg>.*)" % (NOVA_TIMESTAMP) |
| 33 | |
| 34 | |
| 35 | class StackTrace(object): |
| 36 | timestamp = None |
| 37 | pid = None |
| 38 | level = "" |
| 39 | module = "" |
| 40 | msg = "" |
| 41 | |
| 42 | def __init__(self, timestamp=None, pid=None, level="", module="", |
| 43 | msg=""): |
| 44 | self.timestamp = timestamp |
| 45 | self.pid = pid |
| 46 | self.level = level |
| 47 | self.module = module |
| 48 | self.msg = msg |
| 49 | |
| 50 | def append(self, msg): |
| 51 | self.msg = self.msg + msg |
| 52 | |
| 53 | def is_same(self, data): |
| 54 | return (data['timestamp'] == self.timestamp and |
| 55 | data['level'] == self.level) |
| 56 | |
| 57 | def not_none(self): |
| 58 | return self.timestamp is not None |
| 59 | |
| 60 | def __str__(self): |
| 61 | buff = "<%s %s %s>\n" % (self.timestamp, self.level, self.module) |
| 62 | for line in self.msg.splitlines(): |
| 63 | buff = buff + line + "\n" |
| 64 | return buff |
| 65 | |
Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 66 | |
| 67 | def hunt_for_stacktrace(url): |
| 68 | """Return TRACE or ERROR lines out of logs.""" |
Yatin Kumbhare | 2e2c83a | 2016-05-30 22:45:58 +0530 | [diff] [blame] | 69 | req = urlreq.Request(url) |
David Kranz | 9e3c717 | 2013-10-09 21:45:31 -0400 | [diff] [blame] | 70 | req.add_header('Accept-Encoding', 'gzip') |
Yatin Kumbhare | 2e2c83a | 2016-05-30 22:45:58 +0530 | [diff] [blame] | 71 | page = urlreq.urlopen(req) |
Harshada Mangesh Kakad | b3ecf65 | 2015-12-22 09:24:26 -0800 | [diff] [blame] | 72 | buf = six.StringIO(page.read()) |
Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 73 | f = gzip.GzipFile(fileobj=buf) |
| 74 | content = f.read() |
Sean Dague | 70eef03 | 2013-03-20 13:41:15 -0400 | [diff] [blame] | 75 | |
| 76 | traces = [] |
| 77 | trace = StackTrace() |
| 78 | for line in content.splitlines(): |
| 79 | m = re.match(NOVA_REGEX, line) |
| 80 | if m: |
| 81 | data = m.groupdict() |
| 82 | if trace.not_none() and trace.is_same(data): |
| 83 | trace.append(data['msg'] + "\n") |
| 84 | else: |
| 85 | trace = StackTrace( |
| 86 | timestamp=data.get('timestamp'), |
| 87 | pid=data.get('pid'), |
| 88 | level=data.get('level'), |
| 89 | module=data.get('module'), |
| 90 | msg=data.get('msg')) |
| 91 | |
| 92 | else: |
| 93 | if trace.not_none(): |
| 94 | traces.append(trace) |
| 95 | trace = StackTrace() |
| 96 | |
| 97 | # once more at the end to pick up any stragglers |
| 98 | if trace.not_none(): |
| 99 | traces.append(trace) |
| 100 | |
| 101 | return traces |
Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 102 | |
| 103 | |
| 104 | def log_url(url, log): |
| 105 | return "%s/%s" % (url, log) |
| 106 | |
| 107 | |
| 108 | def collect_logs(url): |
Yatin Kumbhare | 2e2c83a | 2016-05-30 22:45:58 +0530 | [diff] [blame] | 109 | page = urlreq.urlopen(url) |
Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 110 | content = page.read() |
| 111 | logs = re.findall('(screen-[\w-]+\.txt\.gz)</a>', content) |
| 112 | return logs |
| 113 | |
| 114 | |
| 115 | def usage(): |
Dirk Mueller | 1db5db2 | 2013-06-23 20:21:32 +0200 | [diff] [blame] | 116 | print(""" |
Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 117 | Usage: find_stack_traces.py <logurl> |
| 118 | |
| 119 | Hunts for stack traces in a devstack run. Must provide it a base log url |
| 120 | from a tempest devstack run. Should start with http and end with /logs/. |
| 121 | |
| 122 | Returns a report listing stack traces out of the various files where |
| 123 | they are found. |
Dirk Mueller | 1db5db2 | 2013-06-23 20:21:32 +0200 | [diff] [blame] | 124 | """) |
Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 125 | sys.exit(0) |
| 126 | |
| 127 | |
Sean Dague | 70eef03 | 2013-03-20 13:41:15 -0400 | [diff] [blame] | 128 | def print_stats(items, fname, verbose=False): |
| 129 | errors = len(filter(lambda x: x.level == "ERROR", items)) |
| 130 | traces = len(filter(lambda x: x.level == "TRACE", items)) |
Dirk Mueller | 1db5db2 | 2013-06-23 20:21:32 +0200 | [diff] [blame] | 131 | print("%d ERRORS found in %s" % (errors, fname)) |
| 132 | print("%d TRACES found in %s" % (traces, fname)) |
Sean Dague | 70eef03 | 2013-03-20 13:41:15 -0400 | [diff] [blame] | 133 | |
| 134 | if verbose: |
| 135 | for item in items: |
Dirk Mueller | 1db5db2 | 2013-06-23 20:21:32 +0200 | [diff] [blame] | 136 | print(item) |
| 137 | print("\n\n") |
Sean Dague | 70eef03 | 2013-03-20 13:41:15 -0400 | [diff] [blame] | 138 | |
| 139 | |
Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 140 | def main(): |
| 141 | if len(sys.argv) == 2: |
| 142 | url = sys.argv[1] |
| 143 | loglist = collect_logs(url) |
| 144 | |
| 145 | # probably wrong base url |
| 146 | if not loglist: |
| 147 | usage() |
| 148 | |
| 149 | for log in loglist: |
| 150 | logurl = log_url(url, log) |
| 151 | traces = hunt_for_stacktrace(logurl) |
Sean Dague | 70eef03 | 2013-03-20 13:41:15 -0400 | [diff] [blame] | 152 | |
Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 153 | if traces: |
Sean Dague | 70eef03 | 2013-03-20 13:41:15 -0400 | [diff] [blame] | 154 | print_stats(traces, log, verbose=True) |
| 155 | |
Sean Dague | bcdba08 | 2013-03-12 15:14:16 -0400 | [diff] [blame] | 156 | else: |
| 157 | usage() |
| 158 | |
| 159 | if __name__ == '__main__': |
| 160 | main() |