blob: 98e079a779d8d21fa43dbcd8221e7743548e38e7 [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
25import yaml
26
27
David Kranze07cdb82013-11-27 10:53:54 -050028is_neutron = os.environ.get('DEVSTACK_GATE_NEUTRON', "0") == "1"
Sean Dague1159e522013-12-13 18:46:21 -050029is_grenade = (os.environ.get('DEVSTACK_GATE_GRENADE', "0") == "1" or
30 os.environ.get('DEVSTACK_GATE_GRENADE_FORWARD', "0") == "1")
David Kranz955a9e32013-12-30 12:04:17 -050031dump_all_errors = True
David Kranze07cdb82013-11-27 10:53:54 -050032
33
David Kranze8e26312013-10-09 21:31:32 -040034def process_files(file_specs, url_specs, whitelists):
David Kranz002d6842014-02-20 17:53:02 -050035 regexp = re.compile(r"^.* (ERROR|CRITICAL|TRACE) .*\[.*\-.*\]")
David Kranze8e26312013-10-09 21:31:32 -040036 had_errors = False
37 for (name, filename) in file_specs:
38 whitelist = whitelists.get(name, [])
39 with open(filename) as content:
40 if scan_content(name, content, regexp, whitelist):
41 had_errors = True
42 for (name, url) in url_specs:
43 whitelist = whitelists.get(name, [])
44 req = urllib2.Request(url)
45 req.add_header('Accept-Encoding', 'gzip')
46 page = urllib2.urlopen(req)
47 buf = StringIO.StringIO(page.read())
48 f = gzip.GzipFile(fileobj=buf)
49 if scan_content(name, f.read().splitlines(), regexp, whitelist):
50 had_errors = True
51 return had_errors
52
53
54def scan_content(name, content, regexp, whitelist):
55 had_errors = False
David Kranze07cdb82013-11-27 10:53:54 -050056 print_log_name = True
David Kranze8e26312013-10-09 21:31:32 -040057 for line in content:
58 if not line.startswith("Stderr:") and regexp.match(line):
59 whitelisted = False
60 for w in whitelist:
61 pat = ".*%s.*%s.*" % (w['module'].replace('.', '\\.'),
62 w['message'])
63 if re.match(pat, line):
64 whitelisted = True
65 break
David Kranze07cdb82013-11-27 10:53:54 -050066 if not whitelisted or dump_all_errors:
David Kranz78dc5ab2013-11-29 12:33:02 -050067 if print_log_name:
David Kranze8e26312013-10-09 21:31:32 -040068 print("Log File: %s" % name)
David Kranze07cdb82013-11-27 10:53:54 -050069 print_log_name = False
70 if not whitelisted:
71 had_errors = True
David Kranz955a9e32013-12-30 12:04:17 -050072 print("*** Not Whitelisted ***"),
David Kranze8e26312013-10-09 21:31:32 -040073 print(line)
74 return had_errors
75
76
77def collect_url_logs(url):
78 page = urllib2.urlopen(url)
79 content = page.read()
80 logs = re.findall('(screen-[\w-]+\.txt\.gz)</a>', content)
81 return logs
82
83
84def main(opts):
85 if opts.directory and opts.url or not (opts.directory or opts.url):
86 print("Must provide exactly one of -d or -u")
87 exit(1)
88 print("Checking logs...")
89 WHITELIST_FILE = os.path.join(
90 os.path.abspath(os.path.dirname(os.path.dirname(__file__))),
91 "etc", "whitelist.yaml")
92
93 file_matcher = re.compile(r".*screen-([\w-]+)\.log")
94 files = []
95 if opts.directory:
96 d = opts.directory
97 for f in os.listdir(d):
98 files.append(os.path.join(d, f))
99 files_to_process = []
100 for f in files:
101 m = file_matcher.match(f)
102 if m:
103 files_to_process.append((m.group(1), f))
104
105 url_matcher = re.compile(r".*screen-([\w-]+)\.txt\.gz")
106 urls = []
107 if opts.url:
108 for logfile in collect_url_logs(opts.url):
109 urls.append("%s/%s" % (opts.url, logfile))
110 urls_to_process = []
111 for u in urls:
112 m = url_matcher.match(u)
113 if m:
114 urls_to_process.append((m.group(1), u))
115
116 whitelists = {}
117 with open(WHITELIST_FILE) as stream:
118 loaded = yaml.safe_load(stream)
119 if loaded:
120 for (name, l) in loaded.iteritems():
121 for w in l:
122 assert 'module' in w, 'no module in %s' % name
123 assert 'message' in w, 'no message in %s' % name
124 whitelists = loaded
125 if process_files(files_to_process, urls_to_process, whitelists):
126 print("Logs have errors")
David Kranze07cdb82013-11-27 10:53:54 -0500127 if is_neutron:
128 print("Currently not failing neutron builds with errors")
129 return 0
Sean Dague1159e522013-12-13 18:46:21 -0500130 if is_grenade:
131 print("Currently not failing grenade runs with errors")
132 return 0
David Kranzb705d462013-11-27 14:51:26 -0500133 print("FAILED")
134 return 1
David Kranze8e26312013-10-09 21:31:32 -0400135 else:
136 print("ok")
137 return 0
138
139usage = """
140Find non-white-listed log errors in log files from a devstack-gate run.
141Log files will be searched for ERROR or CRITICAL messages. If any
142error messages do not match any of the whitelist entries contained in
143etc/whitelist.yaml, those messages will be printed to the console and
144failure will be returned. A file directory containing logs or a url to the
145log files of an OpenStack gate job can be provided.
146
147The whitelist yaml looks like:
148
149log-name:
150 - module: "a.b.c"
151 message: "regexp"
152 - module: "a.b.c"
153 message: "regexp"
154
155repeated for each log file with a whitelist.
156"""
157
158parser = argparse.ArgumentParser(description=usage)
159parser.add_argument('-d', '--directory',
160 help="Directory containing log files")
161parser.add_argument('-u', '--url',
162 help="url containing logs from an OpenStack gate job")
David Kranz852c5c22013-10-04 15:10:15 -0400163
164if __name__ == "__main__":
David Kranze8e26312013-10-09 21:31:32 -0400165 try:
166 sys.exit(main(parser.parse_args()))
167 except Exception as e:
168 print("Failure in script: %s" % e)
169 # Don't fail if there is a problem with the script.
170 sys.exit(0)