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