home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / lsof_3.37 / scripts / count_pf.perl < prev    next >
Encoding:
Text File  |  1995-07-31  |  1.1 KB  |  37 lines

  1. #!/usr/local/bin/perl
  2. #
  3. # count_pf.perl-- run lsof in repeat mode and count processes and
  4. #          files
  5.  
  6. sub interrupt { print "\n"; exit 0; }
  7.  
  8. $LSOF = "../lsof";            # path to lsof
  9. $RPT = 15;                # lsof repeat time
  10.  
  11. if ( ! -x $LSOF) { print "can't execute $LSOF\n"; exit 1 }
  12.  
  13. # Read lsof -HPF output repeatedly from a pipe.
  14.  
  15. $| = 1;                    # unbuffer output
  16. $SIG{'INT'} = 'interrupt';        # catch interrupt
  17. $proc = $files = $proto{'TCP'} = $proto{'UDP'} = 0;
  18. $progress="/";                # used to show "progress"
  19. open(P, "$LSOF -HPF -r $RPT|") || die "can't open pipe to $LSOF\n";
  20.  
  21. while (<P>) {
  22.     chop;
  23.     if (/^m/) {
  24.  
  25.     # A marker line signals the end of an lsof repetition.
  26.  
  27.     printf "%s  Processes: %5d,  Files: %6d,  TCP: %6d, UDP: %6d\r",
  28.         $progress, $proc, $files, $proto{'TCP'}, $proto{'UDP'};
  29.     $proc = $files = $proto{'TCP'} = $proto{'UDP'} = 0;
  30.     if ($progress eq "/") { $progress = "\\"; } else { $progress = "/"; }
  31.     next;
  32.     }
  33.     if (/^p/) { $proc++; next; }        # Count processes.
  34.     if (/^f/) { $files++; next; }        # Count files.
  35.     if (/^P(.*)/) { $proto{$1}++; next; }    # Count protocols.
  36. }
  37.