From 0e3b4449d9142a2b00b51bc2ea5981f02157b087 Mon Sep 17 00:00:00 2001 From: Darold Date: Sat, 23 Jun 2012 10:55:00 +0200 Subject: [PATCH] Add --exclude-query option to exclude types of queries by specifying a regex - Isaac Reuben --- README | 4 ++++ doc/pgBadger.pod | 5 +++++ pgbadger | 26 ++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/README b/README index ab49826..f515dac 100644 --- a/README +++ b/README @@ -41,12 +41,16 @@ SYNOPSIS -v | --version : show current version --pie-limit num : do not show pie data lower that num%, show a sum of them instead. -w | -watch-mode : only report errors just like logwatch could do. + --exclude-query regex : any query matching the given regex will be excluded + from the report. For example: "^(COPY|VACUUM|COMMIT)" + you can use this option multiple time. Examples: pgbadger -p -g /var/log/postgresql.log pgbadger -p -g /var/log/postgres.log.2.gz /var/log/postgres.log.1.gz /var/log/postgres.log pgbadger -p -g `ls /var/log/postgresql/postgresql-2012-05-*` + pgbadger -p -g --exclude-query="^(COPY|VACUUM|COMMIT)" /var/log/postgresql.log Reporting errors every week by cron job: diff --git a/doc/pgBadger.pod b/doc/pgBadger.pod index 64fc567..c27eff2 100644 --- a/doc/pgBadger.pod +++ b/doc/pgBadger.pod @@ -43,12 +43,17 @@ Options: -v | --version : show current version --pie-limit num : do not show pie data lower that num%, show a sum of them instead. -w | -watch-mode : only report errors just like logwatch could do. + --exclude-query regex : any query matching the given regex will be excluded + from the report. For example: "^(COPY|VACUUM|COMMIT)" + you can use this option multiple time. Examples: pgbadger -p -g /var/log/postgresql.log pgbadger -p -g /var/log/postgres.log.2.gz /var/log/postgres.log.1.gz /var/log/postgres.log pgbadger -p -g `ls /var/log/postgresql/postgresql-2012-05-*` + pgbadger -p -g --exclude-query="^(COPY|VACUUM|COMMIT)" /var/log/postgresql.log + Reporting errors every week by cron job: diff --git a/pgbadger b/pgbadger index 78d2fc1..40619e1 100755 --- a/pgbadger +++ b/pgbadger @@ -64,6 +64,7 @@ my $regex_prefix_dbuser = ''; my $quiet = 0; my $progress = 0; my $error_only = 0; +my @exclude_query = (); my $NUMPROGRESS = 10000; my @DIMENSIONS = (800,300); @@ -107,6 +108,7 @@ my $result = GetOptions ( "pie-limit=i" => \$pie_percentage_limit, "image-format=s"=> \$img_format, "w|watch-mode!" => \$error_only, + "exclude-query=s" => \@exclude_query, ); if ($ver) { @@ -187,6 +189,15 @@ if (not defined $tmpfh) { $tmpfh->close(); unlink($outfile) if (-e $outfile); +# Testing regex syntaxe +if ($#exclude_query >= 0) { + foreach my $r (@exclude_query) { + eval { m/$r/i; }; + if ($@) { + die "FATAL: invalid regex '$r', $!\n"; + } + } +} # Check start/end date time if ($from) { if ($from =~ /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/) { @@ -500,6 +511,9 @@ Options: enabled with this option. --pie-limit num : pie data lower than num% will show a sum instead. -w | -watch-mode : only report errors just like logwatch could do. + --exclude-query regex : any query matching the given regex will be excluded + from the report. For example: "^(COPY|VACUUM|COMMIT)" + you can use this option multiple time. Examples: @@ -507,6 +521,7 @@ Examples: pgbadger -p -g /var/log/postgres.log.2.gz /var/log/postgres.log.1.gz \ /var/log/postgres.log pgbadger -p -g `ls /var/log/postgresql/postgresql-2012-05-*` + pgbadger -p -g --exclude-query="^(COPY|VACUUM|COMMIT)" /var/log/postgresql.log Reporting errors every week by cron job: @@ -2521,8 +2536,19 @@ sub parse_query # Cleanup and normalize the current query $cur_info{query} =~ s/^[\t\s]+//s; $cur_info{query} =~ s/[\t\s]+$//s; + # Should we have to exclude some queries + if ($#exclude_query >= 0) { + foreach (@exclude_query) { + if ($cur_info{query} =~ /$_/i) { + $cur_info{query} = ''; + return; + } + } + } $cur_info{query} = substr($cur_info{query}, 0, $maxlength) . '[...]' if (($maxlength > 0) && (length($cur_info{query}) > $maxlength)); $cur_info{query} .= ';' if (substr($cur_info{query}, -1, 1) ne ';'); + + my $normalized = &normalize_query($cur_info{query}); # Stores global statistics -- 2.50.1