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