def _process_query(self, input, parser): self._run_stats['linesRead'] += 1 line_time = get_line_time(input) if line_time is not None: if ((self._run_stats['timeRange']['start'] is None) or (self._run_stats['timeRange']['start'] > line_time)): self._run_stats['timeRange']['start'] = line_time if ((self._run_stats['timeRange']['end'] is None) or (self._run_stats['timeRange']['end'] < line_time)): self._run_stats['timeRange']['end'] = line_time parsed = parser.parse(input) if parsed is not None: if parsed['supported']: self._run_stats['linesAnalyzed'] += 1 namespace_tuple = self._tuplefy_namespace(parsed['ns']) # If the query is for a requested namespace .... if self._namespace_requested(parsed['ns']): db_name = namespace_tuple[0] collection_name = namespace_tuple[1] query_report = None if parsed['stats']['millis'] >= self._slowms: try: query_report = self.generate_query_report( self._db_uri, parsed, db_name, collection_name) except Exception as e: #print traceback.print_exc() return 1 if query_report is not None: if query_report['recommendation'] is not None: self._run_stats['linesWithRecommendations'] += 1 self._report.add_query_occurrence(query_report) else: self._run_stats['unparsableLineInfo']['unparsableLines'] += 1 self._run_stats['unparsableLineInfo'][ 'unparsableLinesWithTime'] += 1 self._run_stats['unparsableLineInfo'][ 'unparsedTimeMillis'] += int(parsed['stats']['millis']) self._run_stats['unparsableLineInfo'][ 'unparsedAvgTimeMillis'] = self._run_stats[ 'unparsableLineInfo'][ 'unparsedTimeMillis'] / self._run_stats[ 'unparsableLineInfo']['unparsableLinesWithTime'] else: self._run_stats['unparsableLineInfo']['unparsableLines'] += 1 self._run_stats['unparsableLineInfo'][ 'unparsableLinesWithoutTime'] += 1
def watch_logfile(self, logfile_path): """Analyzes queries from the tail of a given log file""" self._run_stats['logSource'] = logfile_path log_parser = LogParser() # For each new line in the logfile ... output_time = time.time() + WATCH_DISPLAY_REFRESH_SECONDS try: firstLine = True for line in self._tail_file(open(logfile_path), WATCH_INTERVAL_SECONDS): if firstLine: self._run_stats['timeRange']['start'] = get_line_time(line) self._process_query(line, log_parser) self._run_stats['timeRange']['end'] = get_line_time(line) if time.time() >= output_time: self._output_aggregated_report(sys.stderr) output_time = time.time() + WATCH_DISPLAY_REFRESH_SECONDS except KeyboardInterrupt: sys.stderr.write("Interrupt received\n") finally: self._output_aggregated_report(sys.stdout) return 0
def _process_query(self, input, parser): self._run_stats['linesRead'] += 1 line_time = get_line_time(input) if line_time is not None: if ((self._run_stats['timeRange']['start'] is None) or (self._run_stats['timeRange']['start'] > line_time)): self._run_stats['timeRange']['start'] = line_time if ((self._run_stats['timeRange']['end'] is None) or (self._run_stats['timeRange']['end'] < line_time)): self._run_stats['timeRange']['end'] = line_time parsed = parser.parse(input) if parsed is not None: if parsed['supported']: self._run_stats['linesAnalyzed'] += 1 namespace_tuple = self._tuplefy_namespace(parsed['ns']) # If the query is for a requested namespace .... if self._namespace_requested(parsed['ns']): db_name = namespace_tuple[0] collection_name = namespace_tuple[1] query_report = None if parsed['stats']['millis'] >= self._slowms: try: query_report = self.generate_query_report(self._db_uri, parsed, db_name, collection_name) except Exception as e: #print traceback.print_exc() return 1 if query_report is not None: if query_report['recommendation'] is not None: self._run_stats['linesWithRecommendations'] += 1 self._report.add_query_occurrence(query_report) else: self._run_stats['unparsableLineInfo']['unparsableLines'] += 1 self._run_stats['unparsableLineInfo']['unparsableLinesWithTime'] += 1 self._run_stats['unparsableLineInfo']['unparsedTimeMillis'] += int(parsed['stats']['millis']) self._run_stats['unparsableLineInfo']['unparsedAvgTimeMillis'] = self._run_stats['unparsableLineInfo']['unparsedTimeMillis'] / self._run_stats['unparsableLineInfo']['unparsableLinesWithTime'] else: self._run_stats['unparsableLineInfo']['unparsableLines'] += 1 self._run_stats['unparsableLineInfo']['unparsableLinesWithoutTime'] += 1