#!/usr/bin/perl ############### # SquidLogFilter - by AltBlue # http://www.cs.tuiasi.ro/altblue/ # 10.03.1998 ############### #Usage: SquidLogFilter [-h] [-v] [-html] [] # -h help # -v verbose # -html make HTML file # filter for log file ############### ######################### Variables to be set ################################# $logfile="/var/log/squid/access.log"; # squid log file $outputdir="/home/marius/public_html/proxy"; # output directory $topfile="topsites"; # name for files ############################################################################### ###################### Don't change anything below ############################ ############################################################################### $ver = '1.0'; @author = ('AltBlue','altblue@tuiasi.ro','http://www.cs.tuiasi.ro/altblue/'); $name = 'SquidLogFilter'; ################################################# Get program name $prog = $0; # $prog =~ s/(\.cgi|\.pl)//; # $prog =~ s!^(.*/)!!; # $prog =~ s/\\(.)/sprintf("%%%02x", ord($1))/ge; # ################################################# Get options require 'newgetopt.pl'; # &NGetOpt("h","v","html"); # vprint("$prog is version $ver of $name\nby $author[0] <$author[1]> ($author[2]) \n"); &usage, exit if $opt_h; foreach(@ARGV) { if(!/-v|-h|-html/) { $filter = $_; } } ###### ###### Get initial data ###### open(LOG, "$logfile") || die "Can't open log file: $!\n"; if ($filter) { vprint("Filter log for \'$filter\' ..."); @log = grep(/$filter/,); $topfile="$topfile.$filter"; vprint("Done.\n"); } else { @log=; } close(LOG); ###### ###### Filter log, build top ###### vprint("Filter fields..."); foreach(@log) { if(/http:\/\/|ftp:\/\//) { ($a,$interes,$c)=split(/http:\/\/|ftp:\/\//); ($bun,$kk)=split(/\//,$interes); $TOP{$bun}++; if ($TOP{$bun} == 1) { $key++; $HOSTS[$key]=$bun; } } } vprint("Done.\nSorting...."); foreach (@HOSTS) { push @LISTA, "$TOP{$_}-$_"; } @top = sort {$b <=> $a} @LISTA; pop @top; ###### ###### Writing topfile ###### vprint("OK.\nWriting topsites file \'$topfile\'..."); $topfile = "$topfile.html" if $opt_html; open(OUT, ">$outputdir/$topfile") || die "Can't open output file: $!\n"; if($opt_html) { ############ HTML file print OUT "$name results\n"; if($filter) { print OUT "

$name results for \"$filter\"

\n"; } else { print OUT "

$name general results

\n"; } print OUT <<"HTML";

HTML foreach(@top) { ($loc,$cine)=split(/-/,$_,2); print OUT "\n"; } print OUT <<"HTML";
Site Accesses
$cine$loc

This page was produced by $name ver. $ver,
manufactured by $author[0] ($author[1]) HTML } else { ################### Text file foreach(@top) { ($loc,$cine)=split(/-/,$_,2); printf OUT "%-40s %5s\n", $cine,$loc; } } close(OUT); vprint("OK.\nJob completed.\n"); ######################### # End Main Program ######################### ############ Miscellaneous subroutines sub usage { print STDERR < $author[2] Usage: $prog [-h] [-v] [-html] [] -h help (this message) -v verbose -html make HTML file filter for log file USAGE } sub vprint { print STDERR @_ if $opt_v; }