blob: 8ea94e84b22b5967d3a583315aa7e44c6d572e50 [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
David Kranze8e26312013-10-09 21:31:32 -040026import yaml
27
Clark Boylana5c669d2014-09-03 12:29:03 -070028# DEVSTACK_GATE_GRENADE is either unset if grenade is not running
29# or a string describing what type of grenade run to perform.
30is_grenade = os.environ.get('DEVSTACK_GATE_GRENADE') is not None
David Kranz955a9e32013-12-30 12:04:17 -050031dump_all_errors = True
David Kranze07cdb82013-11-27 10:53:54 -050032
Ihar Hrachyshka226de0b2014-10-31 13:56:36 +010033# As logs are made clean, remove from this set
Sean Dague5d407e22014-03-18 14:31:05 -040034allowed_dirty = set([
35 'c-api',
36 'ceilometer-acentral',
37 'ceilometer-acompute',
38 'ceilometer-alarm-evaluator',
39 'ceilometer-anotification',
40 'ceilometer-api',
Sean Daguee2cda412014-03-26 15:39:05 -040041 'ceilometer-collector',
Sean Dague5d407e22014-03-18 14:31:05 -040042 'c-vol',
43 'g-api',
44 'h-api',
45 'h-eng',
46 'ir-cond',
47 'n-api',
48 'n-cpu',
49 'n-net',
Sean Dague5d407e22014-03-18 14:31:05 -040050 'q-agt',
51 'q-dhcp',
52 'q-lbaas',
53 'q-meta',
54 'q-metering',
55 'q-svc',
Sean Dague5d407e22014-03-18 14:31:05 -040056 's-proxy'])
David Kranz5274de42014-02-27 15:23:35 -050057
David Kranze07cdb82013-11-27 10:53:54 -050058
Martin Kopecdc844232020-12-24 15:57:53 +000059def process_files(file_specs, url_specs, allow_lists):
David Kranz002d6842014-02-20 17:53:02 -050060 regexp = re.compile(r"^.* (ERROR|CRITICAL|TRACE) .*\[.*\-.*\]")
David Kranz5274de42014-02-27 15:23:35 -050061 logs_with_errors = []
David Kranze8e26312013-10-09 21:31:32 -040062 for (name, filename) in file_specs:
Martin Kopecdc844232020-12-24 15:57:53 +000063 allow_list = allow_lists.get(name, [])
David Kranze8e26312013-10-09 21:31:32 -040064 with open(filename) as content:
Martin Kopecdc844232020-12-24 15:57:53 +000065 if scan_content(content, regexp, allow_list):
David Kranz5274de42014-02-27 15:23:35 -050066 logs_with_errors.append(name)
David Kranze8e26312013-10-09 21:31:32 -040067 for (name, url) in url_specs:
Martin Kopecdc844232020-12-24 15:57:53 +000068 allow_list = allow_lists.get(name, [])
Yatin Kumbhare2e2c83a2016-05-30 22:45:58 +053069 req = urlreq.Request(url)
David Kranze8e26312013-10-09 21:31:32 -040070 req.add_header('Accept-Encoding', 'gzip')
Yatin Kumbhare2e2c83a2016-05-30 22:45:58 +053071 page = urlreq.urlopen(req)
songwenping0fa20692021-01-05 06:30:18 +000072 buf = io.StringIO(page.read())
David Kranze8e26312013-10-09 21:31:32 -040073 f = gzip.GzipFile(fileobj=buf)
Martin Kopecdc844232020-12-24 15:57:53 +000074 if scan_content(f.read().splitlines(), regexp, allow_list):
David Kranz5274de42014-02-27 15:23:35 -050075 logs_with_errors.append(name)
76 return logs_with_errors
David Kranze8e26312013-10-09 21:31:32 -040077
78
Martin Kopecdc844232020-12-24 15:57:53 +000079def scan_content(content, regexp, allow_list):
David Kranze8e26312013-10-09 21:31:32 -040080 had_errors = False
81 for line in content:
82 if not line.startswith("Stderr:") and regexp.match(line):
Martin Kopecdc844232020-12-24 15:57:53 +000083 allowed = False
84 for w in allow_list:
David Kranze8e26312013-10-09 21:31:32 -040085 pat = ".*%s.*%s.*" % (w['module'].replace('.', '\\.'),
86 w['message'])
87 if re.match(pat, line):
Martin Kopecdc844232020-12-24 15:57:53 +000088 allowed = True
David Kranze8e26312013-10-09 21:31:32 -040089 break
Martin Kopecdc844232020-12-24 15:57:53 +000090 if not allowed or dump_all_errors:
91 if not allowed:
David Kranze07cdb82013-11-27 10:53:54 -050092 had_errors = True
David Kranze8e26312013-10-09 21:31:32 -040093 return had_errors
94
95
96def collect_url_logs(url):
Yatin Kumbhare2e2c83a2016-05-30 22:45:58 +053097 page = urlreq.urlopen(url)
David Kranze8e26312013-10-09 21:31:32 -040098 content = page.read()
Stephen Finucane7f4a6212018-07-06 13:58:21 +010099 logs = re.findall(r'(screen-[\w-]+\.txt\.gz)</a>', content)
David Kranze8e26312013-10-09 21:31:32 -0400100 return logs
101
102
103def main(opts):
104 if opts.directory and opts.url or not (opts.directory or opts.url):
105 print("Must provide exactly one of -d or -u")
caoyueab333022016-01-25 16:45:21 +0800106 return 1
David Kranze8e26312013-10-09 21:31:32 -0400107 print("Checking logs...")
Martin Kopecdc844232020-12-24 15:57:53 +0000108 ALLOW_LIST_FILE = os.path.join(
David Kranze8e26312013-10-09 21:31:32 -0400109 os.path.abspath(os.path.dirname(os.path.dirname(__file__))),
Martin Kopecdc844232020-12-24 15:57:53 +0000110 "etc", "allow-list.yaml")
David Kranze8e26312013-10-09 21:31:32 -0400111
112 file_matcher = re.compile(r".*screen-([\w-]+)\.log")
113 files = []
114 if opts.directory:
115 d = opts.directory
116 for f in os.listdir(d):
117 files.append(os.path.join(d, f))
118 files_to_process = []
119 for f in files:
120 m = file_matcher.match(f)
121 if m:
122 files_to_process.append((m.group(1), f))
123
124 url_matcher = re.compile(r".*screen-([\w-]+)\.txt\.gz")
125 urls = []
126 if opts.url:
127 for logfile in collect_url_logs(opts.url):
128 urls.append("%s/%s" % (opts.url, logfile))
129 urls_to_process = []
130 for u in urls:
131 m = url_matcher.match(u)
132 if m:
133 urls_to_process.append((m.group(1), u))
134
Martin Kopecdc844232020-12-24 15:57:53 +0000135 allow_lists = {}
136 with open(ALLOW_LIST_FILE) as stream:
David Kranze8e26312013-10-09 21:31:32 -0400137 loaded = yaml.safe_load(stream)
138 if loaded:
songwenpingfe0e0a62022-04-19 11:13:55 +0800139 for (name, l) in loaded.items():
David Kranze8e26312013-10-09 21:31:32 -0400140 for w in l:
141 assert 'module' in w, 'no module in %s' % name
142 assert 'message' in w, 'no message in %s' % name
Martin Kopecdc844232020-12-24 15:57:53 +0000143 allow_lists = loaded
David Kranz5274de42014-02-27 15:23:35 -0500144 logs_with_errors = process_files(files_to_process, urls_to_process,
Martin Kopecdc844232020-12-24 15:57:53 +0000145 allow_lists)
Matthew Treinish11792052014-09-03 14:53:16 -0400146
David Kranz5274de42014-02-27 15:23:35 -0500147 failed = False
Matthew Treinish11792052014-09-03 14:53:16 -0400148 if logs_with_errors:
149 log_files = set(logs_with_errors)
150 for log in log_files:
151 msg = '%s log file has errors' % log
152 if log not in allowed_dirty:
153 msg += ' and is not allowed to have them'
154 failed = True
155 print(msg)
156 print("\nPlease check the respective log files to see the errors")
David Kranz5274de42014-02-27 15:23:35 -0500157 if failed:
Matthew Treinish11792052014-09-03 14:53:16 -0400158 if is_grenade:
159 print("Currently not failing grenade runs with errors")
160 return 0
David Kranz5274de42014-02-27 15:23:35 -0500161 return 1
162 print("ok")
163 return 0
David Kranze8e26312013-10-09 21:31:32 -0400164
Stephen Finucane7f4a6212018-07-06 13:58:21 +0100165
David Kranze8e26312013-10-09 21:31:32 -0400166usage = """
Martin Kopecdc844232020-12-24 15:57:53 +0000167Find non-allow-listed log errors in log files from a devstack-gate run.
David Kranze8e26312013-10-09 21:31:32 -0400168Log files will be searched for ERROR or CRITICAL messages. If any
Martin Kopecdc844232020-12-24 15:57:53 +0000169error messages do not match any of the allow-list entries contained in
170etc/allow-list.yaml, those messages will be printed to the console and
David Kranze8e26312013-10-09 21:31:32 -0400171failure will be returned. A file directory containing logs or a url to the
172log files of an OpenStack gate job can be provided.
173
Martin Kopecdc844232020-12-24 15:57:53 +0000174The allow-list yaml looks like:
David Kranze8e26312013-10-09 21:31:32 -0400175
176log-name:
177 - module: "a.b.c"
178 message: "regexp"
179 - module: "a.b.c"
180 message: "regexp"
181
Martin Kopecdc844232020-12-24 15:57:53 +0000182repeated for each log file with an allow-list.
David Kranze8e26312013-10-09 21:31:32 -0400183"""
184
185parser = argparse.ArgumentParser(description=usage)
186parser.add_argument('-d', '--directory',
187 help="Directory containing log files")
188parser.add_argument('-u', '--url',
189 help="url containing logs from an OpenStack gate job")
David Kranz852c5c22013-10-04 15:10:15 -0400190
191if __name__ == "__main__":
David Kranze8e26312013-10-09 21:31:32 -0400192 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)