Match/PctCPU.pm

filter: Statementstmt 100% 70 / 70 Branchbran 100% 44 / 44 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 100% 132 / 132 CC 25 SCAR 27.5
CC
25
Cov
100%
match
21.0
new
5.0
BEGIN
1.0
CRAP 15.7
« Match/PctMem.pm ↑ Summary Match/Command.pm »
1 package Net::Connection::Match::PctCPU;
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  
8 =head1 NAME
9  
10 Net::Connection::Match::PctCPU - Check if the pctcpu of a process matches for the process that has the connection.
11  
12 =head1 VERSION
13  
14 Version 0.0.0
15  
16 =cut
17  
18 2 our $VERSION = '0.0.0';
19  
20 =head1 SYNOPSIS
21  
22 use Net::Connection::Match::PctCPU;
23 use Net::Connection;
24
25 my $connection_args={
26 foreign_host=>'10.0.0.1',
27 foreign_port=>'22',
28 local_host=>'10.0.0.2',
29 local_port=>'12322',
30 proto=>'tcp4',
31 state=>'ESTABLISHED',
32 pid=>0,
33 pctcpu=>'5.03',
34 };
35
36 my $conn=Net::Connection->new( $connection_args );
37
38 my %args=(
39 pctcpus=>[
40 '>1',
41 ],
42 );
43
44 my $checker=Net::Connection::Match::PctCPU->new( \%args );
45
46 if ( $checker->match( $conn ) ){
47 print "It matches.\n";
48 }
49  
50 =head1 METHODS
51  
52 =head2 new
53  
54 This intiates the object.
55  
56 It takes a hash reference with one key. One key is required and
57 that is 'pctcpus', which is a array of pctcpu value.
58  
59 The pctcpu values can be prefixed with the equalities below for doing
60 additional comparisons.
61  
62 <
63 <=
64 >
65 >=
66  
67 Atleast one must be specified.
68  
69  
70 If the new method fails, it dies.
71  
72 my %args=(
73 pctcpus=>[
74 '>1',
75 ],
76 );
77
78 my $checker=Net::Connection::Match::PctCPU->new( \%args );
79  
80 =cut
81  
82 24 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
83 24 my %args;
84 24 ▶ if ( defined( $_[1] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 23 1 24
85 23 %args = %{ $_[1] };
86 }
87  
88 # run some basic checks to make sure we have the minimum stuff required to work
89 24 ▶ if ( !defined( $args{pctcpus} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"pctcpus"}) 1 23 24
90 1 die('No pctcpus key specified in the argument hash');
91 }
92 23 ▶ if ( ref( $args{pctcpus} ) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"pctcpus"} ne "ARRAY") 1 22 23
93 1 die('The pctcpus key is not a array');
94 }
95 22 ▶ if ( !defined $args{pctcpus}[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"pctcpus"}[0]) 1 21 22
96 1 die('Nothing defined in the pctcpus array');
97 }
98  
99 21 my $self = { pctcpus => $args{pctcpus}, };
100 21 bless $self;
101  
102 21 return $self;
103 } ## end sub new
104  
105 =head2 match
106  
107 Checks if a single Net::Connection object matches the stack.
108  
109 One argument is taken and that is a Net::Connection object.
110  
111 The returned value is a boolean.
112  
113 if ( $checker->match( $conn ) ){
114 print "The connection matches.\n";
115 }
116  
117 =cut
118  
119 24 ▶sub match {
Subroutine match: called CC=21 SCAR=30.4
Pod documented
120 24 my $self = $_[0];
121 24 my $object = $_[1];
122  
123 24 ▶ if ( !defined($object) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $object) 1 23 24
124 1 return 0;
125 }
126  
127 23 ▶ if ( ref($object) ne 'Net::Connection' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object ne "Net::Connection") 1 22 23
128 1 return 0;
129 }
130  
131 22 my $conn_pid = $object->pid;
132  
133 # don't bother proceeding, the object won't match ever
134 # as it does not have a PID
135 22 ▶ if ( !defined($conn_pid) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conn_pid) 1 21 22
136 1 return 0;
137 }
138  
139 21 my $pctcpu;
140 21 ▶ if ( !defined( $object->proc ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $object->proc) { } 3 18 21
141 # go through each proc and look for a matching pid
142 3 my $proctable = Proc::ProcessTable->new;
143 3 my $procs = $proctable->table;
144 3 my $proc_int = 0;
145 3 my $loop = 1;
146 3 ▶ while ( $loop
Branch: 1 branch
BranchTrueFalseTotal
if ($loop and defined $procs->[$proc_int]) 3 3 6
Condition100%3/3
$loop and defined $procs->[$proc_int]
!ll&&!rl&&r
213
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
147 && defined( $procs->[$proc_int] ) )
148 {
149  
150 3 ▶ if ( $conn_pid eq $procs->[$proc_int]->{pid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conn_pid eq $procs->[$proc_int]{"pid"}) 2 1 3
151 2 $pctcpu = $procs->[$proc_int]->{pctcpu};
152  
153 # exit the loop as we found it
154 2 $loop = 0;
155 }
156  
157 3 $proc_int++;
158 } ## end while ( $loop && defined( $procs->[$proc_int]...))
159 } else {
160 18 $pctcpu = $object->pctcpu;
161 }
162  
163 # likely a dead connection that is handing around...
164 # or disappeared since grabbing the connection list
165 # and starting processing
166 21 ▶ if ( !defined($pctcpu) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pctcpu) 2 19 21
167 2 return 0;
168 }
169  
170 # handle non-numeric values, such as inf or nan, some platforms may return
171 19 ▶ if ( $pctcpu !~ /^\s*[0-9]*\.?[0-9]+\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pctcpu =~ /^\s*[0-9]*\.?[0-9]+\s*$/) 1 18 19
172 1 $pctcpu = 0;
173 }
174  
175 # use while as foreach will reference the value
176 19 my $pctcpu_int = 0;
177 19 ▶ while ( defined( $self->{pctcpus}[$pctcpu_int] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"pctcpus"}[$pctcpu_int]) 22 9 31
178 22 my $value = $self->{pctcpus}[$pctcpu_int];
179 22 ▶ if ( ( $value =~ /^[0-9]*\.?[0-9]+$/ )
Branch: 5 branches
BranchTrueFalseTotal
if ($value =~ /^[0-9]*\.?[0-9]+$/ and $value == $pctcpu) { } 2 20 22
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 == $pctcpu
!ll&&!rl&&r
1822
MC/DC100%2/2
$value =~ /^[0-9]*\.?[0-9]+$/ and $value == $pctcpu
$value =~ /^[0-9]*\.?[0-9]+$/ $value == $pctcpu
Truth table100%3/3
A and B
A= $value =~ /^[0-9]*\.?[0-9]+$/B= $value == $pctcpu
ABresult
0X0
100
111
180 && ( $value == $pctcpu ) )
181 {
182 2 return 1;
183 } elsif ( $value =~ /^\<\=[0-9]*\.?[0-9]+$/ ) {
184 3 $value =~ s/^\<\=//;
185 3 ▶ if ( $pctcpu <= $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pctcpu <= $value) 2 1 3
186 2 return 1;
187 }
188 } elsif ( $value =~ /^\<[0-9]*\.?[0-9]+$/ ) {
189 5 $value =~ s/^\<//;
190 5 ▶ if ( $pctcpu < $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pctcpu < $value) 2 3 5
191 2 return 1;
192 }
193 } elsif ( $value =~ /^\>\=[0-9]*\.?[0-9]+$/ ) {
194 3 $value =~ s/^\>\=//;
195 3 ▶ if ( $pctcpu >= $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pctcpu >= $value) 2 1 3
196 2 return 1;
197 }
198 } elsif ( $value =~ /^\>[0-9]*\.?[0-9]+$/ ) {
199 7 $value =~ s/^\>//;
200 7 ▶ if ( $pctcpu > $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pctcpu > $value) 2 5 7
201 2 return 1;
202 }
203 }
204 12 $pctcpu_int++;
205 } ## end while ( defined( $self->{pctcpus}[$pctcpu_int...]))
206  
207 9 return 0;
208 } ## end sub match
209  
210 =head1 AUTHOR
211  
212 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
213  
214 =head1 BUGS
215  
216 Please report any bugs or feature requests to C<bug-net-connection-match at rt.cpan.org>, or through
217 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Match>. I will be notified, and then you'll
218 automatically be notified of progress on your bug as I make changes.
219  
220  
221  
222  
223 =head1 SUPPORT
224  
225 You can find documentation for this module with the perldoc command.
226  
227 perldoc Net::Connection::Match
228  
229  
230 You can also look for information at:
231  
232 =over 4
233  
234 =item * RT: CPAN's request tracker (report bugs here)
235  
236 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Match>
237  
238 =item * AnnoCPAN: Annotated CPAN documentation
239  
240 L<http://annocpan.org/dist/Net-Connection-Match>
241  
242 =item * CPAN Ratings
243  
244 L<https://cpanratings.perl.org/d/Net-Connection-Match>
245  
246 =item * Search CPAN
247  
248 L<https://metacpan.org/release/Net-Connection-Match>
249  
250 =back
251  
252  
253 =head1 ACKNOWLEDGEMENTS
254  
255  
256 =head1 LICENSE AND COPYRIGHT
257  
258 Copyright 2019 Zane C. Bowers-Hadley.
259  
260 This program is free software; you can redistribute it and/or modify it
261 under the terms of the the Artistic License (2.0). You may obtain a
262 copy of the full license at:
263  
264 L<http://www.perlfoundation.org/artistic_license_2_0>
265  
266 Any use, modification, and distribution of the Standard or Modified
267 Versions is governed by this Artistic License. By using, modifying or
268 distributing the Package, you accept this license. Do not use, modify,
269 or distribute the Package, if you do not accept this license.
270  
271 If your Modified Version has been derived from a Modified Version made
272 by someone other than you, you are nevertheless required to ensure that
273 your Modified Version complies with the requirements of this license.
274  
275 This license does not grant you the right to use any trademark, service
276 mark, tradename, or logo of the Copyright Holder.
277  
278 This license includes the non-exclusive, worldwide, free-of-charge
279 patent license to make, have made, use, offer to sell, sell, import and
280 otherwise transfer the Package with respect to any patent claims
281 licensable by the Copyright Holder that are necessarily infringed by the
282 Package. If you institute patent litigation (including a cross-claim or
283 counterclaim) against any party alleging that the Package constitutes
284 direct or contributory patent infringement, then this Artistic License
285 to you shall terminate on the date that such litigation is filed.
286  
287 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
288 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
289 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
290 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
291 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
292 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
293 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
294 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295  
296  
297 =cut
298  
299 2 1; # End of Net::Connection::Match
« Match/PctMem.pm ↑ Summary Match/Command.pm »