blob: edf95a12dd0abdb27377f486214b8fed078efc7b [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
33must_be_clean = set(['c-sch', 'g-reg', 'ceilometer-alarm-notifier',
34 'ceilometer-collector', 'horizon', 'n-crt', 'n-obj',
35 'q-vpn'])
36
David Kranze07cdb82013-11-27 10:53:54 -050037
David Kranze8e26312013-10-09 21:31:32 -040038def process_files(file_specs, url_specs, whitelists):
David Kranz002d6842014-02-20 17:53:02 -050039 regexp = re.compile(r"^.* (ERROR|CRITICAL|TRACE) .*\[.*\-.*\]")
David Kranz5274de42014-02-27 15:23:35 -050040 logs_with_errors = []
David Kranze8e26312013-10-09 21:31:32 -040041 for (name, filename) in file_specs:
42 whitelist = whitelists.get(name, [])
43 with open(filename) as content:
44 if scan_content(name, content, regexp, whitelist):
David Kranz5274de42014-02-27 15:23:35 -050045 logs_with_errors.append(name)
David Kranze8e26312013-10-09 21:31:32 -040046 for (name, url) in url_specs:
47 whitelist = whitelists.get(name, [])
48 req = urllib2.Request(url)
49 req.add_header('Accept-Encoding', 'gzip')
50 page = urllib2.urlopen(req)
51 buf = StringIO.StringIO(page.read())
52 f = gzip.GzipFile(fileobj=buf)
53 if scan_content(name, f.read().splitlines(), regexp, whitelist):
David Kranz5274de42014-02-27 15:23:35 -050054 logs_with_errors.append(name)
55 return logs_with_errors
David Kranze8e26312013-10-09 21:31:32 -040056
57
58def scan_content(name, content, regexp, whitelist):
59 had_errors = False
David Kranze07cdb82013-11-27 10:53:54 -050060 print_log_name = True
David Kranze8e26312013-10-09 21:31:32 -040061 for line in content:
62 if not line.startswith("Stderr:") and regexp.match(line):
63 whitelisted = False
64 for w in whitelist:
65 pat = ".*%s.*%s.*" % (w['module'].replace('.', '\\.'),
66 w['message'])
67 if re.match(pat, line):
68 whitelisted = True
69 break
David Kranze07cdb82013-11-27 10:53:54 -050070 if not whitelisted or dump_all_errors:
David Kranz78dc5ab2013-11-29 12:33:02 -050071 if print_log_name:
David Kranze8e26312013-10-09 21:31:32 -040072 print("Log File: %s" % name)
David Kranze07cdb82013-11-27 10:53:54 -050073 print_log_name = False
74 if not whitelisted:
75 had_errors = True
David Kranz955a9e32013-12-30 12:04:17 -050076 print("*** Not Whitelisted ***"),
David Kranze8e26312013-10-09 21:31:32 -040077 print(line)
78 return had_errors
79
80
81def collect_url_logs(url):
82 page = urllib2.urlopen(url)
83 content = page.read()
84 logs = re.findall('(screen-[\w-]+\.txt\.gz)</a>', content)
85 return logs
86
87
88def main(opts):
89 if opts.directory and opts.url or not (opts.directory or opts.url):
90 print("Must provide exactly one of -d or -u")
91 exit(1)
92 print("Checking logs...")
93 WHITELIST_FILE = os.path.join(
94 os.path.abspath(os.path.dirname(os.path.dirname(__file__))),
95 "etc", "whitelist.yaml")
96
97 file_matcher = re.compile(r".*screen-([\w-]+)\.log")
98 files = []
99 if opts.directory:
100 d = opts.directory
101 for f in os.listdir(d):
102 files.append(os.path.join(d, f))
103 files_to_process = []
104 for f in files:
105 m = file_matcher.match(f)
106 if m:
107 files_to_process.append((m.group(1), f))
108
109 url_matcher = re.compile(r".*screen-([\w-]+)\.txt\.gz")
110 urls = []
111 if opts.url:
112 for logfile in collect_url_logs(opts.url):
113 urls.append("%s/%s" % (opts.url, logfile))
114 urls_to_process = []
115 for u in urls:
116 m = url_matcher.match(u)
117 if m:
118 urls_to_process.append((m.group(1), u))
119
120 whitelists = {}
121 with open(WHITELIST_FILE) as stream:
122 loaded = yaml.safe_load(stream)
123 if loaded:
124 for (name, l) in loaded.iteritems():
125 for w in l:
126 assert 'module' in w, 'no module in %s' % name
127 assert 'message' in w, 'no message in %s' % name
128 whitelists = loaded
David Kranz5274de42014-02-27 15:23:35 -0500129 logs_with_errors = process_files(files_to_process, urls_to_process,
130 whitelists)
131 if logs_with_errors:
David Kranze8e26312013-10-09 21:31:32 -0400132 print("Logs have errors")
David Kranz5274de42014-02-27 15:23:35 -0500133 if is_grenade:
134 print("Currently not failing grenade runs with errors")
David Kranze8e26312013-10-09 21:31:32 -0400135 return 0
David Kranz5274de42014-02-27 15:23:35 -0500136 failed = False
137 for log in logs_with_errors:
138 if log in must_be_clean:
139 print("FAILED: %s" % log)
140 failed = True
141 if failed:
142 return 1
143 print("ok")
144 return 0
David Kranze8e26312013-10-09 21:31:32 -0400145
146usage = """
147Find non-white-listed log errors in log files from a devstack-gate run.
148Log files will be searched for ERROR or CRITICAL messages. If any
149error messages do not match any of the whitelist entries contained in
150etc/whitelist.yaml, those messages will be printed to the console and
151failure will be returned. A file directory containing logs or a url to the
152log files of an OpenStack gate job can be provided.
153
154The whitelist yaml looks like:
155
156log-name:
157 - module: "a.b.c"
158 message: "regexp"
159 - module: "a.b.c"
160 message: "regexp"
161
162repeated for each log file with a whitelist.
163"""
164
165parser = argparse.ArgumentParser(description=usage)
166parser.add_argument('-d', '--directory',
167 help="Directory containing log files")
168parser.add_argument('-u', '--url',
169 help="url containing logs from an OpenStack gate job")
David Kranz852c5c22013-10-04 15:10:15 -0400170
171if __name__ == "__main__":
David Kranze8e26312013-10-09 21:31:32 -0400172 try:
173 sys.exit(main(parser.parse_args()))
174 except Exception as e:
175 print("Failure in script: %s" % e)
176 # Don't fail if there is a problem with the script.
177 sys.exit(0)