Match/PctMem.pm

filter: Statementstmt 94.7% 109 / 115 Branchbran 87.1% 61 / 70 Conditioncond 55.5% 10 / 18 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 86.8% 198 / 228 CC 43 SCAR 27.9
CC
43
Cov
89%
physmem
22.1
match
22.0
pctmem_compute
7.1
CRAP 16.3
« Match/UID.pm ↑ Summary Match/PctCPU.pm »
1 package Net::Connection::Match::PctMem;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Proc::ProcessTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Net::Connection::Match::PctMem - Check if the pctmem of a process matches for the process that has the connection.
12  
13 =head1 VERSION
14  
15 Version 0.0.0
16  
17 =cut
18  
19 2 our $VERSION = '0.0.0';
20  
21 =head1 SYNOPSIS
22  
23 use Net::Connection::Match::PctMem;
24 use Net::Connection;
25
26 my $connection_args={
27 foreign_host=>'10.0.0.1',
28 foreign_port=>'22',
29 local_host=>'10.0.0.2',
30 local_port=>'12322',
31 proto=>'tcp4',
32 state=>'ESTABLISHED',
33 pid=>0,
34 pctmem=>'5.03',
35 };
36
37 my $conn=Net::Connection->new( $connection_args );
38
39 my %args=(
40 pctmems=>[
41 '>1',
42 ],
43 );
44
45 my $checker=Net::Connection::Match::PctMem->new( \%args );
46
47 if ( $checker->match( $conn ) ){
48 print "It matches.\n";
49 }
50  
51 =head1 METHODS
52  
53 =head2 new
54  
55 This intiates the object.
56  
57 It takes a hash reference with one key. One key is required and
58 that is 'pctmems', which is a array of pctmem value.
59  
60 The pctmem values can be prefixed with the equalities below for doing
61 additional comparisons.
62  
63 <
64 <=
65 >
66 >=
67  
68 Atleast one must be specified.
69  
70  
71 If the new method fails, it dies.
72  
73 my %args=(
74 pctmems=>[
75 '>1',
76 ],
77 );
78
79 my $checker=Net::Connection::Match::PctMem->new( \%args );
80  
81 =cut
82  
83 27 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
84 27 my %args;
85 27 ▶ if ( defined( $_[1] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 26 1 27
86 26 %args = %{ $_[1] };
87 }
88  
89 # run some basic checks to make sure we have the minimum stuff required to work
90 27 ▶ if ( !defined( $args{pctmems} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"pctmems"}) 1 26 27
91 1 die('No pctmems key specified in the argument hash');
92 }
93 26 ▶ if ( ref( $args{pctmems} ) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"pctmems"} ne "ARRAY") 1 25 26
94 1 die('The pctmems key is not a array');
95 }
96 25 ▶ if ( !defined $args{pctmems}[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"pctmems"}[0]) 1 24 25
97 1 die('Nothing defined in the pctmems array');
98 }
99  
100 24 my $self = { pctmems => $args{pctmems}, };
101 24 bless $self;
102  
103 24 return $self;
104 } ## end sub new
105  
106 =head2 match
107  
108 Checks if a single Net::Connection object matches the stack.
109  
110 One argument is taken and that is a Net::Connection object.
111  
112 The returned value is a boolean.
113  
114 if ( $checker->match( $conn ) ){
115 print "The connection matches.\n";
116 }
117  
118 =cut
119  
120 26 ▶sub match {
Subroutine match: called CC=22 SCAR=30.9
Pod documented
121 26 my $self = $_[0];
122 26 my $object = $_[1];
123  
124 26 ▶ if ( !defined($object) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $object) 1 25 26
125 1 return 0;
126 }
127  
128 25 ▶ if ( ref($object) ne 'Net::Connection' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object ne "Net::Connection") 1 24 25
129 1 return 0;
130 }
131  
132 24 my $conn_pid = $object->pid;
133  
134 # don't bother proceeding, the object won't match ever
135 # as it does not have a PID
136 24 ▶ if ( !defined($conn_pid) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conn_pid) 1 23 24
137 1 return 0;
138 }
139  
140 23 my $pctmem;
141 23 ▶ if ( !defined( $object->proc ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $object->proc) { } 5 18 23
142 # go through each proc and look for a matching pid
143 5 my $proctable = Proc::ProcessTable->new;
144 5 my $procs = $proctable->table;
145 5 my $proc_int = 0;
146 5 my $loop = 1;
147 5 ▶ while ( $loop
Branch: 1 branch
BranchTrueFalseTotal
if ($loop and defined $procs->[$proc_int]) 10 5 15
Condition100%3/3
$loop and defined $procs->[$proc_int]
!ll&&!rl&&r
4110
MC/DC100%2/2
$loop and defined $procs->[$proc_int]
$loop defined $procs->[$proc_int]
Truth table100%3/3
A and B
A= $loopB= defined $procs->[$proc_int]
ABresult
0X0
100
111
148 && defined( $procs->[$proc_int] ) )
149 {
150  
151 10 ▶ if ( $conn_pid eq $procs->[$proc_int]->{pid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conn_pid eq $procs->[$proc_int]{"pid"}) 4 6 10
152 4 $pctmem = $procs->[$proc_int]->{pctmem};
153  
154 # not all platforms provide the pctmem field, so compute it if possible
155 4 ▶ if ( !defined($pctmem) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pctmem) 2 2 4
156 2 $pctmem = $self->pctmem_compute( $procs->[$proc_int] );
157 }
158  
159 # exit the loop as we found it
160 4 $loop = 0;
161 } ## end if ( $conn_pid eq $procs->[$proc_int]->{pid...})
162  
163 10 $proc_int++;
164 } ## end while ( $loop && defined( $procs->[$proc_int]...))
165 } else {
166 18 $pctmem = $object->pctmem;
167 }
168  
169 # likely a dead connection that is handing around...
170 # or disappeared since grabbing the connection list
171 # and starting processing
172 23 ▶ if ( !defined($pctmem) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pctmem) 2 21 23
173 2 return 0;
174 }
175  
176 # handle non-numeric values, such as inf or nan, some platforms may return
177 21 ▶ if ( $pctmem !~ /^\s*[0-9]*\.?[0-9]+\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pctmem =~ /^\s*[0-9]*\.?[0-9]+\s*$/) 1 20 21
178 1 $pctmem = 0;
179 }
180  
181 # use while as foreach will reference the value
182 21 my $pctmem_int = 0;
183 21 ▶ while ( defined( $self->{pctmems}[$pctmem_int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"pctmems"}[$pctmem_int]) 24 9 33
184 24 my $value = $self->{pctmems}[$pctmem_int];
185 24 ▶ if ( ( $value =~ /^[0-9]*\.?[0-9]+$/ )
Branch: 5 branches
BranchTrueFalseTotal
if ($value =~ /^[0-9]*\.?[0-9]+$/ and $value == $pctmem) { } 4 20 24
elsif ($value =~ /^\<\=[0-9]*\.?[0-9]+$/) { } 3 17 20
elsif ($value =~ /^\<[0-9]*\.?[0-9]+$/) { } 5 12 17
elsif ($value =~ /^\>\=[0-9]*\.?[0-9]+$/) { } 3 9 12
elsif ($value =~ /^\>[0-9]*\.?[0-9]+$/) { } 7 2 9
Condition100%3/3
$value =~ /^[0-9]*\.?[0-9]+$/ and $value == $pctmem
!ll&&!rl&&r
1824
MC/DC100%2/2
$value =~ /^[0-9]*\.?[0-9]+$/ and $value == $pctmem
$value =~ /^[0-9]*\.?[0-9]+$/ $value == $pctmem
Truth table100%3/3
A and B
A= $value =~ /^[0-9]*\.?[0-9]+$/B= $value == $pctmem
ABresult
0X0
100
111
186 && ( $value == $pctmem ) )
187 {
188 4 return 1;
189 } elsif ( $value =~ /^\<\=[0-9]*\.?[0-9]+$/ ) {
190 3 $value =~ s/^\<\=//;
191 3 ▶ if ( $pctmem <= $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pctmem <= $value) 2 1 3
192 2 return 1;
193 }
194 } elsif ( $value =~ /^\<[0-9]*\.?[0-9]+$/ ) {
195 5 $value =~ s/^\<//;
196 5 ▶ if ( $pctmem < $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pctmem < $value) 2 3 5
197 2 return 1;
198 }
199 } elsif ( $value =~ /^\>\=[0-9]*\.?[0-9]+$/ ) {
200 3 $value =~ s/^\>\=//;
201 3 ▶ if ( $pctmem >= $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pctmem >= $value) 2 1 3
202 2 return 1;
203 }
204 } elsif ( $value =~ /^\>[0-9]*\.?[0-9]+$/ ) {
205 7 $value =~ s/^\>//;
206 7 ▶ if ( $pctmem > $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pctmem > $value) 2 5 7
207 2 return 1;
208 }
209 }
210 12 $pctmem_int++;
211 } ## end while ( defined( $self->{pctmems}[$pctmem_int...]))
212  
213 9 return 0;
214 } ## end sub match
215  
216 =head2 pctmem_compute
217  
218 Computes the pctmem for a proc table entry on platforms where
219 L<Proc::ProcessTable> does not provide the pctmem field.
220  
221 Returns undef if it can not be computed.
222  
223 my $pctmem=$checker->pctmem_compute( $proc );
224  
225 =cut
226  
227 5 ▶sub pctmem_compute {
Subroutine pctmem_compute: called CC=7 SCAR=19.6
Pod documented
228 5 my $self = $_[0];
229 5 my $proc = $_[1];
230  
231 5 ▶ if ( !defined($proc) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $proc) 1 4 5
232 1 return undef;
233 }
234  
235 4 my $physmem = $self->physmem;
236 4 ▶ if ( !defined($physmem) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $physmem) 2 2 4
237 2 return undef;
238 }
239  
240 # use rss if provided as it is in bytes everywhere,
241 # otherwise fall back on rssize, which is in pages
242 2 my $rss = $proc->{rss};
243 2 ▶ if ( ( !defined($rss) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rss and defined $proc->{"rssize"}) 1 1 2
Condition66%2/3
not defined $rss and defined $proc->{"rssize"}
!ll&&!rl&&r
101
MC/DC50%1/2
not defined $rss and defined $proc->{"rssize"}
not defined $rss defined $proc->{"rssize"}
Truth table66%2/3
not(A) and B
A= not defined $rssB= defined $proc->{"rssize"}
ABresult
0X0
100
111
244 && defined( $proc->{rssize} ) )
245 {
246 1 my $pagesize;
247 1 eval { $pagesize = POSIX::sysconf( POSIX::_SC_PAGESIZE() ); };
248 1 ▶ if ($pagesize) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pagesize) 1 0 1
249 1 $rss = $proc->{rssize} * $pagesize;
250 }
251 }
252 2 ▶ if ( !defined($rss) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rss) 0 2 2
253 0 return undef;
254 }
255  
256 2 return ( $rss / $physmem ) * 100;
257 } ## end sub pctmem_compute
258  
259 =head2 physmem
260  
261 Returns the physical memory size in bytes on platforms it is known
262 how to fetch it on. Otherwise undef is returned.
263  
264 POSIX::sysconf is tried first and failing that sysctl is used.
265  
266 my $physmem=$checker->physmem;
267  
268 =cut
269  
270 6 ▶sub physmem {
Subroutine physmem: called CC=12 SCAR=31.0
Pod documented
271 6 my $self = $_[0];
272  
273 6 ▶ if ( $self->{physmem_checked} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"physmem_checked"}) 5 1 6
274 5 return $self->{physmem};
275 }
276 1 $self->{physmem_checked} = 1;
277  
278 1 my $physmem;
279  
280 # first try POSIX::sysconf, which requires no external programs,
281 # but not every POSIX module exposes _SC_PHYS_PAGES
282 1 eval {
283 1 my $phys_pages = POSIX::sysconf( POSIX::_SC_PHYS_PAGES() );
284 0 my $pagesize = POSIX::sysconf( POSIX::_SC_PAGESIZE() );
285 0 ▶ if ( $phys_pages && $pagesize ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($phys_pages and $pagesize) 0 0 0
Condition0%0/3
$phys_pages and $pagesize
!ll&&!rl&&r
000
MC/DC0%0/2
$phys_pages and $pagesize
$phys_pages $pagesize
Truth table0%0/3
A and B
A= $phys_pagesB= $pagesize
ABresult
0X0
100
111
286 0 $physmem = $phys_pages * $pagesize;
287 }
288 };
289  
290 # fall back on sysctl, checking the various places it may live
291 1 ▶ if ( !defined($physmem) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $physmem) 1 0 1
292 # hw.physmem on OpenBSD is truncated to 32 bits, so use hw.physmem64 there
293 1 my $physmem_variable = 'hw.physmem';
294 1 ▶ if ( $^O eq 'openbsd' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "openbsd") 0 1 1
295 0 $physmem_variable = 'hw.physmem64';
296 }
297 1 foreach my $sysctl_bin ( '/sbin/sysctl', '/usr/sbin/sysctl' ) {
298 2 ▶ if ( ( !defined($physmem) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $physmem and -x $sysctl_bin) 2 0 2
Condition33%1/3
not defined $physmem and -x $sysctl_bin
!ll&&!rl&&r
002
MC/DC0%0/2
not defined $physmem and -x $sysctl_bin
not defined $physmem -x $sysctl_bin
Truth table33%1/3
not(A) and B
A= not defined $physmemB= -x $sysctl_bin
ABresult
0X0
100
111
299 && ( -x $sysctl_bin ) )
300 {
301 2 my $sysctl_output = `$sysctl_bin -n $physmem_variable 2> /dev/null`;
302 2 ▶ if ( defined($sysctl_output) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sysctl_output) 2 0 2
303 2 chomp($sysctl_output);
304 2 ▶ if ( ( $sysctl_output =~ /^[0-9]+$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($sysctl_output =~ /^[0-9]+$/ and $sysctl_output > 0) 0 2 2
Condition33%1/3
$sysctl_output =~ /^[0-9]+$/ and $sysctl_output > 0
!ll&&!rl&&r
200
MC/DC0%0/2
$sysctl_output =~ /^[0-9]+$/ and $sysctl_output > 0
$sysctl_output =~ /^[0-9]+$/ $sysctl_output > 0
Truth table33%1/3
A and B
A= $sysctl_output =~ /^[0-9]+$/B= $sysctl_output > 0
ABresult
0X0
100
111
305 && ( $sysctl_output > 0 ) )
306 {
307 0 $physmem = $sysctl_output;
308 }
309 }
310 } ## end if ( ( !defined($physmem) ) && ( -x $sysctl_bin...))
311 } ## end foreach my $sysctl_bin ( '/sbin/sysctl', '/usr/sbin/sysctl')
312 } ## end if ( !defined($physmem) )
313  
314 1 $self->{physmem} = $physmem;
315  
316 1 return $physmem;
317 } ## end sub physmem
318  
319 =head1 AUTHOR
320  
321 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
322  
323 =head1 BUGS
324  
325 Please report any bugs or feature requests to C<bug-net-connection-match at rt.cpan.org>, or through
326 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Match>. I will be notified, and then you'll
327 automatically be notified of progress on your bug as I make changes.
328  
329  
330  
331  
332 =head1 SUPPORT
333  
334 You can find documentation for this module with the perldoc command.
335  
336 perldoc Net::Connection::Match
337  
338  
339 You can also look for information at:
340  
341 =over 4
342  
343 =item * RT: CPAN's request tracker (report bugs here)
344  
345 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Match>
346  
347 =item * AnnoCPAN: Annotated CPAN documentation
348  
349 L<http://annocpan.org/dist/Net-Connection-Match>
350  
351 =item * CPAN Ratings
352  
353 L<https://cpanratings.perl.org/d/Net-Connection-Match>
354  
355 =item * Search CPAN
356  
357 L<https://metacpan.org/release/Net-Connection-Match>
358  
359 =back
360  
361  
362 =head1 ACKNOWLEDGEMENTS
363  
364  
365 =head1 LICENSE AND COPYRIGHT
366  
367 Copyright 2019 Zane C. Bowers-Hadley.
368  
369 This program is free software; you can redistribute it and/or modify it
370 under the terms of the the Artistic License (2.0). You may obtain a
371 copy of the full license at:
372  
373 L<http://www.perlfoundation.org/artistic_license_2_0>
374  
375 Any use, modification, and distribution of the Standard or Modified
376 Versions is governed by this Artistic License. By using, modifying or
377 distributing the Package, you accept this license. Do not use, modify,
378 or distribute the Package, if you do not accept this license.
379  
380 If your Modified Version has been derived from a Modified Version made
381 by someone other than you, you are nevertheless required to ensure that
382 your Modified Version complies with the requirements of this license.
383  
384 This license does not grant you the right to use any trademark, service
385 mark, tradename, or logo of the Copyright Holder.
386  
387 This license includes the non-exclusive, worldwide, free-of-charge
388 patent license to make, have made, use, offer to sell, sell, import and
389 otherwise transfer the Package with respect to any patent claims
390 licensable by the Copyright Holder that are necessarily infringed by the
391 Package. If you institute patent litigation (including a cross-claim or
392 counterclaim) against any party alleging that the Package constitutes
393 direct or contributory patent infringement, then this Artistic License
394 to you shall terminate on the date that such litigation is filed.
395  
396 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
397 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
398 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
399 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
400 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
401 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
402 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
403 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
404  
405  
406 =cut
407  
408 2 1; # End of Net::Connection::Match
« Match/UID.pm ↑ Summary Match/PctCPU.pm »