From: Darold Gilles Date: Thu, 30 Oct 2014 17:40:01 +0000 (+0100) Subject: Add autodetection of client=%h or remote=%h from the log so that adding a prefix... X-Git-Tag: v6.3~34 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e0cb24f9bb03a2e4519f01055266a0a485f06912;p=pgbadger Add autodetection of client=%h or remote=%h from the log so that adding a prefix is not needed when it respect the default of pgbadger. --- diff --git a/README b/README index 48cdcb1..8f99c82 100644 --- a/README +++ b/README @@ -250,6 +250,7 @@ FEATURE Sessions per database/user/client. Connections per database/user/client. Autovacuum and autoanalyze per table. + Histogram of sessions times. All charts are zoomable and can be saved as PNG images. SQL queries reported are highlighted and beautified automatically. @@ -350,21 +351,22 @@ POSTGRESQL CONFIGURATION log_line_prefix = '%t [%p]: [%l-1] ' - Log line prefix could add user and database name as follows: + Log line prefix could add user, database name and client ip address as + follows: - log_line_prefix = '%t [%p]: [%l-1] user=%u,db=%d ' + log_line_prefix = '%t [%p]: [%l-1] user=%u,db=%d,client=%h ' or for syslog log file format: - log_line_prefix = 'user=%u,db=%d ' + log_line_prefix = 'user=%u,db=%d,client=%h ' Log line prefix for stderr output could also be: - log_line_prefix = '%t [%p]: [%l-1] db=%d,user=%u ' + log_line_prefix = '%t [%p]: [%l-1] db=%d,user=%u,client=%h ' or for syslog output: - log_line_prefix = 'db=%d,user=%u ' + log_line_prefix = 'db=%d,user=%u,client=%h ' You need to enable other parameters in postgresql.conf to get more information from your log files: diff --git a/doc/pgBadger.pod b/doc/pgBadger.pod index 34abf3f..35fdce5 100644 --- a/doc/pgBadger.pod +++ b/doc/pgBadger.pod @@ -236,6 +236,7 @@ There's also some pie reports of distribution about: Sessions per database/user/client. Connections per database/user/client. Autovacuum and autoanalyze per table. + Histogram of sessions times. All charts are zoomable and can be saved as PNG images. SQL queries reported are highlighted and beautified automatically. @@ -333,21 +334,21 @@ With 'stderr' log format, log_line_prefix must be at least: log_line_prefix = '%t [%p]: [%l-1] ' -Log line prefix could add user and database name as follows: +Log line prefix could add user, database name and client ip address as follows: - log_line_prefix = '%t [%p]: [%l-1] user=%u,db=%d ' + log_line_prefix = '%t [%p]: [%l-1] user=%u,db=%d,client=%h ' or for syslog log file format: - log_line_prefix = 'user=%u,db=%d ' + log_line_prefix = 'user=%u,db=%d,client=%h ' Log line prefix for stderr output could also be: - log_line_prefix = '%t [%p]: [%l-1] db=%d,user=%u ' + log_line_prefix = '%t [%p]: [%l-1] db=%d,user=%u,client=%h ' or for syslog output: - log_line_prefix = 'db=%d,user=%u ' + log_line_prefix = 'db=%d,user=%u,client=%h ' You need to enable other parameters in postgresql.conf to get more information from your log files: diff --git a/pgbadger b/pgbadger index 4feb2e2..6a0fa0f 100755 --- a/pgbadger +++ b/pgbadger @@ -14,6 +14,11 @@ # Log line prefix should be: log_line_prefix = '%t [%p]: [%l-1] ' # Log line prefix should be: log_line_prefix = '%t [%p]: [%l-1] user=%u,db=%d ' # Log line prefix should be: log_line_prefix = '%t [%p]: [%l-1] db=%d,user=%u ' +# If you need report per client Ip adresses you can add client=%h or remote=%h +# pgbadger will also recognized the following form: +# log_line_prefix = '%t [%p]: [%l-1] db=%d,user=%u,client=%h ' +# or +# log_line_prefix = '%t [%p]: [%l-1] user=%u,db=%d,remote=%h ' # With syslog output # Log line prefix should be: log_line_prefix = 'db=%d,user=%u ' # @@ -603,9 +608,10 @@ if ($error_only && $disable_error) { die "FATAL: please choose between no event report and reporting events only.\n"; } -# Set default search pattern for database and user name in log_line_prefix +# Set default search pattern for database, user name and host in log_line_prefix my $regex_prefix_dbname = qr/db=([^,]*)/; my $regex_prefix_dbuser = qr/user=([^,]*)/; +my $regex_prefix_dbclient = qr/(?:client|remote)=([^,]*)/; # Loading excluded query from file if any if ($exclude_file) { @@ -8880,15 +8886,13 @@ sub parse_log_prefix if ($t_logprefix =~ $regex_prefix_dbname) { $prefix_vars{'t_dbname'} = $1; } - } -} - -# OBSOLETE: to be removed -sub is_session_closed -{ - map { return 1 if ($_[0] =~ $_); } @session_closed_msg; - return 0; + # Search for database name + if ($t_logprefix =~ $regex_prefix_dbclient) { + $prefix_vars{'t_dbclient'} = $1; + $prefix_vars{'t_dbclient'} = _gethostbyaddr($prefix_vars{'t_dbclient'}) if ($dns_resolv); + } + } } sub parse_query @@ -8980,7 +8984,7 @@ sub parse_query $cur_lock_info{$t_pid}{timestamp} = $prefix_vars{'t_timestamp'}; $cur_lock_info{$t_pid}{dbname} = $prefix_vars{'t_dbname'}; $cur_lock_info{$t_pid}{dbuser} = $prefix_vars{'t_dbuser'}; - $cur_lock_info{$t_pid}{dbclient} = $prefix_vars{'t_client'}; + $cur_lock_info{$t_pid}{dbclient} = $prefix_vars{'t_client'} || $prefix_vars{'t_dbclient'}; $cur_lock_info{$t_pid}{dbappname} = $prefix_vars{'t_appname'}; } return; @@ -8992,7 +8996,7 @@ sub parse_query $cur_temp_info{$t_pid}{timestamp} = $prefix_vars{'t_timestamp'}; $cur_temp_info{$t_pid}{dbname} = $prefix_vars{'t_dbname'}; $cur_temp_info{$t_pid}{dbuser} = $prefix_vars{'t_dbuser'}; - $cur_temp_info{$t_pid}{dbclient} = $prefix_vars{'t_client'}; + $cur_temp_info{$t_pid}{dbclient} = $prefix_vars{'t_client'} || $prefix_vars{'t_dbclient'}; $cur_temp_info{$t_pid}{dbappname} = $prefix_vars{'t_appname'}; return; } @@ -9003,7 +9007,7 @@ sub parse_query $cur_lock_info{$t_pid}{timestamp} = $prefix_vars{'t_timestamp'}; $cur_lock_info{$t_pid}{dbname} = $prefix_vars{'t_dbname'}; $cur_lock_info{$t_pid}{dbuser} = $prefix_vars{'t_dbuser'}; - $cur_lock_info{$t_pid}{dbclient} = $prefix_vars{'t_client'}; + $cur_lock_info{$t_pid}{dbclient} = $prefix_vars{'t_client'} || $prefix_vars{'t_dbclient'}; $cur_lock_info{$t_pid}{dbappname} = $prefix_vars{'t_appname'}; return; } @@ -9026,7 +9030,7 @@ sub parse_query $cur_temp_info{$t_pid}{timestamp} = $prefix_vars{'t_timestamp'}; $cur_temp_info{$t_pid}{dbname} = $prefix_vars{'t_dbname'}; $cur_temp_info{$t_pid}{dbuser} = $prefix_vars{'t_dbuser'}; - $cur_temp_info{$t_pid}{dbclient} = $prefix_vars{'t_client'}; + $cur_temp_info{$t_pid}{dbclient} = $prefix_vars{'t_client'} || $prefix_vars{'t_dbclient'}; $cur_temp_info{$t_pid}{dbappname} = $prefix_vars{'t_appname'}; } return; @@ -9038,7 +9042,7 @@ sub parse_query $cur_temp_info{$t_pid}{timestamp} = $prefix_vars{'t_timestamp'}; $cur_temp_info{$t_pid}{dbname} = $prefix_vars{'t_dbname'}; $cur_temp_info{$t_pid}{dbuser} = $prefix_vars{'t_dbuser'}; - $cur_temp_info{$t_pid}{dbclient} = $prefix_vars{'t_client'}; + $cur_temp_info{$t_pid}{dbclient} = $prefix_vars{'t_client'} || $prefix_vars{'t_dbclient'}; $cur_temp_info{$t_pid}{dbappname} = $prefix_vars{'t_appname'}; return; } @@ -9047,6 +9051,7 @@ sub parse_query if (($prefix_vars{'t_loglevel'} eq 'LOG') && ($prefix_vars{'t_query'} =~ /connection received: host=([^\s]+)(?: port=(\d+))?/)) { return if ($disable_connection); $conn_received{$t_pid} = $1; + $conn_received{$t_pid} = _gethostbyaddr($conn_received{$t_pid}) if ($dns_resolv); return; } @@ -9068,8 +9073,10 @@ sub parse_query } if ($prefix_vars{'t_query'} =~ / host=([^\s]+)/) { $host = $1; - } elsif ($prefix_vars{'t_client'}) { - $host = $prefix_vars{'t_client'}; + $host = _gethostbyaddr($host) if ($dns_resolv); + } elsif ($prefix_vars{'t_client'} || $prefix_vars{'t_dbclient'}) { + $host = $prefix_vars{'t_client'} || $prefix_vars{'t_dbclient'}; + $host = _gethostbyaddr($host) if ($dns_resolv); } if ($extension eq 'tsung') { $tsung_session{$prefix_vars{'t_pid'}}{connection}{database} = $db; @@ -9120,6 +9127,7 @@ sub parse_query my $usr = $2; my $db = $3; my $host = $4; + $host = _gethostbyaddr($host) if ($dns_resolv); if ($extension eq 'tsung') { &store_tsung_session($prefix_vars{'t_pid'}); @@ -9462,7 +9470,7 @@ sub set_current_infos $cur_info{$t_pid}{loglevel} = $prefix_vars{'t_loglevel'}; $cur_info{$t_pid}{dbname} = $prefix_vars{'t_dbname'}; $cur_info{$t_pid}{dbuser} = $prefix_vars{'t_dbuser'}; - $cur_info{$t_pid}{dbclient} = $prefix_vars{'t_client'}; + $cur_info{$t_pid}{dbclient} = $prefix_vars{'t_client'} || $prefix_vars{'t_dbclient'}; $cur_info{$t_pid}{dbappname} = $prefix_vars{'t_appname'}; $cur_info{$t_pid}{date} = $prefix_vars{'t_date'}; $cur_info{$t_pid}{bind} = $prefix_vars{'t_bind'};