File Coverage

blib/lib/Net/Connection/Match/PctCPU.pm
Criterion Covered Total %
statement 11 67 16.4
branch 0 38 0.0
condition 0 6 0.0
subroutine 4 6 66.6
pod 2 2 100.0
total 17 119 14.2


line stmt bran cond sub pod time code
1             package Net::Connection::Match::PctCPU;
2              
3 1     1   64827 use 5.006;
  1         13  
4 1     1   5 use strict;
  1         2  
  1         21  
5 1     1   5 use warnings;
  1         2  
  1         35  
6 1     1   434 use Proc::ProcessTable;
  1         4709  
  1         633  
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             our $VERSION = '0.0.0';
19              
20              
21             =head1 SYNOPSIS
22              
23             use Net::Connection::Match::PctCPU;
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             pctcpu=>'5.03',
35             };
36            
37             my $conn=Net::Connection->new( $connection_args );
38            
39             my %args=(
40             pctcpus=>[
41             '>1',
42             ],
43             );
44            
45             my $checker=Net::Connection::Match::PctCPU->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 'pctcpus', which is a array of pctcpu value.
59              
60             The pctcpu 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             pctcpus=>[
75             '>1',
76             ],
77             );
78            
79             my $checker=Net::Connection::Match::PctCPU->new( \%args );
80              
81             =cut
82              
83             sub new{
84 0     0 1   my %args;
85 0 0         if(defined($_[1])){
86 0           %args= %{$_[1]};
  0            
87             };
88              
89             # run some basic checks to make sure we have the minimum stuff required to work
90 0 0         if ( ! defined( $args{pctcpus} ) ){
91 0           die ('No pctcpus key specified in the argument hash');
92             }
93 0 0         if ( ref( \$args{pctcpus} ) eq 'ARRAY' ){
94 0           die ('The pctcpus key is not a array');
95             }
96 0 0         if ( ! defined $args{pctcpus}[0] ){
97 0           die ('Nothing defined in the pctcpus array');
98             }
99              
100             my $self = {
101             pctcpus=>$args{pctcpus},
102 0           };
103 0           bless $self;
104              
105 0           return $self;
106             }
107              
108             =head2 match
109              
110             Checks if a single Net::Connection object matches the stack.
111              
112             One argument is taken and that is a Net::Connection object.
113              
114             The returned value is a boolean.
115              
116             if ( $checker->match( $conn ) ){
117             print "The connection matches.\n";
118             }
119              
120             =cut
121              
122             sub match{
123 0     0 1   my $self=$_[0];
124 0           my $object=$_[1];
125              
126 0 0         if ( !defined( $object ) ){
127 0           return 0;
128             }
129              
130 0 0         if ( ref( $object ) ne 'Net::Connection' ){
131 0           return 0;
132             }
133              
134 0           my $conn_pid=$object->pid;
135              
136             # don't bother proceeding, the object won't match ever
137             # as it does not have a PID
138 0 0         if ( ! defined( $conn_pid ) ){
139 0           return 0;
140             }
141              
142              
143 0           my $loop=0;
144 0           my $pctcpu;
145 0 0         if ( ! defined( $object->proc ) ){
146             # go through each proc and look for a matching pid
147 0           my $proctable=Proc::ProcessTable->new;
148 0           my $procs=$proctable->table;
149 0           my $proc_int=0;
150 0           my $loop=1;
151 0   0       while (
152             $loop &&
153             defined( $procs->[$proc_int] )
154             ){
155              
156 0 0         if ( $conn_pid eq $procs->[$proc_int]->{pid} ){
157 0           $pctcpu=$procs->[$proc_int]->{pctcpu};
158              
159             # exit the loop as we found it
160 0           $loop=0;
161             }
162              
163 0           $proc_int++;
164             }
165             }else{
166 0           $pctcpu=$object->pctcpu;
167             }
168              
169             # likely a dead connection that is handing around...
170             # or disappeared since grabbing the connection list
171             # and starting processing
172 0 0         if ( !defined( $pctcpu ) ){
173 0           return 0;
174             }
175              
176             # use while as foreach will reference the value
177 0           my $pctcpu_int=0;
178 0           while (defined( $self->{pctcpus}[$pctcpu_int] )){
179 0           my $value=$self->{pctcpus}[$pctcpu_int];
180 0 0 0       if (
    0          
    0          
    0          
    0          
181             ( $value =~ /^[0-9.]+$/ ) &&
182             ( $value eq $pctcpu )
183             ){
184 0           return 1;
185             }elsif( $value =~ /^\<\=[0-9.]+$/ ){
186 0           $value=~s/^\<\=//;
187 0 0         if ( $value <= $pctcpu ){
188 0           return 1;
189             }
190             }elsif( $value =~ /^\<[0-9.]+$/ ){
191 0           $value=~s/^\
192 0 0         if ( $pctcpu < $value ){
193 0           return 1;
194             }
195             }elsif( $value =~ /^\>\=[0-9.]+$/ ){
196 0           $value=~s/^\>\=//;
197 0 0         if ( $pctcpu >= $value ){
198 0           return 1;
199             }
200             }elsif( $value =~ /^\>[0-9.]+$/ ){
201 0           $value=~s/^\>//;
202 0 0         if ( $pctcpu > $value ){
203 0           return 1;
204             }
205             }
206 0           $pctcpu_int++;
207             }
208              
209 0           return 0;
210             }
211              
212             =head1 AUTHOR
213              
214             Zane C. Bowers-Hadley, C<< >>
215              
216             =head1 BUGS
217              
218             Please report any bugs or feature requests to C, or through
219             the web interface at L. I will be notified, and then you'll
220             automatically be notified of progress on your bug as I make changes.
221              
222              
223              
224              
225             =head1 SUPPORT
226              
227             You can find documentation for this module with the perldoc command.
228              
229             perldoc Net::Connection::Match
230              
231              
232             You can also look for information at:
233              
234             =over 4
235              
236             =item * RT: CPAN's request tracker (report bugs here)
237              
238             L
239              
240             =item * AnnoCPAN: Annotated CPAN documentation
241              
242             L
243              
244             =item * CPAN Ratings
245              
246             L
247              
248             =item * Search CPAN
249              
250             L
251              
252             =back
253              
254              
255             =head1 ACKNOWLEDGEMENTS
256              
257              
258             =head1 LICENSE AND COPYRIGHT
259              
260             Copyright 2019 Zane C. Bowers-Hadley.
261              
262             This program is free software; you can redistribute it and/or modify it
263             under the terms of the the Artistic License (2.0). You may obtain a
264             copy of the full license at:
265              
266             L
267              
268             Any use, modification, and distribution of the Standard or Modified
269             Versions is governed by this Artistic License. By using, modifying or
270             distributing the Package, you accept this license. Do not use, modify,
271             or distribute the Package, if you do not accept this license.
272              
273             If your Modified Version has been derived from a Modified Version made
274             by someone other than you, you are nevertheless required to ensure that
275             your Modified Version complies with the requirements of this license.
276              
277             This license does not grant you the right to use any trademark, service
278             mark, tradename, or logo of the Copyright Holder.
279              
280             This license includes the non-exclusive, worldwide, free-of-charge
281             patent license to make, have made, use, offer to sell, sell, import and
282             otherwise transfer the Package with respect to any patent claims
283             licensable by the Copyright Holder that are necessarily infringed by the
284             Package. If you institute patent litigation (including a cross-claim or
285             counterclaim) against any party alleging that the Package constitutes
286             direct or contributory patent infringement, then this Artistic License
287             to you shall terminate on the date that such litigation is filed.
288              
289             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
290             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
291             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
292             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
293             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
294             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
295             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
296             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
297              
298              
299             =cut
300              
301             1; # End of Net::Connection::Match