]> granicus.if.org Git - pgbadger/commitdiff
Add simple csvlog support
authorAlex Hunsaker <badalex@gmail.com>
Thu, 24 May 2012 04:12:16 +0000 (22:12 -0600)
committerAlex Hunsaker <badalex@gmail.com>
Thu, 24 May 2012 04:12:27 +0000 (22:12 -0600)
README
pgbadger

diff --git a/README b/README
index 8232ae96c197ff3a9516fa052e2dd6d37e9d8864..5a1da1afe8e7a5f0eddd6115116dd2d84534dcdc 100755 (executable)
--- a/README
+++ b/README
@@ -50,7 +50,7 @@ pgbadger -l logfile [...]
     -l | --logfile filename: path to the PostgreSQL log file to parse. It can
                              be a plain text log or a gzip compressed file
                              with the .gz extension.
-    -f | --format logtype  : the value can be: syslog or stderr. Default: stderr
+    -f | --format logtype  : the value can be: syslog, stderr or 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.
index 56b1d4e23c425b69011646f516dcc009b37bacca..49eae27cf1293424b8888e9a7519246390946ad8 100755 (executable)
--- a/pgbadger
+++ b/pgbadger
@@ -27,7 +27,6 @@ use strict;
 use Getopt::Long;
 use IO::File;
 use Benchmark;
-use IO::File;
 use File::Basename;
 use Time::Local 'timelocal_nocheck';
 
@@ -301,6 +300,12 @@ my $CURRENT_DATE = $gyear . sprintf("%02d", $gmon+1) . sprintf("%02d",$gmday);
 
 my $cur_td = $t0;
 
+my $csv_obj;
+if ($format eq 'csv') {
+       require Text::CSV;
+       $csv_obj = Text::CSV->new({'binary'=>1});
+}
+
 while (my $line = <$lfile>) {
        $cursize += length($line);
        chomp($line);
@@ -361,7 +366,38 @@ while (my $line = <$lfile>) {
                        $cur_info{query} .= "\n" . $line if ($cur_info{query});
                }
 
-       } else {
+       } elsif ($format eq 'csv') {
+
+               # Parse csvlog lines
+               if ($csv_obj->parse($line)) {
+                       my @cols = $csv_obj->fields();
+
+                       # Extract the date
+                       $cols[0] =~ m/(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)/;
+                       my @date = ($1, $2, $3, $4, $5, $6);
+
+                       # Skip unwanted lines
+                       my $cur_date = join('', @date);
+                       next if ($from && ($from > $cur_date));
+                       last if ($to && ($to < $cur_date));
+
+                       # Process the log line
+                       &parse_query(
+                               @date,
+                               $cols[4],       # connection from
+                               $cols[3],       # pid
+                               $cols[5],       # session
+                               # logprefix
+                               'user='.$cols[1] . ',db='.$cols[2],
+                               $cols[11].':', # loglevel
+                               $cols[13], # query
+                       );
+               }else {
+                       &logmsg('DEBUG', "Unknown csv line format: $line, error: ". $csv_obj->error_input());
+               }
+
+       } else
+       {
                # unknown format
                &logmsg('DEBUG', "Unknown line format: $line");
        }
@@ -406,7 +442,7 @@ Usage: $0 -l logfile [...]
     -l | --logfile filename: path to the PostgreSQL log file to parse. It can
                             be a plain text log or a gzip compressed file
                             with the .gz extension.
-    -f | --format logtype  : the value can be: syslog or stderr. Default: stderr
+    -f | --format logtype  : the value can be: syslog, stderr or 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.
@@ -2207,6 +2243,9 @@ sub autodetect_format
                } elsif ($line =~ /\d+-\d+-\d+\s+\d+:\d+:\d+\s+[^\s]+\s+\[\d+\]:\s+\[[0-9\-]+\]\s+[^:]+:\s+duration:/) {
                        $fmt = 'stderr';
                        $nfound++;
+               } elsif ($line =~ tr/,/,/ >= 12) {
+                       $fmt = 'csv';
+                       $nfound++;
                }
                last if (($nfound > 10) || ($nline > 5000));
        }