home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / mail / listserv / utils / server-stats.pl.Z / server-stats.pl
Encoding:
Perl Script  |  1993-10-19  |  9.0 KB  |  320 lines

  1. #! /usr/local/bin/perl
  2. # ---------------------------------------------------------------------------
  3. #
  4. # USAGE: serverstats <options>
  5. #
  6. # OPTIONS:
  7. #       -f <filename>   Use <filename> for the log file
  8. #       -r              include real users 
  9. #       -a              include anonymous users 
  10. #       -h        include report on hourly traffic
  11. #       -d        include report on domain traffic
  12. #       -t        report on total traffic by section
  13. #       -D <domain>     report only on traffic from <domain>
  14. #       -l <depth>      Depth of path detail for sections
  15. #       -s <section>    Section to report on, For example: -s /pub will report
  16. #                only on paths under /pub
  17. #
  18. # ---------------------------------------------------------------------------
  19. # Author: Bob Funchess <bobf@MSI.COM>
  20.  
  21. # edit the next two lines to customize for your domain.
  22. # This will allow your domain to be seperated in the domain listing.
  23.  
  24. $mydom1 = "msi";
  25. $mydom2 = "com";
  26.  
  27. # edit the next line to customize for your default log file
  28. $usage_file = "/usr/people/ftp/archives/private/xferlog";
  29.  
  30. # Edit the following lines for default report settings.
  31. # Entries defined here will be over-ridden by the command line.
  32.  
  33. $opt_h = 0; 
  34. $opt_d = 0;
  35. $opt_t = 0;
  36. $opt_l = 3;
  37.  
  38. require 'getopts.pl';
  39. &Getopts('f:rahtdD:l:s:');
  40.  
  41. if ($opt_r) { $real = 1;}
  42. if ($opt_a) { $anon = 1;}
  43. if ($real == 0 && $anon == 0) { $anon = 1; }
  44. if ($opt_f) {$usage_file = $opt_f;}
  45.  
  46. open (LOG,$usage_file) || die "Error opening usage log file: $usage_file\n";
  47.  
  48. if ($opt_D) {print "Transfer Totals include the '$opt_D' domain only.\n";
  49.          print "All other domains are filtered out for this report.\n\n";}
  50.  
  51. if ($opt_s) {print "Transfer Totals include the '$opt_s' section only.\n";
  52.          print "All other sections are filtered out for this report.\n\n";}
  53.  
  54. line: while (<LOG>) {
  55.  
  56.    @line = split;
  57.    next if ($#line != 16);
  58.    next if (!$anon && $line[12] eq "a");
  59.    next if (!$real && $line[12] eq "r");
  60.  
  61.    $daytime = substr($_, 0, 10) . substr($_, 19, 5);
  62.    $time = substr($_,11,2); 
  63.  
  64.    if ($line[8] eq "\.") { $line[8] = "/unreadable/filename";}
  65.    next if (substr($line[8],0,length("$opt_s")) ne "$opt_s");
  66.    $line[8] = substr($line[8],length("$opt_s"));
  67.    @path = split(/\//, $line[8]);
  68.  
  69. #
  70. # Why was the origional xferstats dropping leading 1 character path
  71. # segments???
  72. #
  73. #  while (length($path[1]) <= 1) {
  74. #     shift @path;
  75. #     next line if ($#path == -1);
  76. #  }
  77.  
  78. # Get rid of "/usr/people/server/archives" in the path
  79.  
  80.    shift @path;
  81.    shift @path;
  82.    shift @path;
  83.    shift @path;
  84.  
  85. # Things in the top-level directory are assumed to be informational files
  86.  
  87.    if ($#path == 1)
  88.       { $pathkey = "Index/Informational Files"; }
  89.       else {
  90.     $pathkey = "";
  91.     for ($i=1; $i <= $#path-1 && $i <= $opt_l;$i++) {
  92.         $pathkey = $pathkey . "/" . $path[$i];
  93.         }
  94.     }
  95.  
  96.    $line[6] =~ tr/A-Z/a-z/;
  97.  
  98.    @address = split(/\./, $line[6]);
  99.  
  100.    $domain = $address[$#address];
  101.  
  102.    if ($domain eq "$mydom2" && $address[$#address-1] eq "$mydom1")
  103.       { $domain = $mydom1 . "." . $mydom2; }
  104.    if ( int($address[0]) > 0 || $#address < 1 )
  105.       { $domain = "unresolved"; }
  106.  
  107.    $count = 1;
  108.    if ($opt_D)
  109.      {if (substr($domain,0,length("$opt_D")) eq "$opt_D" ) { $count = 1;} else
  110.      {$count = 0;}
  111.      }
  112.  
  113.  
  114.    if ($count) {
  115.  
  116.    $xferfiles++;                                # total files sent
  117.    $xfertfiles++;                               # total files sent
  118.    $xferfiles{$daytime}++;                      # files per day
  119.    $groupfiles{$pathkey}++;                     # per-group accesses
  120.    $domainfiles{$domain}++;
  121.  
  122.    $xfersecs{$daytime}    += $line[5];          # xmit seconds per day
  123.    $domainsecs{$domain}   += $line[5];        # xmit seconds for domain
  124.    $xferbytes{$daytime}   += $line[7];          # bytes per day
  125.    $domainbytes{$domain}  += $line[7];        # xmit bytes to domain
  126.    $xferbytes             += $line[7];          # total bytes sent
  127.    $groupbytes{$pathkey}  += $line[7];          # per-group bytes sent
  128.  
  129.    $xfertfiles{$time}++;                        # files per hour
  130.    $xfertsecs{$time}      += $line[5];          # xmit seconds per hour
  131.    $xfertbytes{$time}     += $line[7];          # bytes per hour
  132.    $xfertbytes            += $line[7];          # total bytes sent
  133.    }
  134. }
  135. close LOG;
  136.  
  137. @syslist = keys(systemfiles);
  138. @dates = sort datecompare keys(xferbytes);
  139.  
  140. if ($xferfiles == 0) {die "There was no data to process.\n";}
  141.  
  142.  
  143. print "TOTALS FOR SUMMARY PERIOD ", $dates[0], " TO ", $dates[$#dates], "\n\n";
  144. printf ("Files Transmitted During Summary Period  %12.0f\n", $xferfiles);
  145. printf ("Bytes Transmitted During Summary Period  %12.0f\n", $xferbytes); 
  146. printf ("Systems Using Archives                   %12.0f\n\n", $#syslist+1);
  147.  
  148. printf ("Average Files Transmitted Daily          %12.0f\n",
  149.    $xferfiles / ($#dates + 1));
  150. printf ("Average Bytes Transmitted Daily          %12.0f\n",
  151.    $xferbytes / ($#dates + 1));
  152.  
  153. format top1 =
  154.  
  155. Daily Transmission Statistics
  156.  
  157.                  Number Of    Number of    Average    Percent Of  Percent Of
  158.      Date        Files Sent  Bytes  Sent  File  Size  Files Sent  Bytes Sent
  159. ---------------  ----------  -----------  ----------  ----------  ----------
  160. .
  161.  
  162. format line1 =
  163. @<<<<<<<<<<<<<<  @>>>>>>>>>  @>>>>>>>>>>  @>>>>>>>>>  @>>>>>>>    @>>>>>>>  
  164. $date,           $nfiles,    $nbytes,     $avgrate,   $pctfiles,  $pctbytes
  165. .
  166.  
  167. $^ = top1;
  168. $~ = line1;
  169.  
  170. foreach $date ( sort datecompare keys(xferbytes) ) {
  171.  
  172.    $nfiles   = $xferfiles{$date};
  173.    $nbytes   = $xferbytes{$date};
  174.    $avgrate  = sprintf("%5.1f KB", $xferbytes{$date}/$xfersecs{$date}/1000);
  175.    $pctfiles = sprintf("%8.2f", 100*$xferfiles{$date} / $xferfiles);
  176.    $pctbytes = sprintf("%8.2f", 100*$xferbytes{$date} / $xferbytes);
  177.    write;
  178. }
  179.  
  180. if ($opt_t) {
  181. format top2 =
  182.  
  183. Total Transfers from each Archive Section (By bytes)
  184.  
  185.                                                  ---- Percent  Of ----
  186.      Archive Section      Files Sent Bytes Sent  Files Sent Bytes Sent
  187. ------------------------- ---------- ----------- ---------- ----------
  188. .
  189.  
  190. format line2 =
  191. @<<<<<<<<<<<<<<<<<<<<<<<< @>>>>>>>>> @>>>>>>>>>> @>>>>>>>   @>>>>>>>
  192. $section,                 $files,    $bytes,     $pctfiles, $pctbytes
  193. .
  194.  
  195. $| = 1;
  196. $- = 0;
  197. $^ = top2;
  198. $~ = line2;
  199.  
  200. foreach $section ( sort bytecompare keys(groupfiles) ) {
  201.  
  202.    $files = $groupfiles{$section};
  203.    $bytes = $groupbytes{$section};
  204.    $pctbytes = sprintf("%8.2f", 100 * $groupbytes{$section} / $xferbytes);
  205.    $pctfiles = sprintf("%8.2f", 100 * $groupfiles{$section} / $xferfiles);
  206.    write;
  207.  
  208. }
  209.  
  210. if ( $xferfiles < 1 ) { $xferfiles = 1; }
  211. if ( $xferbytes < 1 ) { $xferbytes = 1; }
  212. }
  213.  
  214. if ($opt_d) {
  215. format top3 =
  216.  
  217. Total Transfer Amount By Domain
  218.  
  219.              Number Of    Number of     Average    Percent Of  Percent Of
  220. Domain Name  Files Sent   Bytes Sent   File  Size  Files Sent  Bytes Sent
  221. -----------  ----------  ------------  ----------  ----------  ----------
  222. .
  223.  
  224. format line3 =
  225. @<<<<<<<<<<  @>>>>>>>>>  @>>>>>>>>>>>  @>>>>>>>>>  @>>>>>>>    @>>>>>>>  
  226. $domain,     $files,     $bytes,       $avgrate,   $pctfiles,  $pctbytes
  227. .
  228.  
  229. $- = 0;
  230. $^ = top3;
  231. $~ = line3;
  232.  
  233. foreach $domain ( sort domnamcompare keys(domainfiles) ) {
  234.  
  235.    if ( $domainsecs{$domain} < 1 ) { $domainsecs{$domain} = 1; }
  236.  
  237.    $files = $domainfiles{$domain};
  238.    $bytes = $domainbytes{$domain};
  239.    $avgrate  = sprintf("%5.1f KB",
  240.                   $domainbytes{$domain}/$domainsecs{$domain}/1000);
  241.    $pctfiles = sprintf("%8.2f", 100 * $domainfiles{$domain} / $xferfiles);
  242.    $pctbytes = sprintf("%8.2f", 100 * $domainbytes{$domain} / $xferbytes);
  243.    write;
  244.  
  245. }
  246.  
  247. print "\n";
  248.  
  249. }
  250.  
  251. if ($opt_h) {
  252.  
  253. format top8 =
  254.  
  255. Hourly Transmission Statistics
  256.  
  257.                  Number Of    Number of    Average    Percent Of  Percent Of
  258.      Time        Files Sent  Bytes  Sent  File  Size  Files Sent  Bytes Sent
  259. ---------------  ----------  -----------  ----------  ----------  ----------
  260. .
  261.  
  262. format line8 =
  263. @<<<<<<<<<<<<<<  @>>>>>>>>>  @>>>>>>>>>>  @>>>>>>>>>  @>>>>>>>    @>>>>>>>  
  264. $time,           $nfiles,    $nbytes,     $avgrate,   $pctfiles,  $pctbytes
  265. .
  266.  
  267.  
  268. $| = 1;
  269. $- = 0;
  270. $^ = top8;
  271. $~ = line8;
  272.  
  273. foreach $time ( sort keys(xfertbytes) ) {
  274.  
  275.    $nfiles   = $xfertfiles{$time};
  276.    $nbytes   = $xfertbytes{$time};
  277.    $avgrate  = sprintf("%5.1f KB", $xfertbytes{$time}/$xfertsecs{$time}/1000);
  278.    $pctfiles = sprintf("%8.2f", 100*$xfertfiles{$time} / $xferfiles);
  279.    $pctbytes = sprintf("%8.2f", 100*$xfertbytes{$time} / $xferbytes);
  280.    write;
  281. }
  282. }
  283. exit(0);
  284.  
  285. sub datecompare {
  286.  
  287.    $date1  = substr($a, 11, 4) * 4800;
  288.    $date2  = substr($b, 11, 4) * 4800;
  289.    $date1 += index("JanFebMarAprMayJunJulAugSepOctNovDec",substr($a, 4, 3))*400;
  290.    $date2 += index("JanFebMarAprMayJunJulAugSepOctNovDec",substr($b, 4, 3))*400;
  291.    $date1 += substr($a, 8, 2);
  292.    $date2 += substr($b, 8, 2);
  293.    $date1 - $date2;
  294.  
  295. }
  296.  
  297. sub domnamcompare {
  298.  
  299.    $sdiff = length($a) - length($b);
  300.    ($sdiff < 0) ? -1 : ($sdiff > 0) ? 1 : ($a lt $b) ? -1 : ($a gt $b) ? 1 : 0;
  301.  
  302. }
  303.  
  304. sub bytecompare {
  305.  
  306.    $bdiff = $groupbytes{$b} - $groupbytes{$a};
  307.    ($bdiff < 0) ? -1 : ($bdiff > 0) ? 1 : ($a lt $b) ? -1 : ($a gt $b) ? 1 : 0;
  308.  
  309. }
  310.  
  311. sub faccompare {
  312.  
  313.    $fdiff = $fac{$b} - $fac{$a};
  314.    ($fdiff < 0) ? -1 : ($fdiff > 0) ? 1 : ($a lt $b) ? -1 : ($a gt $b) ? 1 : 0;
  315.  
  316. }
  317.  
  318.  
  319.  
  320.