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