blob: b5b1780c08a6242f636aaa0e1673899b99ee3d93 [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
Sean Dague1159e522013-12-13 18:46:21 -050028is_grenade = (os.environ.get('DEVSTACK_GATE_GRENADE', "0") == "1" or
29 os.environ.get('DEVSTACK_GATE_GRENADE_FORWARD', "0") == "1")
David Kranz955a9e32013-12-30 12:04:17 -050030dump_all_errors = True
David Kranze07cdb82013-11-27 10:53:54 -050031
David Kranz5274de42014-02-27 15:23:35 -050032# As logs are made clean, add to this set
Sean Dague5d407e22014-03-18 14:31:05 -040033allowed_dirty = set([
34 'c-api',
35 'ceilometer-acentral',
36 'ceilometer-acompute',
37 'ceilometer-alarm-evaluator',
38 'ceilometer-anotification',
39 'ceilometer-api',
Sean Daguee2cda412014-03-26 15:39:05 -040040 'ceilometer-collector',
Sean Dague5d407e22014-03-18 14:31:05 -040041 'c-vol',
42 'g-api',
43 'h-api',
44 'h-eng',
45 'ir-cond',
46 'n-api',
47 'n-cpu',
48 'n-net',
49 'n-sch',
50 'q-agt',
51 'q-dhcp',
52 'q-lbaas',
53 'q-meta',
54 'q-metering',
55 'q-svc',
56 'q-vpn',
57 's-proxy'])
David Kranz5274de42014-02-27 15:23:35 -050058
David Kranze07cdb82013-11-27 10:53:54 -050059
David Kranze8e26312013-10-09 21:31:32 -040060def process_files(file_specs, url_specs, whitelists):
David Kranz002d6842014-02-20 17:53:02 -050061 regexp = re.compile(r"^.* (ERROR|CRITICAL|TRACE) .*\[.*\-.*\]")
David Kranz5274de42014-02-27 15:23:35 -050062 logs_with_errors = []
David Kranze8e26312013-10-09 21:31:32 -040063 for (name, filename) in file_specs:
64 whitelist = whitelists.get(name, [])
65 with open(filename) as content:
66 if scan_content(name, content, regexp, whitelist):
David Kranz5274de42014-02-27 15:23:35 -050067 logs_with_errors.append(name)
David Kranze8e26312013-10-09 21:31:32 -040068 for (name, url) in url_specs:
69 whitelist = whitelists.get(name, [])
70 req = urllib2.Request(url)
71 req.add_header('Accept-Encoding', 'gzip')
72 page = urllib2.urlopen(req)
73 buf = StringIO.StringIO(page.read())
74 f = gzip.GzipFile(fileobj=buf)
75 if scan_content(name, f.read().splitlines(), regexp, whitelist):
David Kranz5274de42014-02-27 15:23:35 -050076 logs_with_errors.append(name)
77 return logs_with_errors
David Kranze8e26312013-10-09 21:31:32 -040078
79
80def scan_content(name, content, regexp, whitelist):
81 had_errors = False
David Kranze07cdb82013-11-27 10:53:54 -050082 print_log_name = True
David Kranze8e26312013-10-09 21:31:32 -040083 for line in content:
84 if not line.startswith("Stderr:") and regexp.match(line):
85 whitelisted = False
86 for w in whitelist:
87 pat = ".*%s.*%s.*" % (w['module'].replace('.', '\\.'),
88 w['message'])
89 if re.match(pat, line):
90 whitelisted = True
91 break
David Kranze07cdb82013-11-27 10:53:54 -050092 if not whitelisted or dump_all_errors:
David Kranz78dc5ab2013-11-29 12:33:02 -050093 if print_log_name:
Sean Daguee2cda412014-03-26 15:39:05 -040094 print("\nLog File Has Errors: %s" % name)
David Kranze07cdb82013-11-27 10:53:54 -050095 print_log_name = False
96 if not whitelisted:
97 had_errors = True
David Kranz955a9e32013-12-30 12:04:17 -050098 print("*** Not Whitelisted ***"),
Sean Dague5d407e22014-03-18 14:31:05 -040099 print(line.rstrip())
David Kranze8e26312013-10-09 21:31:32 -0400100 return had_errors
101
102
103def collect_url_logs(url):
104 page = urllib2.urlopen(url)
105 content = page.read()
106 logs = re.findall('(screen-[\w-]+\.txt\.gz)</a>', content)
107 return logs
108
109
110def main(opts):
111 if opts.directory and opts.url or not (opts.directory or opts.url):
112 print("Must provide exactly one of -d or -u")
113 exit(1)
114 print("Checking logs...")
115 WHITELIST_FILE = os.path.join(
116 os.path.abspath(os.path.dirname(os.path.dirname(__file__))),
117 "etc", "whitelist.yaml")
118
119 file_matcher = re.compile(r".*screen-([\w-]+)\.log")
120 files = []
121 if opts.directory:
122 d = opts.directory
123 for f in os.listdir(d):
124 files.append(os.path.join(d, f))
125 files_to_process = []
126 for f in files:
127 m = file_matcher.match(f)
128 if m:
129 files_to_process.append((m.group(1), f))
130
131 url_matcher = re.compile(r".*screen-([\w-]+)\.txt\.gz")
132 urls = []
133 if opts.url:
134 for logfile in collect_url_logs(opts.url):
135 urls.append("%s/%s" % (opts.url, logfile))
136 urls_to_process = []
137 for u in urls:
138 m = url_matcher.match(u)
139 if m:
140 urls_to_process.append((m.group(1), u))
141
142 whitelists = {}
143 with open(WHITELIST_FILE) as stream:
144 loaded = yaml.safe_load(stream)
145 if loaded:
146 for (name, l) in loaded.iteritems():
147 for w in l:
148 assert 'module' in w, 'no module in %s' % name
149 assert 'message' in w, 'no message in %s' % name
150 whitelists = loaded
David Kranz5274de42014-02-27 15:23:35 -0500151 logs_with_errors = process_files(files_to_process, urls_to_process,
152 whitelists)
153 if logs_with_errors:
David Kranze8e26312013-10-09 21:31:32 -0400154 print("Logs have errors")
David Kranz5274de42014-02-27 15:23:35 -0500155 if is_grenade:
156 print("Currently not failing grenade runs with errors")
David Kranze8e26312013-10-09 21:31:32 -0400157 return 0
David Kranz5274de42014-02-27 15:23:35 -0500158 failed = False
159 for log in logs_with_errors:
Sean Dague5d407e22014-03-18 14:31:05 -0400160 if log not in allowed_dirty:
161 print("Log: %s not allowed to have ERRORS or TRACES" % log)
David Kranz5274de42014-02-27 15:23:35 -0500162 failed = True
163 if failed:
164 return 1
165 print("ok")
166 return 0
David Kranze8e26312013-10-09 21:31:32 -0400167
168usage = """
169Find non-white-listed log errors in log files from a devstack-gate run.
170Log files will be searched for ERROR or CRITICAL messages. If any
171error messages do not match any of the whitelist entries contained in
172etc/whitelist.yaml, those messages will be printed to the console and
173failure will be returned. A file directory containing logs or a url to the
174log files of an OpenStack gate job can be provided.
175
176The whitelist yaml looks like:
177
178log-name:
179 - module: "a.b.c"
180 message: "regexp"
181 - module: "a.b.c"
182 message: "regexp"
183
184repeated for each log file with a whitelist.
185"""
186
187parser = argparse.ArgumentParser(description=usage)
188parser.add_argument('-d', '--directory',
189 help="Directory containing log files")
190parser.add_argument('-u', '--url',
191 help="url containing logs from an OpenStack gate job")
David Kranz852c5c22013-10-04 15:10:15 -0400192
193if __name__ == "__main__":
David Kranze8e26312013-10-09 21:31:32 -0400194 try:
195 sys.exit(main(parser.parse_args()))
196 except Exception as e:
197 print("Failure in script: %s" % e)
198 # Don't fail if there is a problem with the script.
199 sys.exit(0)