rsynclogparse-extended.pl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env perl
  2. #
  3. # rsynclogparse-extended.pl, version 1.0
  4. # Script for producing daily or hourly stats from an rsync.log, in
  5. # plain text or XML output formats
  6. #
  7. # (C) Tim Haynes <gentoo@stirfried.vegetable.org.uk>, February 2003
  8. # Redistributable under the terms of the BSD licence
  9. # <http://www.opensource.org/licenses/bsd-license.php>
  10. #
  11. $|=1;
  12. #Determine whether we have a commandline option or not
  13. $arg="";
  14. $arg=shift
  15. if $ARGV[0]=~/^-/;
  16. #Hash of variables to be output and descriptions
  17. %outputVars=(
  18. "mirrorid" => "which mirror name this box is",
  19. "contact" => "email address to contact the server administrator",
  20. "read" => "total bytes read",
  21. "wrote" => "total bytes served",
  22. "total" => "total bytes both directions",
  23. "count" => "number of connections",
  24. "meanxfer" => "mean transfer size",
  25. "biggestXfer" => "biggest individual transfer",
  26. "speedupavg" => "mean speedup-a-like ratio for all conns",
  27. "avgbandwidth" => "mean bandwith reequirement over 1d",
  28. "interval" => "most recent n-seconds' worth of data",
  29. "maxconns" => "number of times max-conns reached",
  30. "percmaxconns" => "percentage of connections rejected",
  31. "configmaxconns" => "max concurrent connections configured",
  32. "timestamp" => "Current time these stats were generated"
  33. );
  34. #Initialise all the above to 0
  35. map { $$_ =0 ; } keys %outputVars;
  36. #Set fields for this specific server
  37. $mirrorid="rsync1.uk.gentoo.org";
  38. $contact="gentoo\@stirfried.vegetable.org.uk";
  39. $configmaxconns=5;
  40. $timestamp=time();
  41. #Determine if we're doing a daily or hourly thing
  42. $interval=3600;
  43. $interval=2600*24
  44. if $arg=~/d/;
  45. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime(time);
  46. $now=dateToTstamp(sprintf("%04d/%02d/%02d %02d:%02d:%02d",
  47. $year+1900, $mon+1, $mday, $hour, $min, $sec));
  48. #Read in all remaining files on commandline and stdin
  49. while (<>) {
  50. chomp;
  51. if (m#^((\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+))#) {
  52. $tstamp=dateToTstamp($1);
  53. }
  54. next # skip too-old log entries
  55. if $tstamp < ($now-$interval);
  56. $maxconns++
  57. if /max connections .\d+. reached/oi;
  58. /wrote (\d+) bytes.*read (\d+) bytes.*size (\d+)/oi
  59. or next;
  60. $wrote+=$1; #running total of outgoing
  61. $read+=$2; #running total of incoming
  62. $volumesize=$3; #total size of the volume, serversize
  63. $localtotal=$1 + $2;
  64. $speedupsum+=$volumesize/$localtotal; #running total of "speedup" ratios
  65. $count++;
  66. $biggestXfer=($localtotal>$biggestXfer)?$localtotal:$biggestXfer;
  67. }
  68. #Compute a few things
  69. $total=$read+$wrote;
  70. $speedupavg=$speedupsum/$count; #average speedup ratio
  71. $meanxfer=$total/$count; #mean-size xfer per connection
  72. $avgbandwidth=$total/$interval; #mean bandwith consumed over this interval
  73. $percmaxconns=100*$maxconns/($count+$maxconns);
  74. #Choice of output format
  75. $arg =~/xml/ ? &outputXML : &outputText;
  76. 1;
  77. ################
  78. sub outputText {
  79. foreach $i ( keys %outputVars ) {
  80. printf("%-20s: $$i\n", $i);
  81. }
  82. }
  83. sub outputXML {
  84. print "<xml>\n <rsyncstats>\n";
  85. foreach $i ( keys %outputVars ) {
  86. if ($arg=~/v/o) {
  87. print " <$i desc=\"$outputVars{$i}\">$$i</$i>\n";
  88. } else {
  89. print " <$i>$$i</$i>\n";
  90. }
  91. }
  92. print " </rsyncstats>\n</xml>\n";
  93. }
  94. sub dateToTstamp {
  95. my $str=shift;
  96. $str =~ m#^(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+)#;
  97. $tstamp=$6 + 60*$5 + 3600*$4 + 3600*24*$3 +
  98. 3600*24*31*$2 + 3600*24*365*($1-1975);
  99. return $tstamp;
  100. }