
Verschiedene Apache-Server in einem int mit geringer Auslastungranet mit fast nur DHCP-Clients, müssen wir Hostnamen statt IP-Adressen protokollieren. Da die DHCP-Umgebung sehr dynamisch ist, würde jeder spätere Versuch, IPs Hostnamen neu zuzuordnen, höchstwahrscheinlich falsche Ergebnisse liefern.
Obwohl wir " HostnameLookups On
" haben, protokolliert nur das Zugriffsprotokoll ordnungsgemäß Hostnamen, das Fehlerprotokoll jedoch nicht.
Wenn ich darüber lese ErrorLogFormat
, stelle ich fest, dass es keine%H, aber nur%A(bedeutet „Client-IP-Adresse und Port“).
Gibt es also wirklich keine Möglichkeit, Apache auch dazu zu bringen, Hostnamen im Fehlerprotokoll aufzuzeichnen...?
Antwort1
Nicht nativ mit der ErrorLog-Direktive.
Ich würde ein Skript schreiben, das die Auflösung für Sie übernimmt und das Fehlerprotokoll darüber leitet. In Ihrer Apache-Konfiguration etwa so:
Errorlog "|/usr/local/bin/errorlog_resolver.pl"
Und dann ein Beispiel für ein Perl-Skript:
#!/usr/bin/perl -w
# errorlog_resolver.pl
# Give apache ErrorLog on STDIN, outputs them with numeric IP addresses
# in the likely (host) field converted to hostnames (where possible).
# based on clf_lookup.plx from "Perl for Web Site Management"
# http://oreilly.com/catalog/perlwsmng/chapter/ch08.html
#
use strict;
use Socket;
open LOGFILE, ">>/tmp/my_error_log" or die "Couldn't open file: $!";
my %hostname;
while (<>) {
my $line = $_;
my($day, $month, $dayn, $hour, $year, $err, $client, $host, $rest) = split / /, $line, 9;
if ( $client ~~ "[client" ) {
# remove the ] trailing the likely ip-address.
my $chr = chop($host);
if ($host =~ /^\d+\.\d+\.\d+\.\d+$/) {
# looks vaguely like an IP address
unless (exists $hostname{$host}) {
# no key, so haven't processed this IP before
$hostname{$host} = gethostbyaddr(inet_aton($host), AF_INET);
}
if ($hostname{$host}) {
# only processes IPs with successful lookups
$line = "$day $month $dayn $hour $year $err $client $hostname{$host}\($host\)\] $rest)";
}
}
}
print LOGFILE $line;
}