#!/usr/bin/perl # # Quick scrip to scrub the referrers section of the awstats file. # By: David A. Flanigan # No Warrnety provided or implied, use at your own risk. # Free for any use. Please send improvements back to dave@flanigan.net # E-mail dave@flanigan.net with questions on modifying this script. # http://www.flanigan.net/scripts # # blacklist file format, domain or strings, 1 per line. No regular expressesion. my $blfile = "/etc/awstats/blacklist.txt"; # Name of Domain to clear - should as specified in awstats config. # default domain if nothing is specified in the command line if ($ARGV[0] ne "") { $awdom = $ARGV[0]; } else { $awdom = "www.flanigan.net"; } # init varables use File::Copy; $found = 0; $removed = 0; $date = `/bin/date`; chop $date; #Step 1 - Creat name of the awstats lib file. my $awdate = `/bin/date +%m%Y`; chop $awdate; $awlib = "/var/lib/awstats/awstats$awdate.$awdom.txt"; $awnew = "/var/lib/awstats/awstats$awdate.$awdom.txt.new"; $awbak = "/var/lib/awstats/awstats$awdate.$awdom.txt.awcback"; #check to verify the $awlib file exists before doing anything else if (!( -e $awlib)) { die "Error Opening Lib file: $awlib file not found.\n"; } #creat copy of the awstats file copy($awlib, $awbak); #step 3 open awstats.txt and blacklist file open (AWTXT, "$awlib"); open (AWNEW, ">$awnew"); # Array the BL open (BL, "$blfile"); @blacklist = ; close BL; # Step 3 - main processing loop foreach $line( ) { if ($start == 1) { foreach $item(@blacklist) { chomp $item; #print "debug:$item | $_ | found: $found\n"; if ($line =~ /$item/) { $removed = $removed +1; #print "removed entry: $line"; $found = 1; } } if ($line =~ /END_PAGEREFS/) { $start = 0; print AWNEW "$line"; } if (($found == 0) && ($start == 1)) { print AWNEW "$line"; } $found = 0; } elsif ($line =~ /BEGIN_PAGEREFS/) { $start = 1; print AWNEW "$line"; } else { print AWNEW "$line"; } } close AWTXT; close AWNEW; #replace current file with corrected file copy ($awnew, $awlib); unlink($awnew); #spit out a quick log if ($removed > 0) { open (LOGOUT, ">>/var/log/referspam.log"); print LOGOUT "$date|$removed\n"; close LOGOUT; #priout output to be reutnred by cron #print "\n\nAwstat Clean Script removed $removed referrer spam entries from $awlib\nScript run time: $date\n"; } exit(0);