blob: c167a9bf20686109707c0ab609ff8c5617467d8b [file] [log] [blame]
David Kranz852c5c22013-10-04 15:10:15 -04001#!/usr/bin/env python
2# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
4# Copyright 2013 Red Hat, Inc.
5# All Rights Reserved.
6#
7# Licensed under the Apache License, Version 2.0 (the "License"); you may
8# not use this file except in compliance with the License. You may obtain
9# a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16# License for the specific language governing permissions and limitations
17# under the License.
18
David Kranze8e26312013-10-09 21:31:32 -040019import argparse
20import gzip
21import os
22import re
23import StringIO
David Kranz852c5c22013-10-04 15:10:15 -040024import sys
David Kranze8e26312013-10-09 21:31:32 -040025import urllib2
26import yaml
27
28
David Kranze07cdb82013-11-27 10:53:54 -050029is_neutron = os.environ.get('DEVSTACK_GATE_NEUTRON', "0") == "1"
Sean Dague1159e522013-12-13 18:46:21 -050030is_grenade = (os.environ.get('DEVSTACK_GATE_GRENADE', "0") == "1" or
31 os.environ.get('DEVSTACK_GATE_GRENADE_FORWARD', "0") == "1")
David Kranz955a9e32013-12-30 12:04:17 -050032dump_all_errors = True
David Kranze07cdb82013-11-27 10:53:54 -050033
34
David Kranze8e26312013-10-09 21:31:32 -040035def process_files(file_specs, url_specs, whitelists):
David Kranz0e9ac352013-12-11 13:59:05 -050036 regexp = re.compile(r"^.* (ERROR|CRITICAL) .*\[.*\-.*\]")
David Kranze8e26312013-10-09 21:31:32 -040037 had_errors = False
38 for (name, filename) in file_specs:
39 whitelist = whitelists.get(name, [])
40 with open(filename) as content:
41 if scan_content(name, content, regexp, whitelist):
42 had_errors = True
43 for (name, url) in url_specs:
44 whitelist = whitelists.get(name, [])
45 req = urllib2.Request(url)
46 req.add_header('Accept-Encoding', 'gzip')
47 page = urllib2.urlopen(req)
48 buf = StringIO.StringIO(page.read())
49 f = gzip.GzipFile(fileobj=buf)
50 if scan_content(name, f.read().splitlines(), regexp, whitelist):
51 had_errors = True
52 return had_errors
53
54
55def scan_content(name, content, regexp, whitelist):
56 had_errors = False
David Kranze07cdb82013-11-27 10:53:54 -050057 print_log_name = True
David Kranze8e26312013-10-09 21:31:32 -040058 for line in content:
59 if not line.startswith("Stderr:") and regexp.match(line):
60 whitelisted = False
61 for w in whitelist:
62 pat = ".*%s.*%s.*" % (w['module'].replace('.', '\\.'),
63 w['message'])
64 if re.match(pat, line):
65 whitelisted = True
66 break
David Kranze07cdb82013-11-27 10:53:54 -050067 if not whitelisted or dump_all_errors:
David Kranz78dc5ab2013-11-29 12:33:02 -050068 if print_log_name:
David Kranze8e26312013-10-09 21:31:32 -040069 print("Log File: %s" % name)
David Kranze07cdb82013-11-27 10:53:54 -050070 print_log_name = False
71 if not whitelisted:
72 had_errors = True
David Kranz955a9e32013-12-30 12:04:17 -050073 print("*** Not Whitelisted ***"),
David Kranze8e26312013-10-09 21:31:32 -040074 print(line)
75 return had_errors
76
77
78def collect_url_logs(url):
79 page = urllib2.urlopen(url)
80 content = page.read()
81 logs = re.findall('(screen-[\w-]+\.txt\.gz)</a>', content)
82 return logs
83
84
85def main(opts):
86 if opts.directory and opts.url or not (opts.directory or opts.url):
87 print("Must provide exactly one of -d or -u")
88 exit(1)
89 print("Checking logs...")
90 WHITELIST_FILE = os.path.join(
91 os.path.abspath(os.path.dirname(os.path.dirname(__file__))),
92 "etc", "whitelist.yaml")
93
94 file_matcher = re.compile(r".*screen-([\w-]+)\.log")
95 files = []
96 if opts.directory:
97 d = opts.directory
98 for f in os.listdir(d):
99 files.append(os.path.join(d, f))
100 files_to_process = []
101 for f in files:
102 m = file_matcher.match(f)
103 if m:
104 files_to_process.append((m.group(1), f))
105
106 url_matcher = re.compile(r".*screen-([\w-]+)\.txt\.gz")
107 urls = []
108 if opts.url:
109 for logfile in collect_url_logs(opts.url):
110 urls.append("%s/%s" % (opts.url, logfile))
111 urls_to_process = []
112 for u in urls:
113 m = url_matcher.match(u)
114 if m:
115 urls_to_process.append((m.group(1), u))
116
117 whitelists = {}
118 with open(WHITELIST_FILE) as stream:
119 loaded = yaml.safe_load(stream)
120 if loaded:
121 for (name, l) in loaded.iteritems():
122 for w in l:
123 assert 'module' in w, 'no module in %s' % name
124 assert 'message' in w, 'no message in %s' % name
125 whitelists = loaded
126 if process_files(files_to_process, urls_to_process, whitelists):
127 print("Logs have errors")
David Kranze07cdb82013-11-27 10:53:54 -0500128 if is_neutron:
129 print("Currently not failing neutron builds with errors")
130 return 0
Sean Dague1159e522013-12-13 18:46:21 -0500131 if is_grenade:
132 print("Currently not failing grenade runs with errors")
133 return 0
David Kranzb705d462013-11-27 14:51:26 -0500134 print("FAILED")
135 return 1
David Kranze8e26312013-10-09 21:31:32 -0400136 else:
137 print("ok")
138 return 0
139
140usage = """
141Find non-white-listed log errors in log files from a devstack-gate run.
142Log files will be searched for ERROR or CRITICAL messages. If any
143error messages do not match any of the whitelist entries contained in
144etc/whitelist.yaml, those messages will be printed to the console and
145failure will be returned. A file directory containing logs or a url to the
146log files of an OpenStack gate job can be provided.
147
148The whitelist yaml looks like:
149
150log-name:
151 - module: "a.b.c"
152 message: "regexp"
153 - module: "a.b.c"
154 message: "regexp"
155
156repeated for each log file with a whitelist.
157"""
158
159parser = argparse.ArgumentParser(description=usage)
160parser.add_argument('-d', '--directory',
161 help="Directory containing log files")
162parser.add_argument('-u', '--url',
163 help="url containing logs from an OpenStack gate job")
David Kranz852c5c22013-10-04 15:10:15 -0400164
165if __name__ == "__main__":
David Kranze8e26312013-10-09 21:31:32 -0400166 try:
167 sys.exit(main(parser.parse_args()))
168 except Exception as e:
169 print("Failure in script: %s" % e)
170 # Don't fail if there is a problem with the script.
171 sys.exit(0)