blob: 917aaaf90fb821de3e85c6daabdc2ccbe34ea526 [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
David Kranze07cdb82013-11-27 10:53:54 -050083 print_log_name = True
David Kranze8e26312013-10-09 21:31:32 -040084 for line in content:
85 if not line.startswith("Stderr:") and regexp.match(line):
86 whitelisted = False
87 for w in whitelist:
88 pat = ".*%s.*%s.*" % (w['module'].replace('.', '\\.'),
89 w['message'])
90 if re.match(pat, line):
91 whitelisted = True
92 break
David Kranze07cdb82013-11-27 10:53:54 -050093 if not whitelisted or dump_all_errors:
David Kranz78dc5ab2013-11-29 12:33:02 -050094 if print_log_name:
Sean Daguee2cda412014-03-26 15:39:05 -040095 print("\nLog File Has Errors: %s" % name)
David Kranze07cdb82013-11-27 10:53:54 -050096 print_log_name = False
97 if not whitelisted:
98 had_errors = True
David Kranz955a9e32013-12-30 12:04:17 -050099 print("*** Not Whitelisted ***"),
Sean Dague5d407e22014-03-18 14:31:05 -0400100 print(line.rstrip())
David Kranze8e26312013-10-09 21:31:32 -0400101 return had_errors
102
103
104def collect_url_logs(url):
105 page = urllib2.urlopen(url)
106 content = page.read()
107 logs = re.findall('(screen-[\w-]+\.txt\.gz)</a>', content)
108 return logs
109
110
111def main(opts):
112 if opts.directory and opts.url or not (opts.directory or opts.url):
113 print("Must provide exactly one of -d or -u")
114 exit(1)
115 print("Checking logs...")
116 WHITELIST_FILE = os.path.join(
117 os.path.abspath(os.path.dirname(os.path.dirname(__file__))),
118 "etc", "whitelist.yaml")
119
120 file_matcher = re.compile(r".*screen-([\w-]+)\.log")
121 files = []
122 if opts.directory:
123 d = opts.directory
124 for f in os.listdir(d):
125 files.append(os.path.join(d, f))
126 files_to_process = []
127 for f in files:
128 m = file_matcher.match(f)
129 if m:
130 files_to_process.append((m.group(1), f))
131
132 url_matcher = re.compile(r".*screen-([\w-]+)\.txt\.gz")
133 urls = []
134 if opts.url:
135 for logfile in collect_url_logs(opts.url):
136 urls.append("%s/%s" % (opts.url, logfile))
137 urls_to_process = []
138 for u in urls:
139 m = url_matcher.match(u)
140 if m:
141 urls_to_process.append((m.group(1), u))
142
143 whitelists = {}
144 with open(WHITELIST_FILE) as stream:
145 loaded = yaml.safe_load(stream)
146 if loaded:
147 for (name, l) in loaded.iteritems():
148 for w in l:
149 assert 'module' in w, 'no module in %s' % name
150 assert 'message' in w, 'no message in %s' % name
151 whitelists = loaded
David Kranz5274de42014-02-27 15:23:35 -0500152 logs_with_errors = process_files(files_to_process, urls_to_process,
153 whitelists)
154 if logs_with_errors:
David Kranze8e26312013-10-09 21:31:32 -0400155 print("Logs have errors")
David Kranz5274de42014-02-27 15:23:35 -0500156 if is_grenade:
157 print("Currently not failing grenade runs with errors")
David Kranze8e26312013-10-09 21:31:32 -0400158 return 0
David Kranz5274de42014-02-27 15:23:35 -0500159 failed = False
160 for log in logs_with_errors:
Sean Dague5d407e22014-03-18 14:31:05 -0400161 if log not in allowed_dirty:
162 print("Log: %s not allowed to have ERRORS or TRACES" % log)
David Kranz5274de42014-02-27 15:23:35 -0500163 failed = True
164 if failed:
165 return 1
166 print("ok")
167 return 0
David Kranze8e26312013-10-09 21:31:32 -0400168
169usage = """
170Find non-white-listed log errors in log files from a devstack-gate run.
171Log files will be searched for ERROR or CRITICAL messages. If any
172error messages do not match any of the whitelist entries contained in
173etc/whitelist.yaml, those messages will be printed to the console and
174failure will be returned. A file directory containing logs or a url to the
175log files of an OpenStack gate job can be provided.
176
177The whitelist yaml looks like:
178
179log-name:
180 - module: "a.b.c"
181 message: "regexp"
182 - module: "a.b.c"
183 message: "regexp"
184
185repeated for each log file with a whitelist.
186"""
187
188parser = argparse.ArgumentParser(description=usage)
189parser.add_argument('-d', '--directory',
190 help="Directory containing log files")
191parser.add_argument('-u', '--url',
192 help="url containing logs from an OpenStack gate job")
David Kranz852c5c22013-10-04 15:10:15 -0400193
194if __name__ == "__main__":
David Kranze8e26312013-10-09 21:31:32 -0400195 try:
196 sys.exit(main(parser.parse_args()))
197 except Exception as e:
198 print("Failure in script: %s" % e)
199 # Don't fail if there is a problem with the script.
200 sys.exit(0)