#!/usr/bin/perl # # Written by Andrii Grytsenko 2009 # # There is variable section my $base="/home/virtwww/w_andriigrytse-net_9c210a4f/logs"; # set path to log files my $base_home="/home/virtwww/w_andriigrytse-net_9c210a4f/stat"; # set path to home directory my $rotatin_period=3; # set rotation period(in days) after this all older files will be deleted my $debug=0; # set to 1 to engage scripto in debug mode # below try to get date my ($day,$mon,$year) = (localtime(time))[3,4,5]; $year=$year+1900; $mon=$mon+1; my @monthes = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); #make array with monthes my $let_mon=$monthes[$mon-1]; # because array starts from zero element my $ip= {}; # init new hash $year =~ m/\d\d(\d\d)/; # get last two digits of the current year and put it into the $l_year variable my $l_year = $1; #my $email=''; my $logfile=sprintf("%s/access_ul1_apache_aux2_%02d-%02d-%02d.log",$base,$l_year,$mon,$day); # generate log name #print "$logfile\n" if $debug; # # This function delete all log files which are older then $rotatin_period # sub rotation { my $rotatin_period=shift; return 1 if ($rotatin_period == 0); @log_files=glob("$base_home/stat*.log"); print "@log_files\n" if $debug; my $cur_time=time; my $border_time=$cur_time-$rotatin_period*24*60*60; foreach my $old_log (@log_files) { my $time = (stat($old_log))[8]; print "File time: $time\t Border time: $border_time\t Current time: $cur_time\n" if $debug; if ($time < $border_time) { unlink($old_log); } } } rotation("$rotatin_period"); open(LOG,"<$logfile") || die "Could not openlog file"; # open logfile while(){ if ($_ =~ m/$day\/$let_mon\/$year/) { /(\d+\.\d+\.\d+\.\d+)/; #try to recognize ip addres $ip{$1}++; } } close LOG; #open (MAIL, "|/usr/sbin/sendmail $email") || die "Unable to send requestn"; #print MAIL "From: root@andriigrytsenko.net"; #print MAIL "Subject: stat for $day $let_mon $year "; my $output=sprintf("%s/stat_%02d-%02d-%02d.log",$base_home,$l_year,$mon,$day); print "$output\n" if $debug; open (FD,">$output") || die "Could not open output file"; foreach my $addr (sort keys %ip) { print FD "$addr = $ip{$addr}\n"; # print MAIL "$addr = $ip{$addr}\n"; } close DF; #close (MAIL);