blob: 0c8fd21a01660c8951fd5f5b8948de376494c891 [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 Kranze07cdb82013-11-27 10:53:54 -050032dump_all_errors = is_neutron
33
34
David Kranze8e26312013-10-09 21:31:32 -040035def process_files(file_specs, url_specs, whitelists):
36 regexp = re.compile(r"^.*(ERROR|CRITICAL).*\[.*\-.*\]")
37 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 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)