#!/usr/bin/perl -w use strict; my $targetdir = '/var/log/web/.tmp'; my $varnishncsa = '/usr/bin/varnishncsa'; # keys in this hash are domain names, value is the name of the logfile to # create for that domain name. # a leading "www." is stripped from the logfile before matching, so # "example.com" will match "www.example.com" but not "images.example.com". my %hostmap = ( 'example1.com' => 'eg1', 'example2.org' => 'example2', '*' => 'other' ); my $file = shift or do { print STDERR "$0 \n\n"; exit 1; }; my $suffix = shift or do { print STDERR "$0 \n\n"; exit 1; }; # open each of the files and replace the entries in the %hostmap # with file handles foreach my $key (keys %hostmap) { my $filename = "$targetdir/$hostmap{$key}.$suffix"; my $fh; if (open $fh, '>>', $filename) { $hostmap{$key} = $fh; } else { print STDERR "Failed to open $filename: $!\n"; exit 4; } } my @parms = ( $varnishncsa, '-r', $file ); if (open RD, '-|', @parms) { while () { # skip requests from 10.x.x.x IPs if (/^10\./) { next; } # match requested domain name if (m#^[^"]+"[^\s]+\s+https?://([^/]+)#) { my $hostname = $1; $hostname =~ s/^www\.//; my $logfile = (defined $hostmap{$hostname}) ? $hostmap{$hostname} : $hostmap{'*'}; print $logfile "$_"; } else { /\(null\)/ and next; } } close RD; foreach my $fh (values %hostmap) { close $fh; } } else { print STDERR "$file: $!\n"; exit 2; }