]> granicus.if.org Git - pgbadger/commitdiff
- Fix broken report of date-time introduced in previous patch.
authorGilles Darold <gilles.darold@dalibo.com>
Tue, 6 Dec 2016 20:13:44 +0000 (21:13 +0100)
committerGilles Darold <gilles.darold@dalibo.com>
Tue, 6 Dec 2016 20:13:44 +0000 (21:13 +0100)
- Add --normalized-only option to generate a text report of all
  normalized queries found in a log with count.

pgbadger

index 2e5c83aa352564d209b75cb3ecb7d84bc70a1de6..2b96cf61ae655237643a612090d2920712267038 100644 (file)
--- a/pgbadger
+++ b/pgbadger
@@ -277,6 +277,7 @@ my $pgbouncer_only          = 0;
 my $rebuild                 = 0;
 my $week_start_monday       = 0;
 my $use_sessionid_as_pid    = 0;
+my $dump_normalized_only    = 0;
 
 my $NUMPROGRESS = 10000;
 my @DIMENSIONS  = (800, 300);
@@ -435,6 +436,7 @@ my $result = GetOptions(
        'rebuild!'                 => \$rebuild,
        'pgbouncer-only!'          => \$pgbouncer_only,
        'start-monday!'            => \$week_start_monday,
+       'normalized-only!'         => \$dump_normalized_only,
 );
 die "FATAL: use pgbadger --help\n" if (not $result);
 
@@ -683,6 +685,14 @@ if (!$extension) {
        }
 }
 
+# Force text output with normalized query list only
+# and disable incremental report
+if ($dump_normalized_only) {
+       $extension = 'txt';
+       $incremental = 0;
+       $report_title = 'Normalized query report' if (!$report_title);
+}
+
 # Set default filename of the output file
 $outfile ||= 'out.' . $extension;
 &logmsg('DEBUG', "Output '$extension' reports will be written to $outfile");
@@ -1781,6 +1791,7 @@ Options:
     --pgbouncer-only       : only show pgbouncer related menu in the header.
     --start-monday         : in incremental mode, calendar's weeks start on
                              sunday. Use this otpion to start on monday.
+    --normalized-only      : only dump all normalized query to out.txt
 
 pgBadger is able to parse a remote log file using a passwordless ssh connection.
 Use the -r or --remote-host to set the host ip address or hostname. There's also
@@ -3737,8 +3748,21 @@ Generated on $curdate
 Log file: $logfile_str
 Parsed $fmt_nlines log entries in $total_time
 Log start from $overall_stat{'first_log_ts'} to $overall_stat{'last_log_ts'}
+
 };
 
+       # Dump normalized queries only if requested
+       if ($dump_normalized_only) {
+               print $fh "Count\t\tQuery\n";
+               print $fh '-'x70,"\n";
+               foreach my $q (sort { $normalyzed_info{$b}{count} <=> $normalyzed_info{$a}{count} } keys %normalyzed_info) {
+                       print $fh "$normalyzed_info{$q}{count}  $q\n";
+               }
+               print $fh "\n\n";
+               print $fh "Report generated by pgBadger $VERSION ($project_url).\n";
+               return;
+       }
+
        # Overall statistics
        my $fmt_unique  = &comma_numbers(scalar keys %normalyzed_info);
        my $fmt_queries = &comma_numbers($overall_stat{'queries_number'});
@@ -12897,9 +12921,9 @@ sub set_current_infos
        $cur_info{$t_pid}{year}      = $prefix_vars{'t_year'} if (!$cur_info{$t_pid}{year});
        $cur_info{$t_pid}{month}     = $prefix_vars{'t_month'} if (!$cur_info{$t_pid}{month});
        $cur_info{$t_pid}{day}       = $prefix_vars{'t_day'} if (!$cur_info{$t_pid}{day});
-       $cur_info{$t_pid}{hour}      = $prefix_vars{'t_hour'} if (($cur_info{$t_pid}{hour} eq ''));
-       $cur_info{$t_pid}{min}       = $prefix_vars{'t_min'} if (!$cur_info{$t_pid}{min} eq '');
-       $cur_info{$t_pid}{sec}       = $prefix_vars{'t_sec'} if (!$cur_info{$t_pid}{sec} eq '');
+       $cur_info{$t_pid}{hour}      = $prefix_vars{'t_hour'} if (!exists $cur_info{$t_pid}{sec} || ($cur_info{$t_pid}{hour} eq ''));
+       $cur_info{$t_pid}{min}       = $prefix_vars{'t_min'} if (!exists $cur_info{$t_pid}{sec} || ($cur_info{$t_pid}{min} eq ''));
+       $cur_info{$t_pid}{sec}       = $prefix_vars{'t_sec'} if (!exists $cur_info{$t_pid}{sec} || ($cur_info{$t_pid}{sec} eq ''));
        $cur_info{$t_pid}{timestamp} = $prefix_vars{'t_timestamp'} if (!$cur_info{$t_pid}{timestamp});
        $cur_info{$t_pid}{ident}     = $prefix_vars{'t_ident'} if (!$cur_info{$t_pid}{ident});
        $cur_info{$t_pid}{query}     = $prefix_vars{'t_query'} if (!$cur_info{$t_pid}{query});
@@ -13002,10 +13026,26 @@ sub store_queries
        return 1 if (!$cur_info{$t_pid}{year});
 
 
-       # Cleanup and normalize the current query
+       # Cleanup and pre-normalize the current query
        $cur_info{$t_pid}{query} =~ s/^\s+//s;
        $cur_info{$t_pid}{query} =~ s/[\s;]+$//s;
 
+
+       # Just store normalized query when --normalized-only is used
+       if ($dump_normalized_only && $cur_info{$t_pid}{query}) {
+
+               # Add a semi-colon at end of the query
+               $cur_info{$t_pid}{query} .= ';' if ($cur_info{$t_pid}{query} !~ /;\s*$/s);
+
+               # Normalize query
+               my $normalized = &normalize_query($cur_info{$t_pid}{query});
+
+               # Store normalized query count
+               $normalyzed_info{$normalized}{count}++;
+
+               return 1;
+       }
+
        # Replace bind parameters values in the query if any
        if (exists $cur_info{$t_pid}{parameters} && ($cur_info{$t_pid}{parameters} =~ /[,\s]*\$(\d+)\s=\s/)) {
                my @t_res = split(/[,\s]*\$(\d+)\s=\s/, $cur_info{$t_pid}{parameters});