<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/env perl

#
# rsynclogparse-extended.pl, version 1.0
# Script for producing daily or hourly stats from an rsync.log, in
# plain text or XML output formats
#
# (C) Tim Haynes &lt;gentoo@stirfried.vegetable.org.uk&gt;, February 2003
# Redistributable under the terms of the BSD licence
# &lt;http://www.opensource.org/licenses/bsd-license.php&gt;
#

$|=1;

#Determine whether we have a commandline option or not
$arg="";
$arg=shift
  if $ARGV[0]=~/^-/;

#Hash of variables to be output and descriptions
%outputVars=(
	     "mirrorid" =&gt; "which mirror name this box is",
	     "contact" =&gt; "email address to contact the server administrator",
	     "read" =&gt; "total bytes read",
	     "wrote" =&gt; "total bytes served",
	     "total" =&gt; "total bytes both directions",
	     "count" =&gt; "number of connections",
	     "meanxfer" =&gt; "mean transfer size",
	     "biggestXfer" =&gt; "biggest individual transfer",
	     "speedupavg" =&gt; "mean speedup-a-like ratio for all conns",
	     "avgbandwidth" =&gt; "mean bandwith reequirement over 1d",
	     "interval" =&gt; "most recent n-seconds' worth of data",
	     "maxconns" =&gt; "number of times max-conns reached",
	     "percmaxconns" =&gt; "percentage of connections rejected",
	     "configmaxconns" =&gt; "max concurrent connections configured",
	     "timestamp" =&gt; "Current time these stats were generated"
	    );

#Initialise all the above to 0
map { $$_ =0 ; } keys %outputVars;

#Set fields for this specific server
$mirrorid="rsync1.uk.gentoo.org";
$contact="gentoo\@stirfried.vegetable.org.uk";
$configmaxconns=5;

$timestamp=time();

#Determine if we're doing a daily or hourly thing
$interval=3600;
$interval=2600*24 
  if $arg=~/d/;

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime(time);
$now=dateToTstamp(sprintf("%04d/%02d/%02d %02d:%02d:%02d",
			  $year+1900, $mon+1, $mday, $hour, $min, $sec));


#Read in all remaining files on commandline and stdin
while (&lt;&gt;) {
  chomp;

  if (m#^((\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+))#) {  
      $tstamp=dateToTstamp($1);
    }

    next			# skip too-old log entries
      if $tstamp &lt; ($now-$interval);

  $maxconns++
    if /max connections .\d+. reached/oi;

  /wrote (\d+) bytes.*read (\d+) bytes.*size (\d+)/oi
    or next;

  $wrote+=$1;			#running total of outgoing
  $read+=$2;			#running total of incoming
  $volumesize=$3;        	#total size of the volume, serversize
  $localtotal=$1 + $2;
  $speedupsum+=$volumesize/$localtotal;	#running total of "speedup" ratios
  $count++;
  $biggestXfer=($localtotal&gt;$biggestXfer)?$localtotal:$biggestXfer;
}

#Compute a few things
$total=$read+$wrote;
$speedupavg=$speedupsum/$count;	#average speedup ratio
$meanxfer=$total/$count;	#mean-size xfer per connection
$avgbandwidth=$total/$interval; #mean bandwith consumed over this interval
$percmaxconns=100*$maxconns/($count+$maxconns);

#Choice of output format
$arg =~/xml/ ? &amp;outputXML : &amp;outputText;

1;

################

sub outputText {
  foreach $i ( keys %outputVars ) {
    printf("%-20s: $$i\n", $i);
  }
}


sub outputXML {
  print "&lt;xml&gt;\n  &lt;rsyncstats&gt;\n";
  foreach $i ( keys %outputVars ) {
    if ($arg=~/v/o) {
      print "    &lt;$i desc=\"$outputVars{$i}\"&gt;$$i&lt;/$i&gt;\n";
    } else {
      print "    &lt;$i&gt;$$i&lt;/$i&gt;\n";
    }
  }
  print "  &lt;/rsyncstats&gt;\n&lt;/xml&gt;\n";
}


sub dateToTstamp {
  my $str=shift;

  $str =~ m#^(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+)#;  

  $tstamp=$6 + 60*$5 + 3600*$4 + 3600*24*$3 +
    3600*24*31*$2 + 3600*24*365*($1-1975);

  return $tstamp;
}
</pre></body></html>