home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2 / Openstep-4.2-Intel-User.iso / NextLibrary / PrivateFrameworks / Uucp.framework / Versions / A / Resources / contrib / uutraf < prev    next >
Text File  |  1995-06-04  |  6KB  |  211 lines

  1. #!/usr/bin/perl
  2. # uutraf.pl -- UUCP Traffic Analyzer
  3. # SCCS Status     : @(#)@ uutraf    1.8
  4. # Author          : Johan Vromans
  5. # Created On      : ***
  6. # Last Modified By: Johan Vromans
  7. # Last Modified On: Mon Aug 30 15:02:22 1993
  8. # Update Count    : 6
  9. # Status          : OK
  10. # Requires:       : Perl V4 or later
  11.  
  12. # Reads UUCP syslog, and generates a report from it.
  13. #
  14. # Created by Johan Vromans <jv@mh.nl>
  15. # Loosely based on an idea by Greg Hackney (hack@texbell.swbt.com)
  16.  
  17. # Usage: uutraf [-taylor|-hdb|-bnu|-bsd] [syslog]
  18.  
  19. # Logfile formats:
  20. #
  21. # BSD:
  22. #
  23. # jv mhres (2/23-5:18) (698818735) received 135 b 2 secs
  24. # root mhres (2/23-5:19) (698818742) sent 2365 b 3 secs, Pk: 38, Rxmt: 0
  25. #
  26. # HDB:
  27. #
  28. # uunet!uucp M (12/10-09:04:22) (C,16390,1) [ttyXX] <- 2371 / 5.000 secs, \
  29. #     474 bytes/sec
  30. #
  31. # Taylor:
  32. #
  33. # jv mhres (1992-02-24 20:49:04.06) sent 16234 bytes in 148.780 seconds \
  34. #    (109 bytes/sec)
  35. # jv mhres (1992-02-24 21:04:05.76) received 449 bytes in 6.550 seconds \
  36. #    (68 bytes/sec)
  37.  
  38. $uucp_type = "gnu";
  39.  
  40. %hosts = ();        # hosts seen
  41. %bytes_in = ();        # of bytes received from host
  42. %bytes_out = ();    # of bytes sent to host
  43. %secs_in = ();        # of seconds connect for recving
  44. %secs_out = ();        # of seconds connect for sending
  45. %files_in = ();        # of input requests
  46. %files_out = ();    # of output requests
  47.  
  48. # read info, break the lines and tally
  49.  
  50. if ( $ARGV[0] =~ /^-/ ) {
  51.     ($uucp_type = substr (shift (@ARGV), 1)) =~ tr/A-Z/a-z/;
  52. }
  53.  
  54. if ( $uucp_type eq "taylor" || $uucp_type eq "gnu" ) {
  55.     @ARGV = ("/usr/local/spool/uucp/Stats") unless $#ARGV >= 0;
  56.     $pat = "^[^ ]+ ([^ ]+) \\(([-0-9:\\/ .]+)\\) " .
  57.     "(sent|received) (\\d+) bytes in (\\d+)\\.(\\d+) seconds";
  58.     $uucp_type = 0;
  59.     $recv = "received";
  60. }
  61. elsif ( $uucp_type eq "hdb" || $uucp_type eq "bnu" ) {
  62.     @ARGV = ("/usr/spool/uucp/.Admin/xferstats") unless $#ARGV >= 0;
  63.     $pat = "^([^!]+)![^(]+\\(([-0-9:\\/]+)\\).+([<>])-? " .
  64.     "(\\d+) \\/ (\\d+)\\.(\\d+) secs";
  65.     $uucp_type = 1;
  66.     $recv = "<";
  67. }
  68. elsif ( $uucp_type eq "bsd" || $uucp_type eq "v7" ) {
  69.     @ARGV = ("/usr/spool/uucp/SYSLOG") unless $#ARGV >= 0;
  70.     $pat = "^[^ ]+ ([^ ]+) \\(([-0-9:\\/]+)\\) \\([^)]+\\) " .
  71.     "(sent|received) (\\d+) b (\\d+) secs";
  72.     $uucp_type = 2;
  73.     $recv = "received";
  74. }
  75. else {
  76.     die ("FATAL: Unknown UUCP type: $uucp_type\n");
  77. }
  78.  
  79. $garbage = 0;
  80.  
  81. while ( <> ) {
  82.     unless ( /$pat/o ) {
  83.     print STDERR "$_";
  84.     next if /failed/;
  85.     if ( $garbage++ > 10 ) {
  86.         die ("FATAL: Too much garbage; wrong UUCP type?\n");
  87.     }
  88.     next;
  89.     }
  90.  
  91.     # gather timestamps
  92.     $last_date = $2;
  93.     $first_date = $last_date unless defined $first_date;
  94.     
  95.     # initialize new hosts
  96.     unless ( defined $hosts{$1} ) {
  97.     $hosts{$1} = $files_in{$1} = $files_out{$1} = 
  98.         $bytes_in{$1} = $bytes_out{$1} =
  99.         $secs_in{$1} = $secs_out{$1} = 0;
  100.     }
  101.  
  102.     # Taylor and HDB have milliseconds, BSD has not.
  103.     $secs = ($uucp_type == 2) ? ($5 + ($5 == 0 ? 0.5 : 0)) : ($5 + $6/1000);
  104.  
  105.     # tally
  106.     if ( $3 eq $recv ) {        # recv
  107.     $bytes_in{$1} += $4;
  108.     $files_in{$1}++;
  109.     $secs_in{$1} += $secs;
  110.     }
  111.     else {            # xmit
  112.     $bytes_out{$1} += $4;
  113.     $files_out{$1}++;
  114.     $secs_out{$1} += $secs;
  115.     }
  116.     $garbage = 0;
  117. }
  118.  
  119. @hosts = keys (%hosts);
  120. die ("No info found, stopped\n") if $#hosts < 0;
  121.  
  122. ################ report section ################
  123.  
  124. $thishost = &gethostname();
  125. $thishost = (defined $thishost) ? "on node $thishost" : "report";
  126.  
  127. if ( $uucp_type eq 0 ) {    # Taylor UUCP
  128.     substr ($first_date, 16) = "";
  129.     substr ($last_date, 16) = "";
  130. }
  131.  
  132. format std_head =
  133. @|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  134. "UUCP traffic $thishost from $first_date to $last_date"
  135.  
  136. Remote   -----------K-Bytes----------- ----Hours---- --Avg CPS-- --Files--
  137.  Host         Recv      Sent     Total   Recv   Sent  Recv  Sent Recv Sent
  138. .
  139. format std_out =
  140. @<<<<<<< @>>>>>>>> @>>>>>>>> @>>>>>>>> @>>>>> @>>>>> @>>>> @>>>> @>>> @>>>
  141. $Zhost,   $Zi_bytes, $Zo_bytes, $Zt_bytes, $Zi_hrs, $Zo_hrs, $Zi_acps, $Zo_acps, $Zi_count, $Zo_count
  142. .
  143.  
  144. $^ = "std_head";
  145. $~ = "std_out";
  146.  
  147. &print_dashes ();
  148.  
  149. reset "T";           # reset totals
  150.  
  151. foreach $host (@hosts) {
  152.   &print_line ($host, $bytes_in{$host}, $bytes_out{$host},
  153.          $secs_in{$host},  $secs_out{$host},
  154.          $files_in{$host}, $files_out{$host});
  155.  
  156. }
  157.  
  158. &print_dashes ();
  159. &print_line ("Total", $Ti_bytes, $To_bytes,
  160.            $Ti_secs, $To_secs, $Ti_count, $To_count);
  161.  
  162. ################ that's it ################
  163.  
  164. sub print_line {
  165.   reset "Z";        # reset print fields
  166.   local ($Zhost, 
  167.      $Zi_bytes, $Zo_bytes, 
  168.      $Zi_secs, $Zo_secs, 
  169.      $Zi_count, $Zo_count) = @_;
  170.   $Ti_bytes += $Zi_bytes;
  171.   $To_bytes += $Zo_bytes;
  172.   $Zt_bytes = $Zi_bytes + $Zo_bytes;
  173.   $Tt_bytes += $Zt_bytes;
  174.   $Zi_acps = ($Zi_secs > 0) ? sprintf ("%.0f", $Zi_bytes/$Zi_secs) : "0";
  175.   $Zo_acps = ($Zo_secs > 0) ? sprintf ("%.0f", $Zo_bytes/$Zo_secs) : "0";
  176.   $Zi_bytes = sprintf ("%.1f", $Zi_bytes/1000);
  177.   $Zo_bytes = sprintf ("%.1f", $Zo_bytes/1000);
  178.   $Zt_bytes = sprintf ("%.1f", $Zt_bytes/1000);
  179.   $Zi_hrs = sprintf ("%.1f", $Zi_secs/3600);
  180.   $Zo_hrs = sprintf ("%.1f", $Zo_secs/3600);
  181.   $Ti_secs += $Zi_secs;
  182.   $To_secs += $Zo_secs;
  183.   $Ti_count += $Zi_count;
  184.   $To_count += $Zo_count;
  185.   write;
  186. }
  187.  
  188. sub print_dashes {
  189.   $Zhost = $Zi_bytes = $Zo_bytes = $Zt_bytes =
  190.     $Zi_hrs = $Zo_hrs = $Zi_acps = $Zo_acps = $Zi_count = $Zo_count = 
  191.       "------------";
  192.   write;
  193.   # easy, isn't it?
  194. }
  195.  
  196. ################ missing ################
  197.  
  198. sub gethostname {
  199.   $ENV{"SHELL"} = "/bin/sh";
  200.   $try = `(hostname) 2>/dev/null`;
  201.   chop $try;
  202.   return $+ if $try =~ /^[-.\w]+$/;
  203.   $try = `uname -n 2>/dev/null`;
  204.   chop $try;
  205.   return $+ if $try =~ /^[-.\w]+$/;
  206.   $try = `uuname -l 2>/dev/null`;
  207.   chop $try;
  208.   return $+ if $try =~ /^[-.\w]+$/;
  209.   return undef;
  210. }
  211.