home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3195 / uutraf.pl
Encoding:
Perl Script  |  1991-04-17  |  5.3 KB  |  202 lines

  1. #!/usr/bin/perl
  2. eval "exec /usr/bin/perl -S $0 $*"
  3.   if $running_under_some_shell;
  4. do verify_perl_version (3001);
  5.  
  6. # @(#)@ uutraf    1.4 - uutraf.pl
  7. #
  8. # UUCP Traffic Analyzer
  9. #
  10. # Reads /usr/lib/uucp/.Admin/xferstats, and generates a report from it.
  11. # Also understands Ultrix SYSLOG format.
  12. #
  13. # Created by Johan Vromans <jv@mh.nl>
  14. # Loosely based on an idea by Greg Hackney (hack@texbell.swbt.com)
  15.  
  16. # Usage: uutraf [xferstats]
  17.  
  18. $type = "unknown";
  19.  
  20. if ( $#ARGV >= 0 ) {
  21.     open (STDIN, $ARGV[0]) || die "Cannot open $ARGV[0]";
  22.     open (IN, $ARGV[0]) || die "Cannot open $ARGV[0]";
  23.     $line = <IN>;
  24.     split (/ /, $line);
  25.     $type = ($_[0] =~ /!/) ? "HDB" : "U";
  26. }
  27. elsif ( -r "/usr/spool/uucp/.Admin/xferstats" ) {
  28.     open (STDIN, "/usr/spool/uucp/.Admin/xferstats");
  29.     $type = "HDB";
  30. }
  31. elsif ( -r "/usr/spool/uucp/SYSLOG" ) {
  32.     open (STDIN, "/usr/spool/uucp/SYSLOG");
  33.     $type = "U";
  34. }
  35. else { die "Sorry, don't know what"; }
  36.  
  37. if ( $type eq "HDB" ) {
  38.     $pat = "([^!]+)![^(]+\\(([-0-9:/]+)\\).+([<>])-? (\\d+) / (\\d+)\\.(\\d+) secs";
  39.     $recv = "<";
  40. }
  41. else {
  42.     $pat = "\\S+\\s+(\\S+)\\s+\\(([-0-9:/]+)\\)\\s+\\(\\d+\\)\\s+(\\w+) (\\d+) b (\\d+) secs";
  43.     $recv = "received";
  44. }
  45.  
  46. %hosts = ();        # hosts seen
  47. %bytes_in = ();        # of bytes received from host
  48. %bytes_out = ();    # of bytes sent to host
  49. %secs_in = ();        # of seconds connect for recving
  50. %secs_out = ();        # of seconds connect for sending
  51. %files_in = ();        # of input requests
  52. %files_out = ();    # of output requests
  53.  
  54. # read info, break the lines and tally
  55.  
  56. while ( <STDIN> ) {
  57.   if ( /^$pat/o ) {
  58. #   print "host $1, date $2, dir $3, bytes $4, secs $5.$6\n";
  59.     $6 = 0 if $type eq "U";
  60.     # gather timestamps
  61.     $last_date = $2;
  62.     $first_date = $last_date unless defined $first_date;
  63.  
  64.     # initialize new hosts
  65.     unless ( defined $hosts{$1} ) {
  66.       $hosts{$1} = $files_in{$1} = $files_out{$1} = 
  67.     $bytes_in{$1} = $bytes_out{$1} =
  68.       $secs_in{$1} = $secs_out{$1} = 0;
  69.     }
  70.  
  71.     # tally
  72.     if ( $3 eq $recv ) {        # recv
  73.       $bytes_in{$1} += $4;
  74.       $files_in{$1}++;
  75.       $secs_in{$1} += $5 + $6/1000;
  76.     }
  77.     else {            # xmit
  78.       $bytes_out{$1} += $4;
  79.       $files_out{$1}++;
  80.       $secs_out{$1} += $5 + $6/1000;
  81.     }
  82.   }
  83.   else {
  84.     print STDERR "Possible garbage: $_";
  85.   }
  86. }
  87.  
  88. @hosts = keys (%hosts);
  89. die "No info found, stopped" if $#hosts < 0;
  90.  
  91. ################ report section ################
  92.  
  93. $thishost = do gethostname();
  94. $thishost = (defined $thishost) ? "on node $thishost" : "report";
  95.  
  96. format std_head =
  97. @|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  98. "UUCP traffic $thishost from $first_date to $last_date"
  99.  
  100. Remote   -----------K-Bytes----------- ----Hours---- --Avg CPS-- --Files--
  101.  Host         Recv      Sent     Total   Recv   Sent  Recv  Sent Recv Sent
  102. .
  103. format std_out =
  104. @<<<<<<< @>>>>>>>> @>>>>>>>> @>>>>>>>> @>>>>> @>>>>> @>>>> @>>>> @>>> @>>>
  105. $Zhost,   $Zi_bytes, $Zo_bytes, $Zt_bytes, $Zi_hrs, $Zo_hrs, $Zi_acps, $Zo_acps, $Zi_count, $Zo_count
  106. .
  107.  
  108. $^ = "std_head";
  109. $~ = "std_out";
  110.  
  111. do print_dashes ();
  112.  
  113. reset "T";           # reset totals
  114.  
  115. foreach $host (@hosts) {
  116.   do print_line ($host, $bytes_in{$host}, $bytes_out{$host},
  117.          $secs_in{$host},  $secs_out{$host},
  118.          $files_in{$host}, $files_out{$host});
  119.  
  120. }
  121.  
  122. do print_dashes ();
  123. do print_line ("Total", $Ti_bytes, $To_bytes,
  124.            $Ti_secs, $To_secs, $Ti_count, $To_count);
  125.  
  126. ################ that's it ################
  127.  
  128. sub print_line {
  129.   reset "Z";        # reset print fields
  130.   local ($Zhost, 
  131.      $Zi_bytes, $Zo_bytes, 
  132.      $Zi_secs, $Zo_secs, 
  133.      $Zi_count, $Zo_count) = @_;
  134.   $Ti_bytes += $Zi_bytes;
  135.   $To_bytes += $Zo_bytes;
  136.   $Zt_bytes = $Zi_bytes + $Zo_bytes;
  137.   $Tt_bytes += $Zt_bytes;
  138.   $Zi_acps = ($Zi_secs > 0) ? sprintf ("%.0f", $Zi_bytes/$Zi_secs) : "0";
  139.   $Zo_acps = ($Zo_secs > 0) ? sprintf ("%.0f", $Zo_bytes/$Zo_secs) : "0";
  140.   $Zi_bytes = sprintf ("%.1f", $Zi_bytes/1000);
  141.   $Zo_bytes = sprintf ("%.1f", $Zo_bytes/1000);
  142.   $Zt_bytes = sprintf ("%.1f", $Zt_bytes/1000);
  143.   $Zi_hrs = sprintf ("%.1f", $Zi_secs/3600);
  144.   $Zo_hrs = sprintf ("%.1f", $Zo_secs/3600);
  145.   $Ti_secs += $Zi_secs;
  146.   $To_secs += $Zo_secs;
  147.   $Ti_count += $Zi_count;
  148.   $To_count += $Zo_count;
  149.   write;
  150. }
  151.  
  152. sub print_dashes {
  153.   $Zhost = $Zi_bytes = $Zo_bytes = $Zt_bytes =
  154.     $Zi_hrs = $Zo_hrs = $Zi_acps = $Zo_acps = $Zi_count = $Zo_count = 
  155.       "------------";
  156.   write;
  157.   # easy, isn't it?
  158. }
  159.  
  160. ################ missing ################
  161.  
  162. sub gethostname {
  163.   $ENV{"SHELL"} = "/bin/sh";
  164.   $try = `hostname 2>/dev/null`;
  165.   chop $try;
  166.   return $+ if $try =~ /^[-.\w]+$/;
  167.   $try = `uname -n 2>/dev/null`;
  168.   chop $try;
  169.   return $+ if $try =~ /^[-.\w]+$/;
  170.   $try = `uuname -l 2>/dev/null`;
  171.   chop $try;
  172.   return $+ if $try =~ /^[-.\w]+$/;
  173.   return undef;
  174. }
  175.  
  176. ################ verify perl version ################
  177.  
  178. # do verify_perl_version ( [ required , [ message ] ] )
  179.  
  180. sub verify_perl_version {
  181.   local ($version,$patchlevel) = $] =~ /(\d+.\d+).*\nPatch level: (\d+)/;
  182.   $version = $version * 1000 + $patchlevel;
  183.  
  184.   # did the caller pass a required version?
  185.   if ( $#_ >= 0 ) {
  186.     local ($req, $msg, @req);
  187.     @req = split (//, $req = shift);
  188.     # if the request is valid - check it
  189.     if ( $#req == 3 && $req > $version ) {
  190.       if ( $#_ >= 0 ) {    # user supplied message
  191.     $msg = shift;
  192.       }
  193.       else {
  194.         $msg = "Sorry, this program requires perl " . $req[0] . "." . $req[1] .
  195.             " patch level " . $req % 100 ." or later.\nStopped";
  196.       }
  197.       die $msg;
  198.     }
  199.   }
  200.   return $version;
  201. }
  202.