blob: f3204e3e333e9e4911df82d5d801769b77704dc3 [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 Kranze07cdb82013-11-27 10:53:54 -050031dump_all_errors = is_neutron
32
33
David Kranze8e26312013-10-09 21:31:32 -040034def process_files(file_specs, url_specs, whitelists):
David Kranz0e9ac352013-12-11 13:59:05 -050035 regexp = re.compile(r"^.* (ERROR|CRITICAL) .*\[.*\-.*\]")
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 Kranze8e26312013-10-09 21:31:32 -040072 print(line)
73 return had_errors
74
75
76def collect_url_logs(url):
77 page = urllib2.urlopen(url)
78 content = page.read()
79 logs = re.findall('(screen-[\w-]+\.txt\.gz)</a>', content)
80 return logs
81
82
83def main(opts):
84 if opts.directory and opts.url or not (opts.directory or opts.url):
85 print("Must provide exactly one of -d or -u")
86 exit(1)
87 print("Checking logs...")
88 WHITELIST_FILE = os.path.join(
89 os.path.abspath(os.path.dirname(os.path.dirname(__file__))),
90 "etc", "whitelist.yaml")
91
92 file_matcher = re.compile(r".*screen-([\w-]+)\.log")
93 files = []
94 if opts.directory:
95 d = opts.directory
96 for f in os.listdir(d):
97 files.append(os.path.join(d, f))
98 files_to_process = []
99 for f in files:
100 m = file_matcher.match(f)
101 if m:
102 files_to_process.append((m.group(1), f))
103
104 url_matcher = re.compile(r".*screen-([\w-]+)\.txt\.gz")
105 urls = []
106 if opts.url:
107 for logfile in collect_url_logs(opts.url):
108 urls.append("%s/%s" % (opts.url, logfile))
109 urls_to_process = []
110 for u in urls:
111 m = url_matcher.match(u)
112 if m:
113 urls_to_process.append((m.group(1), u))
114
115 whitelists = {}
116 with open(WHITELIST_FILE) as stream:
117 loaded = yaml.safe_load(stream)
118 if loaded:
119 for (name, l) in loaded.iteritems():
120 for w in l:
121 assert 'module' in w, 'no module in %s' % name
122 assert 'message' in w, 'no message in %s' % name
123 whitelists = loaded
124 if process_files(files_to_process, urls_to_process, whitelists):
125 print("Logs have errors")
David Kranze07cdb82013-11-27 10:53:54 -0500126 if is_neutron:
127 print("Currently not failing neutron builds with errors")
128 return 0
Sean Dague1159e522013-12-13 18:46:21 -0500129 if is_grenade:
130 print("Currently not failing grenade runs with errors")
131 return 0
David Kranzb705d462013-11-27 14:51:26 -0500132 print("FAILED")
133 return 1
David Kranze8e26312013-10-09 21:31:32 -0400134 else:
135 print("ok")
136 return 0
137
138usage = """
139Find non-white-listed log errors in log files from a devstack-gate run.
140Log files will be searched for ERROR or CRITICAL messages. If any
141error messages do not match any of the whitelist entries contained in
142etc/whitelist.yaml, those messages will be printed to the console and
143failure will be returned. A file directory containing logs or a url to the
144log files of an OpenStack gate job can be provided.
145
146The whitelist yaml looks like:
147
148log-name:
149 - module: "a.b.c"
150 message: "regexp"
151 - module: "a.b.c"
152 message: "regexp"
153
154repeated for each log file with a whitelist.
155"""
156
157parser = argparse.ArgumentParser(description=usage)
158parser.add_argument('-d', '--directory',
159 help="Directory containing log files")
160parser.add_argument('-u', '--url',
161 help="url containing logs from an OpenStack gate job")
David Kranz852c5c22013-10-04 15:10:15 -0400162
163if __name__ == "__main__":
David Kranze8e26312013-10-09 21:31:32 -0400164 try:
165 sys.exit(main(parser.parse_args()))
166 except Exception as e:
167 print("Failure in script: %s" % e)
168 # Don't fail if there is a problem with the script.
169 sys.exit(0)