From: Darold Gilles Date: Tue, 7 Oct 2014 17:25:22 +0000 (+0200) Subject: Add -D | --dns-resolv command line option to replace ip adresses by their DNS name... X-Git-Tag: v6.2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=cc76e6322f5415bc8e68a1d8f9978900a22cf961;p=pgbadger Add -D | --dns-resolv command line option to replace ip adresses by their DNS name. Be warned that this can slow down pgBagder a lot. Thanks to Jiri Hlinka for the feature request. --- diff --git a/README b/README index 847b8ff..3dba42b 100644 --- a/README +++ b/README @@ -23,6 +23,8 @@ SYNOPSIS -c | --dbclient host : only report on entries for the given client host. -C | --nocomment : remove comments like /* ... */ from queries. -d | --dbname database : only report on entries for the given database. + -D | --dns-resolv : client ip adresses are replaced by their DNS name. + Be warned that this can really slow down pgBadger. -e | --end datetime : end date/time for the data to be parsed in log. -f | --format logtype : possible values: syslog,stderr,csv. Default: stderr. -G | --nograph : disable graphs on HTML output. Enabled by default. diff --git a/doc/pgBadger.pod b/doc/pgBadger.pod index cfb3dd9..30d4247 100644 --- a/doc/pgBadger.pod +++ b/doc/pgBadger.pod @@ -25,6 +25,8 @@ Options: -c | --dbclient host : only report on entries for the given client host. -C | --nocomment : remove comments like /* ... */ from queries. -d | --dbname database : only report on entries for the given database. + -D | --dns-resolv : client ip adresses are replaced by their DNS name. + Be warned that this can really slow down pgBadger. -e | --end datetime : end date/time for the data to be parsed in log. -f | --format logtype : possible values: syslog,stderr,csv. Default: stderr. -G | --nograph : disable graphs on HTML output. Enabled by default. diff --git a/pgbadger b/pgbadger index d165c4b..2d7dfb2 100755 --- a/pgbadger +++ b/pgbadger @@ -43,6 +43,7 @@ use File::Temp qw/ tempfile /; use IO::Handle; use IO::Pipe; use FileHandle; +use Socket; $VERSION = '6.1'; @@ -60,6 +61,8 @@ my $graphid = 1; my $NODATA = '
NO DATASET
'; my $MAX_QUERY_LENGTH = 20480; my $terminate = 0; +my %CACHE_DNS = (); +my $DNSLookupTimeout = 1; # (in seconds) my $pgbadger_logo = ' \@dbclient, "C|nocomment!" => \$remove_comment, "d|dbname=s" => \@dbname, + "D|dns-resolv!" => \$dns_resolv, "e|end=s" => \$to, "f|format=s" => \$format, "G|nograph!" => \$nograph, @@ -1549,6 +1554,8 @@ Options: -c | --dbclient host : only report on entries for the given client host. -C | --nocomment : remove comments like /* ... */ from queries. -d | --dbname database : only report on entries for the given database. + -D | --dns-resolv : client ip adresses are replaced by their DNS name. + Be warned that this can really slow down pgBadger. -e | --end datetime : end date/time for the data to be parsed in log. -f | --format logtype : possible values: syslog,stderr,csv. Default: stderr. -G | --nograph : disable graphs on HTML output. Enabled by default. @@ -2003,6 +2010,7 @@ sub process_file $prefix_vars{'t_appname'} = $row->[22] || ''; $prefix_vars{'t_client'} = $row->[4] || ''; $prefix_vars{'t_client'} =~ s/:.*//; + $prefix_vars{'t_client'} = _gethostbyaddr($prefix_vars{'t_client'}) if ($dns_resolv); $prefix_vars{'t_host'} = 'csv'; $prefix_vars{'t_pid'} = $row->[3]; $prefix_vars{'t_session_line'} = $row->[5]; @@ -2151,6 +2159,7 @@ sub process_file next if (!&check_incremental_position($prefix_vars{'t_timestamp'}, $line)); $cur_pid = $prefix_vars{'t_pid'}; $goon = 1; + $prefix_vars{'t_client'} = _gethostbyaddr($prefix_vars{'t_client'}) if ($dns_resolv); # Store the current timestamp of the log line &store_current_timestamp($prefix_vars{'t_timestamp'}); @@ -2283,6 +2292,7 @@ sub process_file # Jump to the last line parsed if required next if (!&check_incremental_position($prefix_vars{'t_timestamp'}, $line)); $cur_pid = $prefix_vars{'t_pid'}; + $prefix_vars{'t_client'} = _gethostbyaddr($prefix_vars{'t_client'}) if ($dns_resolv); # Store the current timestamp of the log line &store_current_timestamp($prefix_vars{'t_timestamp'}); @@ -8727,7 +8737,6 @@ sub validate_log_line if ($#dbclient >= 0) { # Log line does not match the required dbclient - $prefix_vars{'t_client'} ||= $prefix_vars{'t_hostport'}; if (!$prefix_vars{'t_client'} || !grep(/^$prefix_vars{'t_client'}$/i, @dbclient)) { delete $current_sessions{$prefix_vars{'t_pid'}}; return 0; @@ -11126,7 +11135,31 @@ sub get_calendar } +sub _gethostbyaddr +{ + my $ip = shift; + + my $host = undef; + unless(exists $CACHE_DNS{$ip}) { + eval { + local $SIG{ALRM} = sub { die "DNS lookup timeout.\n"; }; + alarm($DNSLookupTimeout); + $host = gethostbyaddr(inet_aton($ip), AF_INET); + alarm(0); + }; + if ($@) { + $CACHE_DNS{$ip} = undef; + #printf "_gethostbyaddr timeout : %s\n", $ip; + } + else { + $CACHE_DNS{$ip} = $host; + #printf "_gethostbyaddr success : %s (%s)\n", $ip, $host; + } + } + + return $CACHE_DNS{$ip} || $ip; +} __DATA__