-
Notifications
You must be signed in to change notification settings - Fork 1k
Loganalyzer enhancements #321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
6b7a3d5
a182cfd
98b9e19
ba0ab21
4012844
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,6 +96,11 @@ def create_start_marker(self): | |
|
|
||
| #--------------------------------------------------------------------- | ||
|
|
||
| def is_filename_stdin(self, file_name): | ||
| return True if file_name == "-" else False | ||
|
|
||
| #--------------------------------------------------------------------- | ||
|
|
||
| def create_end_marker(self): | ||
| return self.end_marker_prefix + "-" + self.run_id | ||
| #--------------------------------------------------------------------- | ||
|
|
@@ -108,7 +113,7 @@ def place_marker(self, log_file_list, marker): | |
| ''' | ||
|
|
||
| for log_file in log_file_list: | ||
| if not len(log_file) : | ||
| if not len(log_file) or self.is_filename_stdin(log_file): | ||
| continue | ||
| self.print_diagnostic_message('log file:%s, place marker %s'%(log_file, marker)) | ||
| with open(log_file, 'a') as file: | ||
|
|
@@ -214,7 +219,7 @@ def create_msg_regex(self, file_lsit): | |
| regex = re.compile('|'.join(messages_regex)) | ||
| else: | ||
| regex = None | ||
| return regex | ||
| return regex, messages_regex | ||
| #--------------------------------------------------------------------- | ||
|
|
||
| def line_matches(self, str, match_messages_regex, ignore_messages_regex): | ||
|
|
@@ -288,62 +293,65 @@ def analyze_file(self, log_file_path, match_messages_regex, ignore_messages_rege | |
| #-- indicates whether log analyzer currently is in the log range between start | ||
| #-- and end marker. see analyze_file method. | ||
| in_analysis_range = False | ||
| stdin_as_input = True if self.is_filename_stdin(log_file_path) else False | ||
|
||
| matching_lines = [] | ||
| log_file = open(log_file_path, 'r') | ||
| expected_lines = [] | ||
| found_start_marker = False | ||
| found_end_marker = False | ||
|
|
||
| #-- True if expected message is found | ||
| found_expected_message = False | ||
| expecting_lines = [] | ||
| if stdin_as_input: | ||
| log_file = sys.stdin | ||
| else: | ||
| log_file = open(log_file_path, 'r') | ||
|
|
||
| start_marker = self.create_start_marker() | ||
| end_marker = self.create_end_marker() | ||
|
|
||
| for rev_line in reversed(log_file.readlines()): | ||
|
|
||
| if rev_line.find(end_marker) != -1: | ||
| self.print_diagnostic_message('found end marker: %s' % end_marker) | ||
| if (found_end_marker): | ||
| print 'ERROR: duplicate end marker found' | ||
| sys.exit(err_duplicate_end_marker) | ||
| found_end_marker = True | ||
| if stdin_as_input: | ||
| in_analysis_range = True | ||
| continue | ||
|
|
||
| if rev_line.find(start_marker) != -1: | ||
| self.print_diagnostic_message('found start marker: %s' % start_marker) | ||
| if (found_start_marker): | ||
| print 'ERROR: duplicate start marker found' | ||
| sys.exit(err_duplicate_start_marker) | ||
| found_start_marker = True | ||
|
|
||
| if(not in_analysis_range): | ||
| print 'ERROR: found start marker:%s without corresponding end marker' % rev_line | ||
| sys.exit(err_no_end_marker) | ||
| in_analysis_range = False | ||
| break | ||
| else: | ||
| if rev_line.find(end_marker) != -1: | ||
| self.print_diagnostic_message('found end marker: %s' % end_marker) | ||
| if (found_end_marker): | ||
| print 'ERROR: duplicate end marker found' | ||
| sys.exit(err_duplicate_end_marker) | ||
| found_end_marker = True | ||
| in_analysis_range = True | ||
| continue | ||
|
|
||
| if not stdin_as_input: | ||
| if rev_line.find(start_marker) != -1: | ||
| self.print_diagnostic_message('found start marker: %s' % start_marker) | ||
| if (found_start_marker): | ||
| print 'ERROR: duplicate start marker found' | ||
| sys.exit(err_duplicate_start_marker) | ||
| found_start_marker = True | ||
|
|
||
| if(not in_analysis_range): | ||
| print 'ERROR: found start marker:%s without corresponding end marker' % rev_line | ||
| sys.exit(err_no_end_marker) | ||
| in_analysis_range = False | ||
| break | ||
|
|
||
| if in_analysis_range : | ||
| if self.line_is_expected(rev_line, expect_messages_regex): | ||
| found_expected_message = True | ||
| expecting_lines.append(rev_line) | ||
| expected_lines.append(rev_line) | ||
|
|
||
| elif self.line_matches(rev_line, match_messages_regex, ignore_messages_regex): | ||
| matching_lines.append(rev_line) | ||
|
|
||
| if (not found_start_marker): | ||
| print 'ERROR: start marker was not found' | ||
| sys.exit(err_no_start_marker) | ||
|
|
||
| if (not found_end_marker): | ||
| print 'ERROR: end marker was not found' | ||
| sys.exit(err_no_end_marker) | ||
| # care about the markers only if input is not stdin | ||
| if not stdin_as_input: | ||
| if (not found_start_marker): | ||
| print 'ERROR: start marker was not found' | ||
| sys.exit(err_no_start_marker) | ||
|
|
||
| if (not found_expected_message) and (expect_messages_regex is not None): | ||
| print 'ERROR: expected error messages were not found in logfile' | ||
| if (not found_end_marker): | ||
| print 'ERROR: end marker was not found' | ||
| sys.exit(err_no_end_marker) | ||
|
|
||
| return matching_lines,expecting_lines | ||
| return matching_lines, expected_lines | ||
| #--------------------------------------------------------------------- | ||
|
|
||
| def analyze_file_list(self, log_file_list, match_messages_regex, ignore_messages_regex, expect_messages_regex): | ||
|
|
@@ -400,6 +408,10 @@ def usage(): | |
| print ' A string from log file matching any string from these' | ||
| print ' files will be ignored during analysis. Must be present' | ||
| print ' when action == analyze.' | ||
| print '--expect_files_in path{,path} List of path to files containing string. ' | ||
| print ' All the strings from these files will be expected to present' | ||
| print ' in one of specified log files during the analysis. Must be present' | ||
| print ' when action == analyze.' | ||
|
|
||
| #--------------------------------------------------------------------- | ||
|
|
||
|
|
@@ -451,7 +463,7 @@ def check_run_id(run_id): | |
| return ret_code | ||
| #--------------------------------------------------------------------- | ||
|
|
||
| def write_result_file(run_id, out_dir, analysis_result_per_file): | ||
| def write_result_file(run_id, out_dir, analysis_result_per_file, messages_regex_e, unused_regex_messages): | ||
| ''' | ||
| @summary: Write results of analysis into a file. | ||
|
|
||
|
|
@@ -465,35 +477,50 @@ def write_result_file(run_id, out_dir, analysis_result_per_file): | |
| ''' | ||
|
|
||
| match_cnt = 0 | ||
| expect_cnt = 0 | ||
| expected_cnt = 0 | ||
| expected_lines_total = [] | ||
|
|
||
| with open(out_dir + "/result.loganalysis." + run_id + ".log", 'w') as out_file: | ||
| for key, val in analysis_result_per_file.iteritems(): | ||
| matching_lines, expecting_lines = val | ||
| matching_lines, expected_lines = val | ||
|
|
||
| out_file.write("\n-----------Matches found in file:%s-----------\n" % key) | ||
| out_file.write("\n-----------Matches found in file:'%s'-----------\n" % key) | ||
| for s in matching_lines: | ||
| out_file.write(s) | ||
| out_file.flush() | ||
| out_file.write('\nMatches:%d\n' % len(matching_lines)) | ||
| match_cnt += len(matching_lines) | ||
|
|
||
| out_file.write("\n-------------------------------------------------\n\n") | ||
|
|
||
| for i in expecting_lines: | ||
| for i in expected_lines: | ||
| out_file.write(i) | ||
| out_file.flush() | ||
| out_file.write('\nExpecting matches:%d\n' % len(expecting_lines)) | ||
| expect_cnt += len(expecting_lines) | ||
| expected_lines_total.append(i) | ||
| out_file.write('\nExpected and found matches:%d\n' % len(expected_lines)) | ||
| expected_cnt += len(expected_lines) | ||
|
|
||
| out_file.write("\n-------------------------------------------------\n\n") | ||
| out_file.write('Total matches:%d\n' % match_cnt) | ||
| out_file.write('Total expected matches:%d\n' % expect_cnt) | ||
| # Find unused regex matches | ||
| for regex in messages_regex_e: | ||
| regex_used = False | ||
| for line in expected_lines_total: | ||
| if re.search(regex, line): | ||
| regex_used = True | ||
| break | ||
| if not regex_used: | ||
| unused_regex_messages.append(regex) | ||
|
|
||
| out_file.write('Total expected and found matches:%d\n' % expected_cnt) | ||
| out_file.write('Total expected but not found matches: %d\n\n' % len(unused_regex_messages)) | ||
| for regex in unused_regex_messages: | ||
| out_file.write(regex + "\n") | ||
|
|
||
| out_file.write("\n-------------------------------------------------\n\n") | ||
| out_file.flush() | ||
|
|
||
| #--------------------------------------------------------------------- | ||
|
|
||
| def write_summary_file(run_id, out_dir, analysis_result_per_file): | ||
| def write_summary_file(run_id, out_dir, analysis_result_per_file, unused_regex_messages): | ||
| ''' | ||
| @summary: This function writes results summary into a file | ||
|
|
||
|
|
@@ -521,10 +548,11 @@ def write_summary_file(run_id, out_dir, analysis_result_per_file): | |
| total_match_cnt += file_match_cnt | ||
| total_expect_cnt += file_expect_cnt | ||
|
|
||
| out_file.write("-----------------------------\n") | ||
| out_file.write("TOTAL MATCHES: %d\n" % total_match_cnt) | ||
| out_file.write("TOTAL EXPECTED MATCHES: %d\n" % total_expect_cnt) | ||
| out_file.write("-----------------------------\n") | ||
| out_file.write("-----------------------------------\n") | ||
| out_file.write("TOTAL MATCHES: %d\n" % total_match_cnt) | ||
| out_file.write("TOTAL EXPECTED MATCHES: %d\n" % total_expect_cnt) | ||
| out_file.write("TOTAL EXPECTED MISSING MATCHES: %d\n" % len(unused_regex_messages)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Do you need to change back the grep command which modified by Maggie, in this PR? #Closed
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, could your please change the loganalyzer_end.yml which process the different result for validation?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, done |
||
| out_file.write("-----------------------------------\n") | ||
| out_file.flush() | ||
| out_file.close() | ||
| #--------------------------------------------------------------------- | ||
|
|
@@ -583,7 +611,7 @@ def main(argv): | |
|
|
||
| analyzer = LogAnalyzer(run_id, verbose) | ||
|
|
||
| log_file_list = log_files_in.split(tokenizer) | ||
| log_file_list = filter(None, log_files_in.split(tokenizer)) | ||
|
|
||
| result = {} | ||
| if (action == "init"): | ||
|
|
@@ -596,15 +624,19 @@ def main(argv): | |
|
|
||
| analyzer.place_marker(log_file_list, analyzer.create_end_marker()) | ||
|
|
||
| match_messages_regex = analyzer.create_msg_regex(match_file_list) | ||
| ignore_messages_regex = analyzer.create_msg_regex(ignore_file_list) | ||
| expect_messages_regex = analyzer.create_msg_regex(expect_file_list) | ||
| match_messages_regex, messages_regex_m = analyzer.create_msg_regex(match_file_list) | ||
| ignore_messages_regex, messages_regex_i = analyzer.create_msg_regex(ignore_file_list) | ||
| expect_messages_regex, messages_regex_e = analyzer.create_msg_regex(expect_file_list) | ||
|
|
||
| # if no log file specified - add system log | ||
| if not log_file_list: | ||
| log_file_list.append(system_log_file) | ||
|
|
||
| log_file_list.append(system_log_file) | ||
| result = analyzer.analyze_file_list(log_file_list, match_messages_regex, | ||
| ignore_messages_regex, expect_messages_regex) | ||
| write_result_file(run_id, out_dir, result) | ||
| write_summary_file(run_id, out_dir, result) | ||
| unused_regex_messages = [] | ||
| write_result_file(run_id, out_dir, result, messages_regex_e, unused_regex_messages) | ||
| write_summary_file(run_id, out_dir, result, unused_regex_messages) | ||
|
|
||
| else: | ||
| print 'Unknown action:%s specified' % action | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,11 +17,11 @@ | |
| register: errors_found | ||
|
|
||
| - name: Check if loganalyzer missed expected messages | ||
| shell: grep "TOTAL EXPECTED MATCHES" "{{ test_out_dir }}/{{ summary_file }}" | sed -n "s/TOTAL EXPECTED MATCHES:[[:space:]]*//p" | ||
| register: expected_matches | ||
| shell: grep "TOTAL EXPECTED MISSING MATCHES" "{{ test_out_dir }}/{{ summary_file }}" | sed -n "s/TOTAL EXPECTED MISSING MATCHES:[[:space:]]*//p" | ||
| register: expected_missing_matches | ||
|
|
||
| - set_fact: | ||
| fail_in_logs: "{{ errors_found.stdout != \"0\" or expected_matches.stdout != \"0\" }}" | ||
| fail_in_logs: errors_found.stdout != "0" or expected_missing_matches.stdout != "0" | ||
|
||
|
|
||
| - name: Generate system dump | ||
| command: generate_dump | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return file_name == "-" #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done