Match.pm

filter: Statementstmt 100% 78 / 78 Branchbran 97.2% 35 / 36 Conditioncond 91.6% 11 / 12 MC/DCmcdc 87.5% 7 / 8 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 97.8% 138 / 141 CC 24 SCAR 25.0
CC
24
Cov
98%
new
16.0
match
9.0
BEGIN
1.0
CRAP 12.1
« Match/Start.pm ↑ Summary Match/Swapped.pm »
1 package Proc::ProcessTable::Match;
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  
7 =head1 NAME
8  
9 Proc::ProcessTable::Match - Matches a Proc::ProcessTable::Process against a stack of checks.
10  
11 =head1 VERSION
12  
13 Version 0.1.0
14  
15 =cut
16  
17 2 our $VERSION = '0.1.0';
18  
19  
20 =head1 SYNOPSIS
21  
22 use Proc::ProcessTable::Match;
23 use Proc::ProcessTable;
24 use Data::Dumper;
25
26 # looks for a kernel proc with the PID of 0
27 my %args=(
28 checks=>[
29 {
30 type=>'PID',
31 invert=>0,
32 args=>{
33 pids=>['0'],
34 }
35 },{
36 type=>'KernProc',
37 invert=>0,
38 args=>{
39 }
40 }
41 ]
42 );
43
44 # hits on every proc but the idle proc
45 %args=(
46 checks=>[
47 {
48 type=>'Idle',
49 invert=>1,
50 args=>{
51 }
52 }
53 ]
54 );
55
56 my $ppm;
57 eval{
58 $ppm=Proc::ProcessTable::Match->new( \%args );
59 } or die "New failed with...".$@;
60
61 my $pt = Proc::ProcessTable->new;
62 foreach my $proc ( @{$pt->table} ){
63 if ( $ppm->match( $proc ) ){
64 print Dumper( $proc );
65 }
66 }
67  
68 =head1 METHODS
69  
70 =head2 new
71  
72 This ininitates the object.
73  
74 One argument is taken and it is a hashref with the key "checks".
75 That value needs to contains a array of hashs of checks to run.
76  
77 =head3 checks hash
78  
79 Every check must hit for it to beconsidered a match.
80  
81 Each of these should always be defined.
82  
83 =head4 type
84  
85 This is the module to use to use to run the check.
86  
87 The name is relative 'Proc::ProcessTable::Match::', so
88 'PID' becomes 'Proc::ProcessTable::Match::PID'.
89  
90 =head4 invert
91  
92 This inverts inverts the returned value from the check.
93  
94 =head4 args
95  
96 This is a hash that will be pashed to the checker module's
97 new method.
98  
99 For any required keys, check the documents for that checker.
100  
101 If it does not take any arguments, just pass a blank hash.
102  
103 my %args=(
104 checks=>[
105 {
106 type=>'PID',
107 invert=>0,
108 args=>{
109 pids=>['0'],
110 }
111 },{
112 type=>'KernProc',
113 invert=>0,
114 args=>{
115 }
116 }
117 ]
118 );
119
120 my $ppm;
121 eval{
122 $ppm=Proc::ProcessTable::Match->new( \%args );
123 } or die "New failed with...".$@;
124  
125 =cut
126  
127 14 ▶sub new{
Subroutine new: called CC=16 SCAR=27.7
Pod documented
128 14 my %args;
129 14 ▶ if(defined($_[1])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 13 1 14
130 13 %args= %{$_[1]};
131 };
132  
133 # Provides some basic checks.
134 # Could make these all one if, but this provides more
135 # granularity for some one using it.
136 14 ▶ if ( ! defined( $args{checks} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"checks"}) 2 12 14
137 2 die ('No check key specified in the argument hash');
138 }
139 12 ▶ if ( ref( $args{checks} ) ne 'ARRAY' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"checks"} ne "ARRAY") 1 11 12
140 1 die ('The checks key is not a array');
141 }
142 # Will never match anything.
143 11 ▶ if ( ! defined $args{checks}[0] ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"checks"}[0]) 1 10 11
144 1 die ('Nothing in the checks array');
145 }
146 10 ▶ if ( ref( $args{checks}[0] ) ne 'HASH' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"checks"}[0] ne "HASH") 1 9 10
147 1 die ('The first item in the checks array is not a hash');
148 }
149  
150 9 my $class=$_[0];
151 9 my $self = {
152 checks=>[],
153 };
154 9 bless $self, $class;
155  
156 # will hold the created check objects
157 9 my @checks;
158  
159 # Loads up each check or dies if it fails to.
160 9 my $check_int=0;
161 9 ▶ while( defined( $args{checks}[$check_int] ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"checks"}[$check_int]) 10 3 13
162 10 my %new_check=(
163 type=>undef,
164 args=>undef,
165 invert=>undef,
166 );
167  
168 # make sure we have a check type
169 10 ▶ if ( defined($args{checks}[$check_int]{'type'}) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'checks'}[$check_int]{'type'}) { } 9 1 10
170 9 $new_check{type}=$args{checks}[$check_int]{'type'};
171 }else{
172 1 die('No type defined for check '.$check_int);
173 }
174  
175 # does a quick check on the tpye name
176 9 my $type_test=$new_check{type};
177 9 $type_test=~s/[A-Za-z0-9]//g;
178 9 $type_test=~s/\:\://g;
179 9 ▶ if ( $type_test ne '' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($type_test ne "") 1 8 9
180 1 die 'The type "'.$new_check{type}.'" for check '.$check_int.' is not a valid check name';
181 }
182  
183 # makes sure we have a args object and that it is a hash
184 8 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'checks'}[$check_int]{'args'} and ref $args{'checks'}[$check_int]{'args'} eq 'HASH') { } 6 2 8
Condition100%3/3
defined $args{"checks"}[$check_int]{"args"} and ref $args{"checks"}[$check_int]{"args"} eq "HASH"
!ll&&!rl&&r
116
MC/DC100%2/2
defined $args{"checks"}[$check_int]{"args"} and ref $args{"checks"}[$check_int]{"args"} eq "HASH"
defined $args{"checks"}[$check_int]{"args"} ref $args{"checks"}[$check_int]{"args"} eq "HASH"
Truth table100%3/3
A and B
A= defined $args{"checks"}[$check_int]{"args"}B= ref $args{"checks"}[$check_int]{"args"} eq "HASH"
ABresult
0X0
100
111
185 ( defined($args{checks}[$check_int]{'args'}) ) &&
186 ( ref( $args{checks}[$check_int]{'args'} ) eq 'HASH' )
187 ){
188 6 $new_check{args}=$args{checks}[$check_int]{'args'};
189 }else{
190 2 die('No args defined for check '.$check_int.' or it is not a HASH');
191 }
192  
193 # makes sure we have a args object and that it is a hash
194 6 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{'checks'}[$check_int]{'invert'} and ref \$args{'checks'}[$check_int]{'invert'} ne 'SCALAR') { } 1 5 6
elsif (defined $args{'checks'}[$check_int]{'invert'} and ref \$args{'checks'}[$check_int]{'invert'} eq 'SCALAR') { } 3 4 7
Condition100%3/3
defined $args{"checks"}[$check_int]{"invert"} and ref \$args{"checks"}[$check_int]{"invert"} ne "SCALAR"
!ll&&!rl&&r
231
MC/DC100%2/2
defined $args{"checks"}[$check_int]{"invert"} and ref \$args{"checks"}[$check_int]{"invert"} ne "SCALAR"
defined $args{"checks"}[$check_int]{"invert"} ref \$args{"checks"}[$check_int]{"invert"} ne "SCALAR"
Truth table100%3/3
A and B
A= defined $args{"checks"}[$check_int]{"invert"}B= ref \$args{"checks"}[$check_int]{"invert"} ne "SCALAR"
ABresult
0X0
100
111
195 ( defined($args{checks}[$check_int]{'invert'}) ) &&
196 ( ref( \$args{checks}[$check_int]{'invert'} ) ne 'SCALAR' )
197 ){
198 1 die('Invert defined for check '.$check_int.' but it is not a SCALAR');
199 ▶ }elsif(
Condition66%2/3
defined $args{"checks"}[$check_int]{"invert"} and ref \$args{"checks"}[$check_int]{"invert"} eq "SCALAR"
!ll&&!rl&&r
203
MC/DC50%1/2
defined $args{"checks"}[$check_int]{"invert"} and ref \$args{"checks"}[$check_int]{"invert"} eq "SCALAR"
defined $args{"checks"}[$check_int]{"invert"} ref \$args{"checks"}[$check_int]{"invert"} eq "SCALAR"
Truth table66%2/3
A and B
A= defined $args{"checks"}[$check_int]{"invert"}B= ref \$args{"checks"}[$check_int]{"invert"} eq "SCALAR"
ABresult
0X0
100
111
200 ( defined($args{checks}[$check_int]{'invert'}) ) &&
201 ( ref( \$args{checks}[$check_int]{'invert'} ) eq 'SCALAR' )
202 ){
203 3 $new_check{invert}=$args{checks}[$check_int]{'invert'};
204 }
205  
206 5 my $check;
207 5 my $check_module='Proc::ProcessTable::Match::'.$new_check{type};
208 5 my $check_module_file=$check_module;
209 5 $check_module_file=~s/\:\:/\//g;
210 5 $check_module_file=$check_module_file.'.pm';
211 5 eval{
212 5 require $check_module_file;
213 4 $check=$check_module->new( $new_check{args} );
214 };
215  
216 5 ▶ if (!defined( $check )){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $check) 1 4 5
217 1 die 'Failed to init the check for '.$check_int.' as it returned undef... '.$@;
218 }
219  
220 4 $new_check{check}=$check;
221  
222 4 push(@{ $self->{checks} }, \%new_check );
223  
224 4 $check_int++;
225 }
226  
227 3 return $self;
228 }
229  
230 =head2 match
231  
232 Checks if a single Proc::ProcessTable::Process object matches the stack.
233  
234 One argument is taken and that is the Proc::ProcessTable::Process to check.
235  
236 The return value is a boolean.
237  
238 If the passed object is undefined or not a Proc::ProcessTable::Process,
239 this method dies.
240  
241 if ( $ppm->match( $proc ) ){
242 print "It matched.\n";
243 }
244  
245 =cut
246  
247 9 ▶sub match{
Subroutine match: called CC=9 SCAR=22.0
Pod documented
248 9 my $self=$_[0];
249 9 my $proc=$_[1];
250  
251 9 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $proc or ref $proc ne "Proc::ProcessTable::Process") 2 7 9
Condition100%3/3
not defined $proc or ref $proc ne "Proc::ProcessTable::Process"
l!l&&r!l&&!r
117
MC/DC100%2/2
not defined $proc or ref $proc ne "Proc::ProcessTable::Process"
not defined $proc ref $proc ne "Proc::ProcessTable::Process"
Truth table100%3/3
not(A) or B
A= not defined $procB= ref $proc ne "Proc::ProcessTable::Process"
ABresult
1X1
011
000
252 ( ! defined( $proc ) ) ||
253 ( ref( $proc ) ne 'Proc::ProcessTable::Process' )
254 ){
255 2 die('Either the process is undefined or is not a Proc::ProcessTable::Process object');
256 }
257  
258 # Stores the number of hits
259 7 my $hits=0;
260 7 my $required=0;
261 7 foreach my $check ( @{ $self->{checks} } ){
262 10 my $hit;
263 10 eval{
264 10 $hit=$check->{check}->match($proc);
265 };
266  
267 # If $hit is undef, then one of the checks errored and we skip processing the results.
268 # Should only be 0 or 1.
269 10 ▶ if ( defined( $hit ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hit) 10 0 10
270 # invert if needed
271 10 ▶ if ( $check->{invert} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($check->{"invert"}) 2 8 10
272 2 ▶ $hit = $hit ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$hit ? : 1 1 2
273 }
274  
275 # increment the hits count if we hit
276 10 ▶ if ( $hit ){
Branch: 1 branch
BranchTrueFalseTotal
if ($hit) 5 5 10
277 5 $hits++;
278 }
279 }
280  
281 10 $required++;
282 }
283  
284 # if these are the same, then we have a match
285 7 ▶ if ( $required == $hits ){
Branch: 1 branch
BranchTrueFalseTotal
if ($required == $hits) 3 4 7
286 3 return 1;
287 }
288  
289 # If we get here, it is not a match
290 4 return 0;
291 }
292  
293 =head1 AUTHOR
294  
295 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
296  
297 =head1 BUGS
298  
299 Please report any bugs or feature requests to C<bug-proc-processtable-match at rt.cpan.org>, or through
300 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Proc-ProcessTable-Match>. I will be notified, and then you'll
301 automatically be notified of progress on your bug as I make changes.
302  
303  
304  
305  
306 =head1 SUPPORT
307  
308 You can find documentation for this module with the perldoc command.
309  
310 perldoc Proc::ProcessTable::Match
311  
312  
313 You can also look for information at:
314  
315 =over 4
316  
317 =item * RT: CPAN's request tracker (report bugs here)
318  
319 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Proc-ProcessTable-Match>
320  
321 =item * Search CPAN
322  
323 L<https://metacpan.org/release/Proc-ProcessTable-Match>
324  
325 =back
326  
327  
328 =head1 ACKNOWLEDGEMENTS
329  
330  
331 =head1 LICENSE AND COPYRIGHT
332  
333 This software is Copyright (c) 2019 by Zane C. Bowers-Hadley.
334  
335 This is free software, licensed under:
336  
337 The Artistic License 2.0 (GPL Compatible)
338  
339  
340 =cut
341  
342 2 1; # End of Proc::ProcessTable::Match
« Match/Start.pm ↑ Summary Match/Swapped.pm »