Access Log pro vhost

Status
Für weitere Antworten geschlossen.

jahlives

Benutzer
Mitglied seit
19. Aug 2008
Beiträge
18.275
Punkte für Reaktionen
4
Punkte
0
Lange habe ich nach einer "guten" Lösung gesucht um Access Log Files des Apache Servers pro (v)host zu haben.
Natürlich könnte ma ganz einfach im httpd-vhost.conf File jedem vhost Eintrag ein eigenes Logfile zu verpassen. Leider bedeutet dies einiges an Tipparbeit wenn man viele vhosts hat. Auch müsste man bei Änderungen des Log Verzeichnisses alle vhost wieder anpassen. Das war mir einfach zu unpraktikabel.
Heute bin ich dann im Apache Manual (tja man sollte die Manuals ab und zu lesen ;) ) zufällig auf die Lösung gestossen: Das perl Script heisst split-logfile und kann ein zentrales Apache Log in Files pro vhost "zerlegen"
Dazu muss man einfach in der Apache Conf (/usr/syno/apache/conf/httpd.conf-user) einen Logeintrag ergänzen:
Code:
LogFormat "%v %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%v %h %l %u %t \"%r\" %>s %b" common
LogFormat "%v %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
das %v am Anfang wird zum verwendeten vhost aufgelöst. split-logfile erwartet als ersten Parameter eines Logeintrages den Hostnamen des vhost!
Dann noch das Logfile festlegen
Code:
CustomLog /opt/var/log/httpd-access.log combined
Das split-logfile Script schaut so aus (ggf Pfad zum perl Interpreter auf der ersten Zeile anpassen)
Code:
#!/opt/bin/perl
%is_open = ();

while ($log_line = <STDIN>) {
    #
    # Get the first token from the log record; it's the
    # identity of the virtual host to which the record
    # applies.
    #
    ($vhost) = split (/\s/, $log_line);
    #
    # Normalize the virtual host name to all lowercase.
    # If it's blank, the request was handled by the default
    # server, so supply a default name.  This shouldn't
    # happen, but caution rocks.
    #
    $vhost = lc ($vhost) or "access";
    #
    # if the vhost contains a "/" or "\", it is illegal so just use 
    # the default log to avoid any security issues due if it is interprted
    # as a directory separator.
    if ($vhost =~ m#[/\\]#) { $vhost = "access" }
    #
    # If the log file for this virtual host isn't opened
    # yet, do it now.
    #
    if (! $is_open{$vhost}) {
        open $vhost, ">>${vhost}.log"
            or die ("Can't open ${vhost}.log");
        $is_open{$vhost} = 1;
    }
    #
    # Strip off the first token (which may be null in the
    # case of the default server), and write the edited
    # record to the current log file.
    #
    $log_line =~ s/^\S*\s+//;
    printf $vhost "%s", $log_line;
}
exit 0;
Das Script kann man so aufrufen (erst ausführbar machen oder sh verwenden)
Code:
split-logfile < /opt/var/log/httpd-access.log
split-logfile erstellt also vom angegebenen Logfile die Files pro Host. Diese werden im aktuellen Verzeichnis gespeichert als HOST_NAME.log
Vergesst also nicht ggf vor dem Aufruf mittels cd in das gewünschte Verzeichnis zu wechseln.

Gruss

tobi
 
Status
Für weitere Antworten geschlossen.
 

Kaffeautomat

Wenn du das Forum hilfreich findest oder uns unterstützen möchtest, dann gib uns doch einfach einen Kaffee aus.

Als Dankeschön schalten wir deinen Account werbefrei.

:coffee:

Hier gehts zum Kaffeeautomat