From: Darold Date: Sat, 9 Jun 2012 11:57:33 +0000 (+0200) Subject: Add -w option to only report errors X-Git-Tag: v3.2~228 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ff31bab3d2b28a03f28c107617983f15637f1735;p=pgbadger Add -w option to only report errors --- diff --git a/README b/README index 10c2bac..ab49826 100644 --- a/README +++ b/README @@ -40,6 +40,7 @@ SYNOPSIS -p | --progress : show a progress bar, quiet mode is enabled with this option. -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. Examples: @@ -47,6 +48,13 @@ SYNOPSIS 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-*` + Reporting errors every week by cron job: + + 30 23 * * 1 /usr/bin/pgbadger -q -w /var/log/postgresql.log -o /var/reports/pg_errors.html + + this suppose that your log file and HTML report are also rotated every + weeks. + DESCRIPTION pgBadger is a PostgreSQL log analyzer build for speed with fully detailed reports from your PostgreSQL log file. It's a single and small diff --git a/doc/pgBadger.pod b/doc/pgBadger.pod index 7afbb4a..64fc567 100644 --- a/doc/pgBadger.pod +++ b/doc/pgBadger.pod @@ -42,6 +42,7 @@ Options: -p | --progress : show a progress bar, quiet mode is enabled with this option. -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. Examples: @@ -49,6 +50,11 @@ 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-*` +Reporting errors every week by cron job: + + 30 23 * * 1 /usr/bin/pgbadger -q -w /var/log/postgresql.log -o /var/reports/pg_errors.html + +this suppose that your log file and HTML report are also rotated every weeks. =head1 DESCRIPTION diff --git a/pgbadger b/pgbadger index 5685d1e..3c2ad9a 100755 --- a/pgbadger +++ b/pgbadger @@ -61,6 +61,7 @@ my $regex_prefix_dbname = ''; my $regex_prefix_dbuser = ''; my $quiet = 0; my $progress = 0; +my $error_only = 0; my $NUMPROGRESS = 10000; my @DIMENSIONS = (800,300); @@ -97,7 +98,8 @@ my $result = GetOptions ( "q|quiet!" => \$quiet, "p|progress!" => \$progress, "pie-limit=i" => \$pie_percentage_limit, - "image-format=s" => \$img_format, + "image-format=s"=> \$img_format, + "w|watch-mode!" => \$error_only, ); if ($ver) { @@ -432,9 +434,17 @@ if (not defined $fh) { die "FATAL: can't write to $outfile, $!\n"; } if ( ($extension eq 'text') || ($extension eq 'txt') ) { - &dump_as_text(); + if ($error_only) { + &dump_error_as_text(); + } else { + &dump_as_text(); + } } else { - &dump_as_html(); + if ($error_only) { + &dump_error_as_html(); + } else { + &dump_as_html(); + } } $fh->close; @@ -462,7 +472,7 @@ Options: with the .gz extension. Note that this option is DEPRECATED, set logfile as a command line argument instead. - -f | --format logtype : the value can be: syslog, stderr or csv. Default: stderr + -f | --format logtype : possible values: syslog,stderr,csv. Default: stderr -o | --outfile filename: define the filename for the output. Default depends of the output format: out.html or out.txt. To dump output to stdout use - as filename. @@ -478,15 +488,25 @@ Options: -g | --graph : generate graphs using the Flotr2 javascript library -b | --begin datetime : start date/time for the data to be parsed in log. -e | --end datetime : end date/time for the data to be parsed in log. - -q | --quiet : disable output to stderr and don't print debug informations. - -p | --progress : show a progress bar, quiet mode is enabled with this option. - --pie-limit num : do not show pie data lower that num%, show a sum of them instead. + -q | --quiet : don't print anything to stdout. + -p | --progress : show a progress bar, quiet mode is automaticaly + 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. 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 /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-*` + +Reporting errors every week by cron job: + + 30 23 * * 1 /usr/bin/pgbadger -q -w /var/log/postgresql.log -o /var/reports/pg_errors.html + +this suppose that your log file and HTML report are also rotated every weeks. + }; exit 0; @@ -757,6 +777,41 @@ Report not supported by text format } +sub dump_error_as_text +{ + + # Global informations + my $curdate = localtime(time); + my $fmt_nlines = &comma_numbers($nlines); + my $total_time = timestr($td); + $total_time =~ s/^([\.0-9]+) wallclock.*/$1/; + $total_time = &convert_time($total_time*1000); + my $logfile_str = join(',', @log_files); + print $fh qq{ + +- Global informations -------------------------------------------------- + +Generated on $curdate +Log file: $logfile_str +Parsed $fmt_nlines log entries in $total_time +Log start from $first_log_date to $last_log_date +}; + + print $fh "\n- Most frequent errors (N) ---------------------------------------------\n\n"; + my $idx = 1; + foreach my $k (sort {$error_info{$b}{count} <=> $error_info{$a}{count}} keys %error_info) { + next if (!$error_info{$k}{count}); + last if ($idx > $top); + print $fh "$idx) " . &comma_numbers($error_info{$k}{count}) . " - $k\n"; + print $fh "--\n"; + $idx++; + } + print $fh "\n\n"; + print $fh "Report generated by PgBadger $VERSION.\n"; + +} + + sub html_header { my $date = localtime(time); @@ -975,56 +1030,57 @@ function toggle(idButton, idDiv, label) { /* ]]> */ }; -if ($JQGRAPH) { - my @jslib = ; - print $fh <; + print $fh <

PgBadger

- +}; + if (!$error_only) { + print $fh qq{ -

Normalized reports are marked with a "(N)".

-}; + if (scalar keys %lock_info > 0) { + print $fh qq{Locks by type |}; + } + if (exists $session_info{database}) { + print $fh qq{Sessions per database |}; + } + if (exists $session_info{user}) { + print $fh qq{Sessions per user |}; + } + if (exists $session_info{host}) { + print $fh qq{Sessions per host |}; + } + if (exists $connection_info{database}) { + print $fh qq{Connections per database |}; + } + if (exists $connection_info{user}) { + print $fh qq{Connections per user |}; + } + if (exists $connection_info{host}) { + print $fh qq{Connections per host |}; + } + print $fh "
\n"; + } + print $fh "

Normalized reports are marked with a \"(N)\".

\n"; } @@ -1037,40 +1093,46 @@ sub html_footer Report generated by PgBadger $VERSION. +}; + if (!$error_only) { + print $fh qq{
Table of contents
+}; + } + print $fh qq{ }; @@ -2018,6 +2080,85 @@ sub dump_as_html } +sub dump_error_as_html +{ + + # Dump the html header + &html_header(); + + # Global informations + my $curdate = localtime(time); + my $fmt_nlines = &comma_numbers($nlines); + my $total_time = timestr($td); + $total_time =~ s/^([\.0-9]+) wallclock.*/$1/; + $total_time = &convert_time($total_time*1000); + my $logfile_str = join(',', @log_files); + print $fh qq{ +
+
    +
  • Generated on $curdate
  • +
  • Log file: $logfile_str
  • +
  • Parsed $fmt_nlines log entries in $total_time
  • +
  • Log start from $first_log_date to $last_log_date
  • +
+
+}; + + print $fh qq{ +

Most frequent errors (N) ^

+ + + + + + + +}; + my $idx = 1; + foreach my $k (sort {$error_info{$b}{count} <=> $error_info{$a}{count}} keys %error_info) { + next if (!$error_info{$k}{count}); + last if ($idx > $top); + my $col = $idx % 2; + print $fh "\n"; + if ($error_info{$k}{count} > 1) { + print $fh "\n"; + $idx++; + } + print $fh "
RankTimes reportedError
$idx
", &comma_numbers($error_info{$k}{count}), ""; + print $fh "
"; + foreach my $d (sort keys %{$error_info{$k}{chronos}}) { + my $c = 1; + $d =~ /^\d{4}(\d{2})(\d{2})$/; + my $zday = "$abbr_month{$1} $2"; + foreach my $h (sort keys %{$error_info{$k}{chronos}{$d}}) { + my $colb = $c % 2; + $zday = " " if ($c > 1); + print $fh ""; + $c++; + } + } + print $fh "
DayTimeCount
$zday$h", &comma_numbers($error_info{$k}{chronos}{$d}{$h}{count}), "
$k
"; + print $fh "
"; + for (my $i = 0; $i <= $#{$error_info{$k}{date}}; $i++) { + my $c = $i % 2; + print $fh "
$error_info{$k}{error}[$i]
\n"; + print $fh "
Detail: $error_info{$k}{detail}[$i]
\n" if ($error_info{$k}{detail}[$i]); + } + print $fh "
"; + } elsif ($error_info{$k}{detail}[0]) { + print $fh "
$k
"; + print $fh "
Detail: $error_info{$k}{detail}[0]
\n"; + } else { + print $fh "
$k
"; + } + print $fh "
\n"; + + # Dump the html footer + &html_footer(); + +} + + # Highlight SQL code sub highlight_code { @@ -2065,6 +2206,7 @@ sub parse_query return; } next if ($t_loglevel eq 'CONTEXT'); + next if ( $error_only && ($t_loglevel !~ /(WARNING|ERROR|FATAL|PANIC|DETAIL|HINT|STATEMENT)/) ); # Extract user and database information from the logprefix part my $t_dbname = '';