blob: caad85c5885492a5029ee14821d3d853eba4d18f [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
20import os
21import re
Harshada Mangesh Kakadb3ecf652015-12-22 09:24:26 -080022import six
Yatin Kumbhare2e2c83a2016-05-30 22:45:58 +053023import six.moves.urllib.request as urlreq
David Kranz852c5c22013-10-04 15:10:15 -040024import sys
Matthew Treinish96e9e882014-06-09 18:37:19 -040025
David Kranze8e26312013-10-09 21:31:32 -040026import yaml
27
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
David Kranze8e26312013-10-09 21:31:32 -040060def process_files(file_specs, url_specs, whitelists):
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:
64 whitelist = whitelists.get(name, [])
65 with open(filename) as content:
66 if scan_content(name, content, regexp, whitelist):
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:
69 whitelist = whitelists.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)
Harshada Mangesh Kakadb3ecf652015-12-22 09:24:26 -080073 buf = six.StringIO(page.read())
David Kranze8e26312013-10-09 21:31:32 -040074 f = gzip.GzipFile(fileobj=buf)
75 if scan_content(name, f.read().splitlines(), regexp, whitelist):
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
80def 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 Kranze07cdb82013-11-27 10:53:54 -050091 if not whitelisted or dump_all_errors:
David Kranze07cdb82013-11-27 10:53:54 -050092 if not whitelisted:
93 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()
100 logs = re.findall('(screen-[\w-]+\.txt\.gz)</a>', content)
101 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...")
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 Kranz5274de42014-02-27 15:23:35 -0500145 logs_with_errors = process_files(files_to_process, urls_to_process,
146 whitelists)
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
166usage = """
167Find non-white-listed log errors in log files from a devstack-gate run.
168Log files will be searched for ERROR or CRITICAL messages. If any
169error messages do not match any of the whitelist entries contained in
170etc/whitelist.yaml, those messages will be printed to the console and
171failure will be returned. A file directory containing logs or a url to the
172log files of an OpenStack gate job can be provided.
173
174The whitelist yaml looks like:
175
176log-name:
177 - module: "a.b.c"
178 message: "regexp"
179 - module: "a.b.c"
180 message: "regexp"
181
182repeated for each log file with a whitelist.
183"""
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)