From 39051280a8e93b2af3a5b1bfee688ff30197bef9 Mon Sep 17 00:00:00 2001 From: Darold Gilles Date: Tue, 26 Nov 2013 19:42:21 +0100 Subject: [PATCH] Add new directive DNSLookupTimeout to change the default timeout for DNS lookup. Add one second timeout when SquidAnalyzer look for an DNS name. Thanks to alex for the patch. --- README | 6 ++++++ SquidAnalyzer.pm | 32 +++++++++++++++++++++++++++++++- doc/SquidAnalyzer.pod | 6 ++++++ doc/squidanalyzer.3 | 7 ++++++- 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/README b/README index 940157c..1238ee1 100644 --- a/README +++ b/README @@ -189,6 +189,12 @@ CONFIGURATION DNS name instead. Note that you must have a working DNS resolution and that it can really slow down the generation of reports. + DNSLookupTimeout 1 + If you have enabled UseClientDNSName and have lot of ip addresses + that do not resolve you may want to increase the DNS lookup timeout. + By default SquidAnalyzer will stop to lookup a DNS name after one + second. + NetworkAlias network-aliases_file Set path to the file containing network alias name. Network are show as Ip addresses so if you want to display name instead create a file diff --git a/SquidAnalyzer.pm b/SquidAnalyzer.pm index 2d957dc..0460f3f 100644 --- a/SquidAnalyzer.pm +++ b/SquidAnalyzer.pm @@ -36,6 +36,9 @@ $ZCAT_PROG = "/bin/zcat"; $BZCAT_PROG = "/bin/bzcat"; $RM_PROG = "/bin/rm"; +# DNS Cache +my %CACHE = {}; + # Default translation srings my %Translate = ( 'CharSet' => 'utf-8', @@ -451,6 +454,7 @@ sub _init $self->{TopUrlUser} = $options{TopUrlUser} || 0; $self->{no_year_stat} = 0; $self->{UseClientDNSName} = $options{UseClientDNSName} || 0; + $self->{DNSLookupTimeout} = $options{DNSLookupTimeout} || 1; if ($self->{Lang}) { open(IN, "$self->{Lang}") or die "ERROR: can't open translation file $self->{Lang}, $!\n"; @@ -601,6 +605,32 @@ sub _init } +sub _gethostbyaddr +{ + my ($self, $ip) = @_; + + return $ip unless $ip=~/\d+\.\d+\.\d+\.\d+/; + + my $host = undef; + unless(exists $CACHE{$ip}) { + eval { + local $SIG{ALRM} = sub { die "timeout\n" }; + alarm $self->{DNSLookupTimeout}; + $host = gethostbyaddr(inet_aton($ip), AF_INET); + alarm 0; + }; + if ($@) { + $CACHE{$ip} = undef; + #printf "_gethostbyaddr timeout : %s\n", $ip; + } + else { + $CACHE{$ip} = $host; + #printf "_gethostbyaddr success : %s (%s)\n", $ip, $host; + } + } + return $CACHE{$ip} || $ip; +} + sub _parseData { my ($self, $time, $elapsed, $client, $code, $bytes, $url, $id, $type) = @_; @@ -636,7 +666,7 @@ sub _parseData # (login is equal to ip) and if client is an ip address if ( ($id eq $client) && $self->{UseClientDNSName}) { if ($client =~ /^\d+\.\d+\.\d+\.\d+$/) { - my $dnsname = gethostbyaddr(inet_aton($client), AF_INET); + my $dnsname = $self->_gethostbyaddr($client); if ($dnsname) { $id = $dnsname; } diff --git a/doc/SquidAnalyzer.pod b/doc/SquidAnalyzer.pod index b14a13c..fc80722 100644 --- a/doc/SquidAnalyzer.pod +++ b/doc/SquidAnalyzer.pod @@ -199,6 +199,12 @@ the client ip address, this allow you to use the DNS name instead. Note that you must have a working DNS resolution and that it can really slow down the generation of reports. +=item DNSLookupTimeout 1 + +If you have enabled UseClientDNSName and have lot of ip addresses that do not +resolve you may want to increase the DNS lookup timeout. By default SquidAnalyzer +will stop to lookup a DNS name after one second. + =item NetworkAlias network-aliases_file Set path to the file containing network alias name. Network are diff --git a/doc/squidanalyzer.3 b/doc/squidanalyzer.3 index 3f7134a..0d51857 100644 --- a/doc/squidanalyzer.3 +++ b/doc/squidanalyzer.3 @@ -124,7 +124,7 @@ .\" ======================================================================== .\" .IX Title "SQUIDANALYZER 1" -.TH SQUIDANALYZER 1 "2013-09-04" "perl v5.14.2" "User Contributed Perl Documentation" +.TH SQUIDANALYZER 1 "2013-11-26" "perl v5.14.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l @@ -330,6 +330,11 @@ thisA\*^\ directive. When you don't have authentication, the username is set to the client ip address, this allow you to use the \s-1DNS\s0 name instead. Note that you must have a working \s-1DNS\s0 resolution and that it can really slow down the generation of reports. +.IP "DNSLookupTimeout 1" 4 +.IX Item "DNSLookupTimeout 1" +If you have enabled UseClientDNSName and have lot of ip addresses that do not +resolve you may want to increase the \s-1DNS\s0 lookup timeout. By default SquidAnalyzer +will stop to lookup a \s-1DNS\s0 name after one second. .IP "NetworkAlias network\-aliases_file" 4 .IX Item "NetworkAlias network-aliases_file" Set path to the file containing network alias name. Network are