#!/usr/bin/perl use Mysql; $db="sanitized!"; $host="sanitized!"; $userid="sanitized!"; $passwd="sanitized!"; $dbh = Mysql->Connect($host,$db,$userid,$passwd); $|=1; #$line = `pwd`; #$line =~ s/\/var\/log\/proxies\///g; $server = `hostname`; chomp $server; #print "Fetching Logs.. \n"; opendir DIR,"/var/log/apache2/proxies/"; @files = sort readdir DIR; foreach $file(@files) { if ($file =~ /access(.*)log.1$/i) { #print "Found $file\n"; push @logfiles,$file; } } #print "\nReading in data...\n "; print "Customer,Server,Date,Code,Count,Bytes\n"; foreach $log(@logfiles) { #get customers codename # print "Working on $log\n"; $log =~ /access_(.*).log.*/; $customer = $1; # print "Customer is $customer\n"; #stuff to skip #if ($log =~ /gz/) # { # open LOGFILE,"/bin/zcat /var/log/apache2/proxies/$log|"; #print "$log - logfile is gzipped..\n"; # } #else # { open LOGFILE,"/var/log/apache2/proxies/$log"; #print "$log - logfile is text...\n"; # } %codecount = (); %bytecount = (); while () { if ($_ =~ /^10.10.10.*|^10.9.77.254.*|^10.9.77.244.*|^rock.verio.*/) { #print "!"; } # now handle the real data else { #print "."; $_ =~ /(.*) - - \[(.*)\] \"(.*)\" (.*) (.*) \"(.*)\" \"(.*)\"/; $host = $1; $date = $2; $url = $3; $code = $4; $size = $5; if ($size == "-") { $size = 0; } $totalsize += $size; ($day,$month,$year) = split(/\//,$date); if ($month =~ /Jan/) { $month = 1;} if ($month =~ /Feb/) { $month = 2;} if ($month =~ /Mar/) { $month = 3;} if ($month =~ /Apr/) { $month = 4;} if ($month =~ /May/) { $month = 5;} if ($month =~ /Jun/) { $month = 6;} if ($month =~ /Jul/) { $month = 7;} if ($month =~ /Aug/) { $month = 8;} if ($month =~ /Sep/) { $month = 9;} if ($month =~ /Oct/) { $month = 10;} if ($month =~ /Nov/) { $month = 11;} if ($month =~ /Dec/) { $month = 12;} ($realyear) = split(/:/,$year); $fulldate = "$realyear-$month-$day"; $codecount{"$fulldate, $code"} ++; $bytecount{"$fulldate, $code"} += $size; } } #print "\nDone with $log, crunching numbers now.. \n"; #print "\nNow Parsing Data.. \n"; foreach $key (keys %codecount) { ($fulldate,$code) = split(/,/,$key); # $totalbytestransferred += $bytecount{$key}; # print "$customer,$server,$fulldate,$code,$codecount{$key},$bytecount{$key}\n"; $query = "insert into data (date,server,customer,resultcode,codecount,size) values ('$fulldate','$server','$customer','$code','$codecount{$key}','$bytecount{$key}')"; $dbh->query($query); $bytecount{$key}=0; } #print "$customer transferred $totalbytestransferred inside $log\n"; #$totalbytestransferred = 0; undef $totalbytestransferred; undef $totalsize; undef $size; undef $bytecount; undef $size; $totalsize = 0; $size = 0; $bytecount = 0; $size = 0; $fulldate=0; }