#!/usr/bin/perl use Date::Manip; # for gathering stats on a gentoo rsync server.. # version 0.01 - feb 15 2006 # Travis Morgan, travis@bigfiber.net if (@ARGV != 1) { print "Usage: ".$0." logfile\n"; exit; } $logfile = $ARGV[0]; &Date_Init("Internal=1"); $startdate = 99999999999999; $enddate = 0; $totcon = 0; $totbw = 0; $tz = Date_TimeZone; $numxfer = 20; $numhits = 20; $numspeed = 20; unless (open LOG, $logfile) { die("Could not open log file $logfile"); } @log = ; close LOG; foreach $line (@log) { chomp $line; @line = split / /, $line; $pid = $line[2]; $pid =~ s/\[(.*)\]/\1/; $datetime = $line[0].$line[1]; $datetime =~ s/\///g; $datetime =~ s/://g; # start of a sync if ($line =~ / \(([0-9]+\.){3,3}[0-9]+\)$/) { # figure out the "hostname ip" hostid $hostid = $line[$#line - 1]." ".$line[$#line]; # save a sync for $hostid at the time for the dir synced $hosts{$hostid}{$datetime}{'dir'} = $line[6]; # set the pid to this hostid $pids{$pid}{'hostid'} = $hostid; $pids{$pid}{'datetime'} = $datetime; # end of a sync } elsif ($line =~ /] sent /) { # date will be in UTC - adjust $datetime = Date_ConvTZ(ParseDate($datetime),"UTC",$tz); if ($pids{$pid}) { $hosts{ $pids{$pid}{'hostid'} }{ $pids{$pid}{'datetime'} }{'sent'} = $line[4]; $hosts{ $pids{$pid}{'hostid'} }{ $pids{$pid}{'datetime'} }{'rcvd'} = $line[8]; $hosts{ $pids{$pid}{'hostid'} }{ $pids{$pid}{'datetime'} }{'finish'} = $datetime; delete($pids{$pid}); } # other info like errors, etc } else { } # find the start date time if ($startdate > $datetime) { $startdate = $datetime; } # find the end date and time if ($enddate < $datetime) { $enddate = $datetime; } } foreach $hostid ( sort keys %hosts ) { foreach $date ( keys %{$hosts{$hostid}} ) { $totcon++; if ($hosts{$hostid}{$date}{'sent'}) { $transfer = $hosts{$hostid}{$date}{'sent'} + $hosts{$hostid}{$date}{'rcvd'}; $totbw += $transfer; if ($hoststat{$hostid}) { $hoststat{$hostid}{'xfer'} = $hoststat{$hostid}{'xfer'} + $transfer; $hoststat{$hostid}{'num'} = $hoststat{$hostid}{'num'} + 1; $hoststat{$hostid}{'time'} = $hoststat{$hostid}{'time'} + &UnixDate($hosts{$hostid}{$date}{'finish'},"%s") - &UnixDate($date,"%s"); } else { $hoststat{$hostid}{'xfer'} = $transfer; $hostnum{$hostid}{'num'} = 1; $hoststat{$hostid}{'time'} = &UnixDate($hosts{$hostid}{$date}{'finish'},"%s") - &UnixDate($date,"%s"); } } } } $logstart = &UnixDate($startdate,"%s"); $logend = &UnixDate($enddate,"%s"); $logtime = ($logend - $logstart)/60/60/24; printf "Log spans %.2f days starting at %s and ending at %s.\n",$logtime,&UnixDate($startdate,"%c"),&UnixDate($enddate,"%c"); print "Total connections during this time: $totcon\n"; printf "Total bandwidth during this time: %.3f gigabytes\n",$totbw/1024/1024/1024; print "\n\n"; print "TOP HOSTS BY BANDWIDTH USE\n"; print "==========================\n"; printf "%4s %11s %s\n","Num","Transfer MB","Host"; printf "%4s %11s %s\n","---","-----------","----"; $num = 1; foreach $hostid ( sort hashDescendingXfer (keys (%hoststat))) { if ($num <= $numxfer) { printf "%4d %11.2f %s\n",$num,$hoststat{$hostid}{'xfer'}/1024/1024,$hostid; } $num++; } print "\n\n"; print "TOP HOSTS BY NUMBER OF SYNCS\n"; print "============================\n"; printf "%4s %11s %s\n","Num","Connections","Host"; printf "%4s %11s %s\n","---","-----------","----"; $num = 1; foreach $hostid ( sort hashDescendingHits (keys (%hoststat))) { if ($num <= $numxfer) { printf "%4d %11d %s\n",$num,$hoststat{$hostid}{'num'},$hostid; } $num++; } print "\n\n"; foreach $hostid ( keys (%hoststat)) { if ($hoststat{$hostid}{'time'} == 0) { $hoststat{$hostid}{'speed'} = 0; } else { $hoststat{$hostid}{'speed'} = $hoststat{$hostid}{'xfer'} / 1024 / $hoststat{$hostid}{'time'}; } } print "TOP HOSTS BY TRANSFER SPEED\n"; print "============================\n"; printf "%4s %11s %s\n","Num","Speed kB/s","Host"; printf "%4s %11s %s\n","---","----------","----"; $num = 1; foreach $hostid ( sort hashDescendingSpeed (keys (%hoststat))) { if ($num <= $numspeed) { # the timing info isn't very accurate so use only hosts that transferred over 2 megs if ($hoststat{$hostid}{'xfer'} > (2*1024*1024)) { printf "%4d %11.2f %s\n",$num,$hoststat{$hostid}{'speed'},$hostid; $num++; } } } print "\n\n"; sub hashDescendingXfer { $hoststat{$b}{'xfer'} <=> $hoststat{$a}{'xfer'}; } sub hashDescendingHits { $hoststat{$b}{'num'} <=> $hoststat{$a}{'num'}; } sub hashDescendingSpeed { $hoststat{$b}{'speed'} <=> $hoststat{$a}{'speed'}; }