| 1 | |||||||||||||||||||||||||||||||||||||||||||
| 2 | package Net::Connection::lsof; | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Net::Connection; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Proc::ProcessTable; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use POSIX (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 13 | 4 | our @EXPORT = qw(lsof_to_nc_objects); | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | Net::Connection::lsof - This uses lsof to generate an array of Net::Connection objects. | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | Version 0.4.0 | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 4 | our $VERSION = '0.4.0'; | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | use Net::Connection::lsof; | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | my @objects; | ||||||||||||||||||||||||||||||||||||||||||
| 32 | eval{ @objects = lsof_to_nc_objects(); }; | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | # this time don't resolve ports, ptrs, or usernames | ||||||||||||||||||||||||||||||||||||||||||
| 35 | my $args={ | ||||||||||||||||||||||||||||||||||||||||||
| 36 | ports=>0, | ||||||||||||||||||||||||||||||||||||||||||
| 37 | ptrs=>0, | ||||||||||||||||||||||||||||||||||||||||||
| 38 | uid_resolve=>0, | ||||||||||||||||||||||||||||||||||||||||||
| 39 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 40 | eval{ @objects = lsof_to_nc_objects($args); }; | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | =head1 SUBROUTINES | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | =head2 lsof_to_nc_objects | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | This runs 'lsof -i UDP -i TCP -n -l -P' and parses the output | ||||||||||||||||||||||||||||||||||||||||||
| 47 | returns an array of L<Net::Connection> objects. If a non-zero exit code is | ||||||||||||||||||||||||||||||||||||||||||
| 48 | returned, it will die. On Linux an exit value of 1 is also accepted as | ||||||||||||||||||||||||||||||||||||||||||
| 49 | some distros will exit 1 upon success. | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | There is one optional argument and that is hash reference that can take | ||||||||||||||||||||||||||||||||||||||||||
| 52 | several possible keys. | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | =head3 args hash | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | =head4 ports | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | Attempt to resolve the port names. | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | Defaults to 1. | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | =head4 ptrs | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | Attempt to resolve the PTRs. | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | Defaults to 1. | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | =head4 uid_resolve | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | Attempt to resolve the UID to a username. | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | Defaults to 1. | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | =head4 proc_info | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | Add assorted process information to the objects. | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | Defaults to 1. | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | my @objects; | ||||||||||||||||||||||||||||||||||||||||||
| 81 | eval{ @objects = lsof_to_nc_objects( $args ); }; | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 4 | ▶ | sub lsof_to_nc_objects { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lsof_to_nc_objects: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 4 | my %func_args; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 4 | ▶ | if ( defined( $_[0] ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 3 | %func_args = %{ $_[0] }; | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 4 | foreach my $arg_name (qw(ptrs ports uid_resolve proc_info)) { | |||||||||||||||||||||||||||||||||||||||||
| 92 | 16 | ▶ | if ( !defined( $func_args{$arg_name} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 4 | $func_args{$arg_name} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 4 | my $output_raw = `lsof -i UDP -i TCP -n -l -P 2> /dev/null`; | |||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | # Some Linux distros, such as Debian 9, have a lsof that will exit 1 | ||||||||||||||||||||||||||||||||||||||||||
| 100 | # upon success, so on Linux also accept a exit value of 1. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 4 | ▶ | if ( ( $? != 0 ) && !( ( $^O eq 'linux' ) && ( $? == 256 ) ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$? != 0 and not $^O eq "linux" && $? == 256
$^O eq 'linux' && $? == 256
MC/DC66%2/3
$? != 0 and not $^O eq "linux" && $? == 256
$? != 0
$^O eq 'linux'
$? == 256
Truth table75%3/4
A and not(B && C)
A= $? != 0B= $^O eq 'linux'C= $? == 256
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | die( | |||||||||||||||||||||||||||||||||||||||||
| 103 | '"lsof -i UDP -i TCP -n -l -P" exited with a non-zero value or in the case of some linux distros a non-1 value' | ||||||||||||||||||||||||||||||||||||||||||
| 104 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 3 | return _parse_lsof_output( $output_raw, \%func_args ); | |||||||||||||||||||||||||||||||||||||||||
| 108 | } ## end sub lsof_to_nc_objects | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 6 | ▶ | sub _parse_lsof_output { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_lsof_output: called CC=17 SCAR=28.6
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 6 | my $output_raw = $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 112 | 6 | my %func_args = %{ $_[1] }; | |||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 6 | my @output_lines = split( /\n/, $output_raw ); | |||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | # skip the header line | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 6 | shift(@output_lines); | |||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 6 | my @nc_objects; | |||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | # per PID cache of assembled process info and a PID to process table entry mapping | ||||||||||||||||||||||||||||||||||||||||||
| 122 | my %pid_info; | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 6 | my %pid_to_proc_entry; | |||||||||||||||||||||||||||||||||||||||||
| 124 | 6 | my $physmem; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 6 | ▶ | if ( $func_args{proc_info} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 1 | my $process_table = Proc::ProcessTable->new->table; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | foreach my $proc_entry ( @{$process_table} ) { | |||||||||||||||||||||||||||||||||||||||||
| 128 | 9 | $pid_to_proc_entry{ $proc_entry->{pid} } = $proc_entry; | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 1 | $physmem = _get_physmem(); | |||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 6 | foreach my $output_line (@output_lines) { | |||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | # The first nine characters are the command column, which is not | ||||||||||||||||||||||||||||||||||||||||||
| 136 | # used, followed by a space. Anything not longer than that is not | ||||||||||||||||||||||||||||||||||||||||||
| 137 | # parsable. | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 10 | ▶ | if ( length($output_line) <= 10 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 2 | next; | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 8 | my $line = substr( $output_line, 10 ); | |||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 8 | $line =~ s/^[\t ]*//; | |||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 8 | my @line_split = split( /[\ \t]+/, $line ); | |||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | # skip any line missing the name field as it is not parsable | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 8 | ▶ | if ( !defined( $line_split[7] ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | my $args = { | ||||||||||||||||||||||||||||||||||||||||||
| 153 | pid => $line_split[0], | ||||||||||||||||||||||||||||||||||||||||||
| 154 | uid => $line_split[1], | ||||||||||||||||||||||||||||||||||||||||||
| 155 | ports => $func_args{ports}, | ||||||||||||||||||||||||||||||||||||||||||
| 156 | ptrs => $func_args{ptrs}, | ||||||||||||||||||||||||||||||||||||||||||
| 157 | uid_resolve => $func_args{uid_resolve}, | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 7 | }; | |||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 7 | my $type = $line_split[3]; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 7 | my $mode = $line_split[6]; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 7 | my $name = $line_split[7]; | |||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | # Use the name and type to build the proto. | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 7 | my $proto = ''; | |||||||||||||||||||||||||||||||||||||||||
| 166 | 7 | ▶ | if ( $type =~ /6/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 2 | $proto = '6'; | |||||||||||||||||||||||||||||||||||||||||
| 168 | } elsif ( $type =~ /4/ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 5 | $proto = '4'; | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 7 | ▶ | if ( $mode =~ /[Uu][Dd][Pp]/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 1 | $proto = 'udp' . $proto; | |||||||||||||||||||||||||||||||||||||||||
| 173 | } elsif ( $mode =~ /[Tt][Cc][Pp]/ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 6 | $proto = 'tcp' . $proto; | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | 7 | $args->{proto} = $proto; | |||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 7 | my ( $local, $foreign ) = split( /\-\>/, $name ); | |||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 7 | my $ip; | |||||||||||||||||||||||||||||||||||||||||
| 181 | my $port; | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 7 | ▶ | if ( !defined($foreign) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 2 | $args->{foreign_host} = '*'; | |||||||||||||||||||||||||||||||||||||||||
| 185 | 2 | $args->{foreign_port} = '*'; | |||||||||||||||||||||||||||||||||||||||||
| 186 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 5 | ▶ | if ( $foreign =~ /\]/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | 1 | ( $ip, $port ) = split( /\]/, $foreign ); | |||||||||||||||||||||||||||||||||||||||||
| 189 | 1 | $ip =~ s/^\[//; | |||||||||||||||||||||||||||||||||||||||||
| 190 | 1 | $port =~ s/\://; | |||||||||||||||||||||||||||||||||||||||||
| 191 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 4 | ( $ip, $port ) = split( /\:/, $foreign ); | |||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | 5 | $args->{foreign_host} = $ip; | |||||||||||||||||||||||||||||||||||||||||
| 196 | 5 | $args->{foreign_port} = $port; | |||||||||||||||||||||||||||||||||||||||||
| 197 | } ## end else [ if ( !defined($foreign) ) ] | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | 7 | ▶ | if ( $local =~ /\]/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | 2 | ( $ip, $port ) = split( /\]/, $local ); | |||||||||||||||||||||||||||||||||||||||||
| 201 | 2 | $ip =~ s/^\[//; | |||||||||||||||||||||||||||||||||||||||||
| 202 | 2 | $port =~ s/\://; | |||||||||||||||||||||||||||||||||||||||||
| 203 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 204 | 5 | ( $ip, $port ) = split( /\:/, $local ); | |||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||
| 206 | 7 | $args->{local_host} = $ip; | |||||||||||||||||||||||||||||||||||||||||
| 207 | 7 | $args->{local_port} = $port; | |||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | 7 | $args->{state} = ''; | |||||||||||||||||||||||||||||||||||||||||
| 210 | 7 | ▶ | if ( defined( $line_split[8] ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 6 | $args->{state} = $line_split[8]; | |||||||||||||||||||||||||||||||||||||||||
| 212 | 6 | $args->{state} =~ s/[\(\)]//g; | |||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | # | ||||||||||||||||||||||||||||||||||||||||||
| 216 | # put together process info if requested | ||||||||||||||||||||||||||||||||||||||||||
| 217 | # | ||||||||||||||||||||||||||||||||||||||||||
| 218 | 7 | ▶ | if ( $func_args{proc_info} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | ▶ | if ( !defined( $pid_info{ $args->{pid} } ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | $pid_info{ $args->{pid} } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | = _proc_info_for_pid( $pid_to_proc_entry{ $args->{pid} }, $args->{pid}, $physmem ); | |||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | my $proc_info = $pid_info{ $args->{pid} }; | |||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | foreach my $info_key ( keys( %{$proc_info} ) ) { | |||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | $args->{$info_key} = $proc_info->{$info_key}; | |||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | } ## end if ( $func_args{proc_info} ) | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | 7 | push( @nc_objects, Net::Connection->new($args) ); | |||||||||||||||||||||||||||||||||||||||||
| 230 | } ## end foreach my $output_line (@output_lines) | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 6 | return @nc_objects; | |||||||||||||||||||||||||||||||||||||||||
| 233 | } ## end sub _parse_lsof_output | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | 0 | ▶ | sub _proc_info_for_pid { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_proc_info_for_pid: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | my $proc_entry = $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | my $pid = $_[1]; | |||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | my $physmem = $_[2]; | |||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | # The process exited between the lsof invocation and the process | ||||||||||||||||||||||||||||||||||||||||||
| 241 | # table being fetched. Returning a empty hash caches the negative | ||||||||||||||||||||||||||||||||||||||||||
| 242 | # result, preventing pointless repeat lookups. | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | ▶ | if ( !defined($proc_entry) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | return {}; | |||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | my %proc_info; | |||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | ▶ | if ( !defined( $proc_entry->{cmndline} ) || $proc_entry->{cmndline} eq '' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $proc_entry->{"cmndline"} or $proc_entry->{"cmndline"} eq ""
MC/DC0%0/2
not defined $proc_entry->{"cmndline"} or $proc_entry->{"cmndline"} eq ""
not defined $proc_entry->{"cmndline"}
$proc_entry->{"cmndline"} eq ""
Truth table0%0/3
not(A) or B
A= not defined $proc_entry->{"cmndline"}B= $proc_entry->{"cmndline"} eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | # kernel proc | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | $proc_info{proc} = '[' . $proc_entry->{fname} . ']'; | |||||||||||||||||||||||||||||||||||||||||
| 253 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | # non-kernel proc | ||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | $proc_info{proc} = $proc_entry->{cmndline}; | |||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | # Linux has shit wchan reporting and apparently if you want more than | ||||||||||||||||||||||||||||||||||||||||||
| 260 | # the address you need to use the ass backwards /proc | ||||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | ▶ | if ( $^O eq 'linux' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | ▶ | if ( open( my $wchan_fh, '<', '/proc/' . $pid . '/wchan' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | $proc_info{wchan} = readline($wchan_fh); | |||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | close($wchan_fh); | |||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||
| 266 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | $proc_info{wchan} = $proc_entry->{wchan}; | |||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | $proc_info{pid_start} = $proc_entry->{start}; | |||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | eval { $proc_info{pctcpu} = $proc_entry->pctcpu; }; | |||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | # Not all platforms provide pctmem, so on those compute it from | ||||||||||||||||||||||||||||||||||||||||||
| 275 | # the RSS, which Proc::ProcessTable provides in bytes everywhere. | ||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | my $proc_pctmem; | |||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | eval { $proc_pctmem = $proc_entry->pctmem; }; | |||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | ▶ | if ( !defined($proc_pctmem) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $proc_pctmem and defined $physmem
MC/DC0%0/2
not defined $proc_pctmem and defined $physmem
not defined $proc_pctmem
defined $physmem
Truth table0%0/3
not(A) and B
A= not defined $proc_pctmemB= defined $physmem
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | && defined($physmem) ) | ||||||||||||||||||||||||||||||||||||||||||
| 280 | { | ||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | eval { $proc_pctmem = ( $proc_entry->rss / $physmem ) * 100; }; | |||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | $proc_info{pctmem} = $proc_pctmem; | |||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | return \%proc_info; | |||||||||||||||||||||||||||||||||||||||||
| 286 | } ## end sub _proc_info_for_pid | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | 1 | ▶ | sub _get_physmem { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_physmem: called CC=8 SCAR=27.2
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 1 | my $physmem; | |||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | # Figure out how much physical memory this machine has, which is | ||||||||||||||||||||||||||||||||||||||||||
| 292 | # needed on platforms where Proc::ProcessTable does not provide | ||||||||||||||||||||||||||||||||||||||||||
| 293 | # pctmem. First try POSIX::sysconf, which requires no external | ||||||||||||||||||||||||||||||||||||||||||
| 294 | # programs, but not every POSIX module exposes _SC_PHYS_PAGES. | ||||||||||||||||||||||||||||||||||||||||||
| 295 | 1 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 296 | 1 | my $phys_pages = POSIX::sysconf( POSIX::_SC_PHYS_PAGES() ); | |||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | my $pagesize = POSIX::sysconf( POSIX::_SC_PAGESIZE() ); | |||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | ▶ | if ( $phys_pages && $pagesize ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$phys_pages and $pagesize
MC/DC0%0/2
$phys_pages and $pagesize
$phys_pages
$pagesize
Truth table0%0/3
A and B
A= $phys_pagesB= $pagesize
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | $physmem = $phys_pages * $pagesize; | |||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||
| 301 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | # Fall back on sysctl, checking the various places it may live. | ||||||||||||||||||||||||||||||||||||||||||
| 304 | 1 | ▶ | if ( !defined($physmem) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | 1 | foreach my $sysctl_bin ( '/sbin/sysctl', '/usr/sbin/sysctl' ) { | |||||||||||||||||||||||||||||||||||||||||
| 306 | 2 | ▶ | if ( -x $sysctl_bin ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | 2 | my $sysctl_physmem = `$sysctl_bin -n hw.physmem 2> /dev/null`; | |||||||||||||||||||||||||||||||||||||||||
| 308 | 2 | ▶ | if ( defined($sysctl_physmem) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 309 | 2 | chomp($sysctl_physmem); | |||||||||||||||||||||||||||||||||||||||||
| 310 | 2 | ▶ | if ( $sysctl_physmem =~ /^[0-9]+$/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | $physmem = $sysctl_physmem; | |||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 315 | } ## end if ( -x $sysctl_bin ) | ||||||||||||||||||||||||||||||||||||||||||
| 316 | } ## end foreach my $sysctl_bin ( '/sbin/sysctl', '/usr/sbin/sysctl') | ||||||||||||||||||||||||||||||||||||||||||
| 317 | } ## end if ( !defined($physmem) ) | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | # If physmem could not be found, pctmem is left undef on platforms | ||||||||||||||||||||||||||||||||||||||||||
| 320 | # where Proc::ProcessTable does not provide it, so failing here is | ||||||||||||||||||||||||||||||||||||||||||
| 321 | # not fatal. | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | 1 | return $physmem; | |||||||||||||||||||||||||||||||||||||||||
| 324 | } ## end sub _get_physmem | ||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >> | ||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | Please report any bugs or feature requests to C<bug-net-connection-lsof at rt.cpan.org>, or through | ||||||||||||||||||||||||||||||||||||||||||
| 333 | the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-lsof>. I will be notified, and then you'll | ||||||||||||||||||||||||||||||||||||||||||
| 334 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | perldoc Net::Connection::lsof | ||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-lsof> | ||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | =item * Search CPAN | ||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | L<https://metacpan.org/release/Net-Connection-lsof> | ||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | =item * Git Repo | ||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | L<https://github.com/VVelox/Net-Connection-lsof> | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||
| 368 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | This software is Copyright (c) 2019 by Zane C. Bowers-Hadley. | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | This is free software, licensed under: | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||
| 379 | 4 | 1; # End of Net::Connection::lsof | |||||||||||||||||||||||||||||||||||||||||