blob: cc74b17ed92c7f440797d3be028f06f2b3253399 [file] [log] [blame]
David Kranz852c5c22013-10-04 15:10:15 -04001#!/usr/bin/env python
David Kranz852c5c22013-10-04 15:10:15 -04002
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 Kranze8e26312013-10-09 21:31:32 -040018import argparse
19import gzip
songwenping0fa20692021-01-05 06:30:18 +000020import io
David Kranze8e26312013-10-09 21:31:32 -040021import os
22import re
David Kranz852c5c22013-10-04 15:10:15 -040023import sys
songwenping99d6e002021-01-05 03:07:46 +000024import urllib.request as urlreq
Matthew Treinish96e9e882014-06-09 18:37:19 -040025
Masayuki Igawa134d9f72017-02-10 18:05:26 +090026import six
David Kranze8e26312013-10-09 21:31:32 -040027import yaml
28
Clark Boylana5c669d2014-09-03 12:29:03 -070029# DEVSTACK_GATE_GRENADE is either unset if grenade is not running
30# or a string describing what type of grenade run to perform.
31is_grenade = os.environ.get('DEVSTACK_GATE_GRENADE') is not None
David Kranz955a9e32013-12-30 12:04:17 -050032dump_all_errors = True
David Kranze07cdb82013-11-27 10:53:54 -050033
Ihar Hrachyshka226de0b2014-10-31 13:56:36 +010034# As logs are made clean, remove from this set
Sean Dague5d407e22014-03-18 14:31:05 -040035allowed_dirty = set([
36 'c-api',
37 'ceilometer-acentral',
38 'ceilometer-acompute',
39 'ceilometer-alarm-evaluator',
40 'ceilometer-anotification',
41 'ceilometer-api',
Sean Daguee2cda412014-03-26 15:39:05 -040042 'ceilometer-collector',
Sean Dague5d407e22014-03-18 14:31:05 -040043 'c-vol',
44 'g-api',
45 'h-api',
46 'h-eng',
47 'ir-cond',
48 'n-api',
49 'n-cpu',
50 'n-net',
Sean Dague5d407e22014-03-18 14:31:05 -040051 'q-agt',
52 'q-dhcp',
53 'q-lbaas',
54 'q-meta',
55 'q-metering',
56 'q-svc',
Sean Dague5d407e22014-03-18 14:31:05 -040057 's-proxy'])
David Kranz5274de42014-02-27 15:23:35 -050058
David Kranze07cdb82013-11-27 10:53:54 -050059
Martin Kopecdc844232020-12-24 15:57:53 +000060def process_files(file_specs, url_specs, allow_lists):
David Kranz002d6842014-02-20 17:53:02 -050061 regexp = re.compile(r"^.* (ERROR|CRITICAL|TRACE) .*\[.*\-.*\]")
David Kranz5274de42014-02-27 15:23:35 -050062 logs_with_errors = []
David Kranze8e26312013-10-09 21:31:32 -040063 for (name, filename) in file_specs:
Martin Kopecdc844232020-12-24 15:57:53 +000064 allow_list = allow_lists.get(name, [])
David Kranze8e26312013-10-09 21:31:32 -040065 with open(filename) as content:
Martin Kopecdc844232020-12-24 15:57:53 +000066 if scan_content(content, regexp, allow_list):
David Kranz5274de42014-02-27 15:23:35 -050067 logs_with_errors.append(name)
David Kranze8e26312013-10-09 21:31:32 -040068 for (name, url) in url_specs:
Martin Kopecdc844232020-12-24 15:57:53 +000069 allow_list = allow_lists.get(name, [])
Yatin Kumbhare2e2c83a2016-05-30 22:45:58 +053070 req = urlreq.Request(url)
David Kranze8e26312013-10-09 21:31:32 -040071 req.add_header('Accept-Encoding', 'gzip')
Yatin Kumbhare2e2c83a2016-05-30 22:45:58 +053072 page = urlreq.urlopen(req)
songwenping0fa20692021-01-05 06:30:18 +000073 buf = io.StringIO(page.read())
David Kranze8e26312013-10-09 21:31:32 -040074 f = gzip.GzipFile(fileobj=buf)
Martin Kopecdc844232020-12-24 15:57:53 +000075 if scan_content(f.read().splitlines(), regexp, allow_list):
David Kranz5274de42014-02-27 15:23:35 -050076 logs_with_errors.append(name)
77 return logs_with_errors
David Kranze8e26312013-10-09 21:31:32 -040078
79
Martin Kopecdc844232020-12-24 15:57:53 +000080def scan_content(content, regexp, allow_list):
David Kranze8e26312013-10-09 21:31:32 -040081 had_errors = False
82 for line in content:
83 if not line.startswith("Stderr:") and regexp.match(line):
Martin Kopecdc844232020-12-24 15:57:53 +000084 allowed = False
85 for w in allow_list:
David Kranze8e26312013-10-09 21:31:32 -040086 pat = ".*%s.*%s.*" % (w['module'].replace('.', '\\.'),
87 w['message'])
88 if re.match(pat, line):
Martin Kopecdc844232020-12-24 15:57:53 +000089 allowed = True
David Kranze8e26312013-10-09 21:31:32 -040090 break
Martin Kopecdc844232020-12-24 15:57:53 +000091 if not allowed or dump_all_errors:
92 if not allowed:
David Kranze07cdb82013-11-27 10:53:54 -050093 had_errors = True
David Kranze8e26312013-10-09 21:31:32 -040094 return had_errors
95
96
97def collect_url_logs(url):
Yatin Kumbhare2e2c83a2016-05-30 22:45:58 +053098 page = urlreq.urlopen(url)
David Kranze8e26312013-10-09 21:31:32 -040099 content = page.read()
Stephen Finucane7f4a6212018-07-06 13:58:21 +0100100 logs = re.findall(r'(screen-[\w-]+\.txt\.gz)</a>', content)
David Kranze8e26312013-10-09 21:31:32 -0400101 return logs
102
103
104def 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")
caoyueab333022016-01-25 16:45:21 +0800107 return 1
David Kranze8e26312013-10-09 21:31:32 -0400108 print("Checking logs...")
Martin Kopecdc844232020-12-24 15:57:53 +0000109 ALLOW_LIST_FILE = os.path.join(
David Kranze8e26312013-10-09 21:31:32 -0400110 os.path.abspath(os.path.dirname(os.path.dirname(__file__))),
Martin Kopecdc844232020-12-24 15:57:53 +0000111 "etc", "allow-list.yaml")
David Kranze8e26312013-10-09 21:31:32 -0400112
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
Martin Kopecdc844232020-12-24 15:57:53 +0000136 allow_lists = {}
137 with open(ALLOW_LIST_FILE) as stream:
David Kranze8e26312013-10-09 21:31:32 -0400138 loaded = yaml.safe_load(stream)
139 if loaded:
Chandan Kumar0601be12017-06-11 20:50:43 +0530140 for (name, l) in six.iteritems(loaded):
David Kranze8e26312013-10-09 21:31:32 -0400141 for w in l:
142 assert 'module' in w, 'no module in %s' % name
143 assert 'message' in w, 'no message in %s' % name
Martin Kopecdc844232020-12-24 15:57:53 +0000144 allow_lists = loaded
David Kranz5274de42014-02-27 15:23:35 -0500145 logs_with_errors = process_files(files_to_process, urls_to_process,
Martin Kopecdc844232020-12-24 15:57:53 +0000146 allow_lists)
Matthew Treinish11792052014-09-03 14:53:16 -0400147
David Kranz5274de42014-02-27 15:23:35 -0500148 failed = False
Matthew Treinish11792052014-09-03 14:53:16 -0400149 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 Kranz5274de42014-02-27 15:23:35 -0500158 if failed:
Matthew Treinish11792052014-09-03 14:53:16 -0400159 if is_grenade:
160 print("Currently not failing grenade runs with errors")
161 return 0
David Kranz5274de42014-02-27 15:23:35 -0500162 return 1
163 print("ok")
164 return 0
David Kranze8e26312013-10-09 21:31:32 -0400165
Stephen Finucane7f4a6212018-07-06 13:58:21 +0100166
David Kranze8e26312013-10-09 21:31:32 -0400167usage = """
Martin Kopecdc844232020-12-24 15:57:53 +0000168Find non-allow-listed log errors in log files from a devstack-gate run.
David Kranze8e26312013-10-09 21:31:32 -0400169Log files will be searched for ERROR or CRITICAL messages. If any
Martin Kopecdc844232020-12-24 15:57:53 +0000170error messages do not match any of the allow-list entries contained in
171etc/allow-list.yaml, those messages will be printed to the console and
David Kranze8e26312013-10-09 21:31:32 -0400172failure will be returned. A file directory containing logs or a url to the
173log files of an OpenStack gate job can be provided.
174
Martin Kopecdc844232020-12-24 15:57:53 +0000175The allow-list yaml looks like:
David Kranze8e26312013-10-09 21:31:32 -0400176
177log-name:
178 - module: "a.b.c"
179 message: "regexp"
180 - module: "a.b.c"
181 message: "regexp"
182
Martin Kopecdc844232020-12-24 15:57:53 +0000183repeated for each log file with an allow-list.
David Kranze8e26312013-10-09 21:31:32 -0400184"""
185
186parser = argparse.ArgumentParser(description=usage)
187parser.add_argument('-d', '--directory',
188 help="Directory containing log files")
189parser.add_argument('-u', '--url',
190 help="url containing logs from an OpenStack gate job")
David Kranz852c5c22013-10-04 15:10:15 -0400191
192if __name__ == "__main__":
David Kranze8e26312013-10-09 21:31:32 -0400193 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)