David Kranz | 852c5c2 | 2013-10-04 15:10:15 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
David Kranz | 852c5c2 | 2013-10-04 15:10:15 -0400 | [diff] [blame] | 2 | |
| 3 | # Copyright 2013 Red Hat, Inc. |
| 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 | |
David Kranz | e8e2631 | 2013-10-09 21:31:32 -0400 | [diff] [blame] | 18 | import argparse |
| 19 | import gzip |
| 20 | import os |
| 21 | import re |
| 22 | import StringIO |
David Kranz | 852c5c2 | 2013-10-04 15:10:15 -0400 | [diff] [blame] | 23 | import sys |
David Kranz | e8e2631 | 2013-10-09 21:31:32 -0400 | [diff] [blame] | 24 | import urllib2 |
| 25 | import yaml |
| 26 | |
| 27 | |
Sean Dague | 1159e52 | 2013-12-13 18:46:21 -0500 | [diff] [blame] | 28 | is_grenade = (os.environ.get('DEVSTACK_GATE_GRENADE', "0") == "1" or |
| 29 | os.environ.get('DEVSTACK_GATE_GRENADE_FORWARD', "0") == "1") |
David Kranz | 955a9e3 | 2013-12-30 12:04:17 -0500 | [diff] [blame] | 30 | dump_all_errors = True |
David Kranz | e07cdb8 | 2013-11-27 10:53:54 -0500 | [diff] [blame] | 31 | |
David Kranz | 5274de4 | 2014-02-27 15:23:35 -0500 | [diff] [blame] | 32 | # As logs are made clean, add to this set |
Sean Dague | 5d407e2 | 2014-03-18 14:31:05 -0400 | [diff] [blame^] | 33 | allowed_dirty = set([ |
| 34 | 'c-api', |
| 35 | 'ceilometer-acentral', |
| 36 | 'ceilometer-acompute', |
| 37 | 'ceilometer-alarm-evaluator', |
| 38 | 'ceilometer-anotification', |
| 39 | 'ceilometer-api', |
| 40 | 'c-vol', |
| 41 | 'g-api', |
| 42 | 'h-api', |
| 43 | 'h-eng', |
| 44 | 'ir-cond', |
| 45 | 'n-api', |
| 46 | 'n-cpu', |
| 47 | 'n-net', |
| 48 | 'n-sch', |
| 49 | 'q-agt', |
| 50 | 'q-dhcp', |
| 51 | 'q-lbaas', |
| 52 | 'q-meta', |
| 53 | 'q-metering', |
| 54 | 'q-svc', |
| 55 | 'q-vpn', |
| 56 | 's-proxy']) |
David Kranz | 5274de4 | 2014-02-27 15:23:35 -0500 | [diff] [blame] | 57 | |
David Kranz | e07cdb8 | 2013-11-27 10:53:54 -0500 | [diff] [blame] | 58 | |
David Kranz | e8e2631 | 2013-10-09 21:31:32 -0400 | [diff] [blame] | 59 | def process_files(file_specs, url_specs, whitelists): |
David Kranz | 002d684 | 2014-02-20 17:53:02 -0500 | [diff] [blame] | 60 | regexp = re.compile(r"^.* (ERROR|CRITICAL|TRACE) .*\[.*\-.*\]") |
David Kranz | 5274de4 | 2014-02-27 15:23:35 -0500 | [diff] [blame] | 61 | logs_with_errors = [] |
David Kranz | e8e2631 | 2013-10-09 21:31:32 -0400 | [diff] [blame] | 62 | for (name, filename) in file_specs: |
| 63 | whitelist = whitelists.get(name, []) |
| 64 | with open(filename) as content: |
| 65 | if scan_content(name, content, regexp, whitelist): |
David Kranz | 5274de4 | 2014-02-27 15:23:35 -0500 | [diff] [blame] | 66 | logs_with_errors.append(name) |
David Kranz | e8e2631 | 2013-10-09 21:31:32 -0400 | [diff] [blame] | 67 | for (name, url) in url_specs: |
| 68 | whitelist = whitelists.get(name, []) |
| 69 | req = urllib2.Request(url) |
| 70 | req.add_header('Accept-Encoding', 'gzip') |
| 71 | page = urllib2.urlopen(req) |
| 72 | buf = StringIO.StringIO(page.read()) |
| 73 | f = gzip.GzipFile(fileobj=buf) |
| 74 | if scan_content(name, f.read().splitlines(), regexp, whitelist): |
David Kranz | 5274de4 | 2014-02-27 15:23:35 -0500 | [diff] [blame] | 75 | logs_with_errors.append(name) |
| 76 | return logs_with_errors |
David Kranz | e8e2631 | 2013-10-09 21:31:32 -0400 | [diff] [blame] | 77 | |
| 78 | |
| 79 | def scan_content(name, content, regexp, whitelist): |
| 80 | had_errors = False |
David Kranz | e07cdb8 | 2013-11-27 10:53:54 -0500 | [diff] [blame] | 81 | print_log_name = True |
David Kranz | e8e2631 | 2013-10-09 21:31:32 -0400 | [diff] [blame] | 82 | for line in content: |
| 83 | if not line.startswith("Stderr:") and regexp.match(line): |
| 84 | whitelisted = False |
| 85 | for w in whitelist: |
| 86 | pat = ".*%s.*%s.*" % (w['module'].replace('.', '\\.'), |
| 87 | w['message']) |
| 88 | if re.match(pat, line): |
| 89 | whitelisted = True |
| 90 | break |
David Kranz | e07cdb8 | 2013-11-27 10:53:54 -0500 | [diff] [blame] | 91 | if not whitelisted or dump_all_errors: |
David Kranz | 78dc5ab | 2013-11-29 12:33:02 -0500 | [diff] [blame] | 92 | if print_log_name: |
Sean Dague | 5d407e2 | 2014-03-18 14:31:05 -0400 | [diff] [blame^] | 93 | print("Log File Has Errors: %s" % name) |
David Kranz | e07cdb8 | 2013-11-27 10:53:54 -0500 | [diff] [blame] | 94 | print_log_name = False |
| 95 | if not whitelisted: |
| 96 | had_errors = True |
David Kranz | 955a9e3 | 2013-12-30 12:04:17 -0500 | [diff] [blame] | 97 | print("*** Not Whitelisted ***"), |
Sean Dague | 5d407e2 | 2014-03-18 14:31:05 -0400 | [diff] [blame^] | 98 | print(line.rstrip()) |
David Kranz | e8e2631 | 2013-10-09 21:31:32 -0400 | [diff] [blame] | 99 | return had_errors |
| 100 | |
| 101 | |
| 102 | def collect_url_logs(url): |
| 103 | page = urllib2.urlopen(url) |
| 104 | content = page.read() |
| 105 | logs = re.findall('(screen-[\w-]+\.txt\.gz)</a>', content) |
| 106 | return logs |
| 107 | |
| 108 | |
| 109 | def main(opts): |
| 110 | if opts.directory and opts.url or not (opts.directory or opts.url): |
| 111 | print("Must provide exactly one of -d or -u") |
| 112 | exit(1) |
| 113 | print("Checking logs...") |
| 114 | WHITELIST_FILE = os.path.join( |
| 115 | os.path.abspath(os.path.dirname(os.path.dirname(__file__))), |
| 116 | "etc", "whitelist.yaml") |
| 117 | |
| 118 | file_matcher = re.compile(r".*screen-([\w-]+)\.log") |
| 119 | files = [] |
| 120 | if opts.directory: |
| 121 | d = opts.directory |
| 122 | for f in os.listdir(d): |
| 123 | files.append(os.path.join(d, f)) |
| 124 | files_to_process = [] |
| 125 | for f in files: |
| 126 | m = file_matcher.match(f) |
| 127 | if m: |
| 128 | files_to_process.append((m.group(1), f)) |
| 129 | |
| 130 | url_matcher = re.compile(r".*screen-([\w-]+)\.txt\.gz") |
| 131 | urls = [] |
| 132 | if opts.url: |
| 133 | for logfile in collect_url_logs(opts.url): |
| 134 | urls.append("%s/%s" % (opts.url, logfile)) |
| 135 | urls_to_process = [] |
| 136 | for u in urls: |
| 137 | m = url_matcher.match(u) |
| 138 | if m: |
| 139 | urls_to_process.append((m.group(1), u)) |
| 140 | |
| 141 | whitelists = {} |
| 142 | with open(WHITELIST_FILE) as stream: |
| 143 | loaded = yaml.safe_load(stream) |
| 144 | if loaded: |
| 145 | for (name, l) in loaded.iteritems(): |
| 146 | for w in l: |
| 147 | assert 'module' in w, 'no module in %s' % name |
| 148 | assert 'message' in w, 'no message in %s' % name |
| 149 | whitelists = loaded |
David Kranz | 5274de4 | 2014-02-27 15:23:35 -0500 | [diff] [blame] | 150 | logs_with_errors = process_files(files_to_process, urls_to_process, |
| 151 | whitelists) |
| 152 | if logs_with_errors: |
David Kranz | e8e2631 | 2013-10-09 21:31:32 -0400 | [diff] [blame] | 153 | print("Logs have errors") |
David Kranz | 5274de4 | 2014-02-27 15:23:35 -0500 | [diff] [blame] | 154 | if is_grenade: |
| 155 | print("Currently not failing grenade runs with errors") |
David Kranz | e8e2631 | 2013-10-09 21:31:32 -0400 | [diff] [blame] | 156 | return 0 |
David Kranz | 5274de4 | 2014-02-27 15:23:35 -0500 | [diff] [blame] | 157 | failed = False |
| 158 | for log in logs_with_errors: |
Sean Dague | 5d407e2 | 2014-03-18 14:31:05 -0400 | [diff] [blame^] | 159 | if log not in allowed_dirty: |
| 160 | print("Log: %s not allowed to have ERRORS or TRACES" % log) |
David Kranz | 5274de4 | 2014-02-27 15:23:35 -0500 | [diff] [blame] | 161 | failed = True |
| 162 | if failed: |
| 163 | return 1 |
| 164 | print("ok") |
| 165 | return 0 |
David Kranz | e8e2631 | 2013-10-09 21:31:32 -0400 | [diff] [blame] | 166 | |
| 167 | usage = """ |
| 168 | Find non-white-listed log errors in log files from a devstack-gate run. |
| 169 | Log files will be searched for ERROR or CRITICAL messages. If any |
| 170 | error messages do not match any of the whitelist entries contained in |
| 171 | etc/whitelist.yaml, those messages will be printed to the console and |
| 172 | failure will be returned. A file directory containing logs or a url to the |
| 173 | log files of an OpenStack gate job can be provided. |
| 174 | |
| 175 | The whitelist yaml looks like: |
| 176 | |
| 177 | log-name: |
| 178 | - module: "a.b.c" |
| 179 | message: "regexp" |
| 180 | - module: "a.b.c" |
| 181 | message: "regexp" |
| 182 | |
| 183 | repeated for each log file with a whitelist. |
| 184 | """ |
| 185 | |
| 186 | parser = argparse.ArgumentParser(description=usage) |
| 187 | parser.add_argument('-d', '--directory', |
| 188 | help="Directory containing log files") |
| 189 | parser.add_argument('-u', '--url', |
| 190 | help="url containing logs from an OpenStack gate job") |
David Kranz | 852c5c2 | 2013-10-04 15:10:15 -0400 | [diff] [blame] | 191 | |
| 192 | if __name__ == "__main__": |
David Kranz | e8e2631 | 2013-10-09 21:31:32 -0400 | [diff] [blame] | 193 | try: |
| 194 | sys.exit(main(parser.parse_args())) |
| 195 | except Exception as e: |
| 196 | print("Failure in script: %s" % e) |
| 197 | # Don't fail if there is a problem with the script. |
| 198 | sys.exit(0) |