bump to 0.1.0 and add process information

This commit is contained in:
Zane C. B-H 2019-08-11 01:49:47 -05:00
parent 4b41137edf
commit 161a8d7b46
3 changed files with 89 additions and 3 deletions

View File

@ -1,5 +1,8 @@
Revision history for Net-Connection-lsof
0.1.0 2019-08-11/02:00
- Add support for process information.
0.0.3 2019-08-09/07:30
- Add a work around for on some Linux systems
where lsof will exit 1 upon successful completion,

View File

@ -17,7 +17,8 @@ my %WriteMakefileArgs = (
'Test::More' => '0',
},
PREREQ_PM => {
'Net::Connection' => '0.0.0',
'Net::Connection' => '0.2.0',
'Proc::ProcessTable' => '0.59',
},
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
clean => { FILES => 'Net-Connection-lsof-*' },

View File

@ -4,6 +4,7 @@ use 5.006;
use strict;
use warnings;
use Net::Connection;
use Proc::ProcessTable;
require Exporter;
our @ISA = qw(Exporter);
@ -15,11 +16,11 @@ Net::Connection::lsof - This uses lsof to generate a array of Net::Connection ob
=head1 VERSION
Version 0.0.3
Version 0.1.0
=cut
our $VERSION = '0.0.3';
our $VERSION = '0.1.0';
=head1 SYNOPSIS
@ -66,6 +67,12 @@ Defaults to 1.
Attempt to resolve the UID to a username.
Defaults to 1.
=head4 proc_info
Add assorted process information to the objects.
Defaults to 1.
my @objects;
@ -88,6 +95,9 @@ sub lsof_to_nc_objects{
if ( !defined( $func_args{uid_resolve} ) ){
$func_args{uid_resolve}=1;
}
if ( !defined( $func_args{proc_info} ) ){
$func_args{proc_info}=1;
}
my $output_raw=`lsof -i UDP -i TCP -n -l -P`;
if (
@ -103,6 +113,23 @@ sub lsof_to_nc_objects{
my @nc_objects;
# process info caches
my %pid_proc;
my %pid_pctmem;
my %pid_pctcpu;
my %pid_wchan;
my %pid_start;
my $proc_table;
my $physmem;
if ( $func_args{proc_info} ){
my $pt=Proc::ProcessTable->new;
$proc_table=$pt->table;
$physmem=`/sbin/sysctl -a hw.physmem`;
chomp( $physmem );
$physmem=~s/^.*\: //;
}
my $line_int=1;
while ( defined( $output_lines[$line_int] ) ){
my $command=substr $output_lines[$line_int], 0, 9;
@ -175,6 +202,61 @@ sub lsof_to_nc_objects{
$args->{state}=~s/[\(\)]//g;
}
#
# put together process info if requested
#
if ( $func_args{proc_info} ){
if ( defined( $pid_proc{ $args->{pid} } ) ){
$args->{proc}=$pid_proc{ $args->{pid} };
$args->{wchan}=$pid_wchan{ $args->{pid} };
$args->{pctmem}=$pid_pctmem{ $args->{pid} };
$args->{pctcpu}=$pid_pctcpu{ $args->{pid} };
$args->{pid_start}=$pid_start{ $args->{pid} };
}else{
my $loop=1;
my $proc_int=0;
while(
defined( $proc_table->[ $proc_int ] ) &&
$loop
){
# matched
if ( $proc_table->[ $proc_int ]->{pid} eq $args->{pid} ){
# exit the loop
$loop = 0;
# fetch and save the proc info
if ( $proc_table->[ $proc_int ]->cmndline =~ /^$/ ){
# kernel proc
$args->{proc}='['.$proc_table->[ $proc_int ]->{fname}.']';
}else{
# non-kernel proc
$args->{proc}=$proc_table->[ $proc_int ]->{cmndline};
}
$pid_proc{ $args->{pid} }=$args->{proc};
$args->{wchan}=$proc_table->[ $proc_int ]->{wchan};
$pid_wchan{ $args->{pid} }=$args->{wchan};
$args->{pid_start}=$proc_table->[ $proc_int ]->{pid_start};
$pid_start{ $args->{pid} }=$args->{pid_start};
$args->{pctcpu}=$proc_table->[ $proc_int ]->{pctcpu};
$pid_pctcpu{ $args->{pid} }=$args->{pctcpu};
if ($^O =~ /bsd/){
$args->{pctmem}= (( $proc_table->[ $proc_int ]->{rssize} * 1024 * 4 ) / $physmem) * 100;
}else{
$args->{pctmem}=$proc_table->[ $proc_int ]->{pctmem};
}
$pid_pctmem{ $args->{pid} }=$args->{pctmem};
}
$proc_int++;
}
}
}
push( @nc_objects, Net::Connection->new( $args ) );
$line_int++;