blob: 7cf9d85734d29dde39bc701d7a63b8b715cd247b [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
22import StringIO
David Kranz852c5c22013-10-04 15:10:15 -040023import sys
David Kranze8e26312013-10-09 21:31:32 -040024import urllib2
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
David Kranz5274de42014-02-27 15:23:35 -050034# As logs are made clean, add to 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',
57 'q-vpn',
58 's-proxy'])
David Kranz5274de42014-02-27 15:23:35 -050059
David Kranze07cdb82013-11-27 10:53:54 -050060
David Kranze8e26312013-10-09 21:31:32 -040061def process_files(file_specs, url_specs, whitelists):
David Kranz002d6842014-02-20 17:53:02 -050062 regexp = re.compile(r"^.* (ERROR|CRITICAL|TRACE) .*\[.*\-.*\]")
David Kranz5274de42014-02-27 15:23:35 -050063 logs_with_errors = []
David Kranze8e26312013-10-09 21:31:32 -040064 for (name, filename) in file_specs:
65 whitelist = whitelists.get(name, [])
66 with open(filename) as content:
67 if scan_content(name, content, regexp, whitelist):
David Kranz5274de42014-02-27 15:23:35 -050068 logs_with_errors.append(name)
David Kranze8e26312013-10-09 21:31:32 -040069 for (name, url) in url_specs:
70 whitelist = whitelists.get(name, [])
71 req = urllib2.Request(url)
72 req.add_header('Accept-Encoding', 'gzip')
73 page = urllib2.urlopen(req)
74 buf = StringIO.StringIO(page.read())
75 f = gzip.GzipFile(fileobj=buf)
76 if scan_content(name, f.read().splitlines(), regexp, whitelist):
David Kranz5274de42014-02-27 15:23:35 -050077 logs_with_errors.append(name)
78 return logs_with_errors
David Kranze8e26312013-10-09 21:31:32 -040079
80
81def scan_content(name, content, regexp, whitelist):
82 had_errors = False
83 for line in content:
84 if not line.startswith("Stderr:") and regexp.match(line):
85 whitelisted = False
86 for w in whitelist:
87 pat = ".*%s.*%s.*" % (w['module'].replace('.', '\\.'),
88 w['message'])
89 if re.match(pat, line):
90 whitelisted = True
91 break
David Kranze07cdb82013-11-27 10:53:54 -050092 if not whitelisted or dump_all_errors:
David Kranze07cdb82013-11-27 10:53:54 -050093 if not whitelisted:
94 had_errors = True
David Kranze8e26312013-10-09 21:31:32 -040095 return had_errors
96
97
98def collect_url_logs(url):
99 page = urllib2.urlopen(url)
100 content = page.read()
101 logs = re.findall('(screen-[\w-]+\.txt\.gz)</a>', content)
102 return logs
103
104
105def main(opts):
106 if opts.directory and opts.url or not (opts.directory or opts.url):
107 print("Must provide exactly one of -d or -u")
108 exit(1)
109 print("Checking logs...")
110 WHITELIST_FILE = os.path.join(
111 os.path.abspath(os.path.dirname(os.path.dirname(__file__))),
112 "etc", "whitelist.yaml")
113
114 file_matcher = re.compile(r".*screen-([\w-]+)\.log")
115 files = []
116 if opts.directory:
117 d = opts.directory
118 for f in os.listdir(d):
119 files.append(os.path.join(d, f))
120 files_to_process = []
121 for f in files:
122 m = file_matcher.match(f)
123 if m:
124 files_to_process.append((m.group(1), f))
125
126 url_matcher = re.compile(r".*screen-([\w-]+)\.txt\.gz")
127 urls = []
128 if opts.url:
129 for logfile in collect_url_logs(opts.url):
130 urls.append("%s/%s" % (opts.url, logfile))
131 urls_to_process = []
132 for u in urls:
133 m = url_matcher.match(u)
134 if m:
135 urls_to_process.append((m.group(1), u))
136
137 whitelists = {}
138 with open(WHITELIST_FILE) as stream:
139 loaded = yaml.safe_load(stream)
140 if loaded:
141 for (name, l) in loaded.iteritems():
142 for w in l:
143 assert 'module' in w, 'no module in %s' % name
144 assert 'message' in w, 'no message in %s' % name
145 whitelists = loaded
David Kranz5274de42014-02-27 15:23:35 -0500146 logs_with_errors = process_files(files_to_process, urls_to_process,
147 whitelists)
Matthew Treinish11792052014-09-03 14:53:16 -0400148
David Kranz5274de42014-02-27 15:23:35 -0500149 failed = False
Matthew Treinish11792052014-09-03 14:53:16 -0400150 if logs_with_errors:
151 log_files = set(logs_with_errors)
152 for log in log_files:
153 msg = '%s log file has errors' % log
154 if log not in allowed_dirty:
155 msg += ' and is not allowed to have them'
156 failed = True
157 print(msg)
158 print("\nPlease check the respective log files to see the errors")
David Kranz5274de42014-02-27 15:23:35 -0500159 if failed:
Matthew Treinish11792052014-09-03 14:53:16 -0400160 if is_grenade:
161 print("Currently not failing grenade runs with errors")
162 return 0
David Kranz5274de42014-02-27 15:23:35 -0500163 return 1
164 print("ok")
165 return 0
David Kranze8e26312013-10-09 21:31:32 -0400166
167usage = """
168Find non-white-listed log errors in log files from a devstack-gate run.
169Log files will be searched for ERROR or CRITICAL messages. If any
170error messages do not match any of the whitelist entries contained in
171etc/whitelist.yaml, those messages will be printed to the console and
172failure will be returned. A file directory containing logs or a url to the
173log files of an OpenStack gate job can be provided.
174
175The whitelist yaml looks like:
176
177log-name:
178 - module: "a.b.c"
179 message: "regexp"
180 - module: "a.b.c"
181 message: "regexp"
182
183repeated for each log file with a whitelist.
184"""
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)