diff options
author | Patrick McCarty <patrick.mccarty@linux.intel.com> | 2013-02-08 13:26:27 -0800 |
---|---|---|
committer | Patrick McCarty <patrick.mccarty@linux.intel.com> | 2013-02-08 13:26:27 -0800 |
commit | 9bb81f8a90ecc8b70c955bff72ec59dd3d9e5ae7 (patch) | |
tree | 881eebfa461e4f8aa6b6f44b96ac0decd3bc887a /scripts/count_pf.perl | |
download | lsof-9bb81f8a90ecc8b70c955bff72ec59dd3d9e5ae7.tar.gz lsof-9bb81f8a90ecc8b70c955bff72ec59dd3d9e5ae7.tar.bz2 lsof-9bb81f8a90ecc8b70c955bff72ec59dd3d9e5ae7.zip |
Imported Upstream version 4.87upstream/4.87
Diffstat (limited to 'scripts/count_pf.perl')
-rwxr-xr-x | scripts/count_pf.perl | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/scripts/count_pf.perl b/scripts/count_pf.perl new file mode 100755 index 0000000..f063697 --- /dev/null +++ b/scripts/count_pf.perl @@ -0,0 +1,68 @@ +#!/usr/local/bin/perl +# +# count_pf.perl-- run lsof in repeat mode and count processes and +# files + +sub interrupt { print "\n"; exit 0; } + +$RPT = 15; # lsof repeat time + +# Set path to lsof. + +if (($LSOF = &isexec("../lsof")) eq "") { # Try .. first + if (($LSOF = &isexec("lsof")) eq "") { # Then try . and $PATH + print "can't execute $LSOF\n"; exit 1 + } +} + +# Read lsof -nPF output repeatedly from a pipe. + +$| = 1; # unbuffer output +$SIG{'INT'} = 'interrupt'; # catch interrupt +$proc = $files = $proto{'TCP'} = $proto{'UDP'} = 0; +$progress="/"; # used to show "progress" +open(P, "$LSOF -nPF -r $RPT|") || die "can't open pipe to $LSOF\n"; + +while (<P>) { + chop; + if (/^m/) { + + # A marker line signals the end of an lsof repetition. + + printf "%s Processes: %5d, Files: %6d, TCP: %6d, UDP: %6d\r", + $progress, $proc, $files, $proto{'TCP'}, $proto{'UDP'}; + $proc = $files = $proto{'TCP'} = $proto{'UDP'} = 0; + if ($progress eq "/") { $progress = "\\"; } else { $progress = "/"; } + next; + } + if (/^p/) { $proc++; next; } # Count processes. + if (/^f/) { $files++; next; } # Count files. + if (/^P(.*)/) { $proto{$1}++; next; } # Count protocols. +} + + +## isexec($path) -- is $path executable +# +# $path = absolute or relative path to file to test for executabiity. +# Paths that begin with neither '/' nor '.' that arent't found as +# simple references are also tested with the path prefixes of the +# PATH environment variable. + +sub +isexec { + my ($path) = @_; + my ($i, @P, $PATH); + + $path =~ s/^\s+|\s+$//g; + if ($path eq "") { return(""); } + if (($path =~ m#^[\/\.]#)) { + if (-x $path) { return($path); } + return(""); + } + $PATH = $ENV{PATH}; + @P = split(":", $PATH); + for ($i = 0; $i <= $#P; $i++) { + if (-x "$P[$i]/$path") { return("$P[$i]/$path"); } + } + return(""); +} |