blib/lib/SNMP/Trapinfo.pm

filter: Statementstmt 99.3% 159 / 160 Branchbran 98.6% 73 / 74 Conditioncond 91.6% 11 / 12 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 18 / 18 Podpod 54.5% 6 / 11 totaltotal 96.4% 272 / 282 CC 45 SCAR 20.9
CC
45
Cov
99%
expand
14.0
read
14.0
match_key
6.0
CRAP 8.1
↑ Summary
1 package SNMP::Trapinfo;
2  
3 1 ▶use 5.008004;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Safe; # Safe module, creates a compartment for eval's and tests for disabled commands
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '1.06';
10  
11 212 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=11.0
12 212 my $self = shift;
13 212 my $attr = our $AUTOLOAD;
14 212 $attr =~ s/.*:://;
15 212 ▶ return if $attr =~ /^DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $attr =~ /^DESTROY$/ 15 197 212
16 197 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 45 152 197
17 45 return $self->{$attr} = shift;
18 } else {
19 152 return $self->{$attr};
20 }
21 }
22  
23 16 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
24 16 my ($class, $data, $opts) = @_;
25 16 ▶ croak "Must specify data source (either GLOB or scalar ref)" unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 1 15 16
26 15 my $self = {
27 data => {},
28 P => [],
29 V => [],
30 };
31 15 $self = bless $self, $class;
32  
33 15 return $self->read($data, $opts);
34 }
35  
36 8 ▶sub trapname {
Subroutine trapname: called CC=2 SCAR=6.9
Pod documented
37 8 my $self = shift;
38 8 my $trapname = $self->data->{"SNMPv2-MIB::snmpTrapOID"};
39 8 ▶ return $trapname || undef;
Condition100%2/2
$trapname || undef
l!l
71
MC/DC100%1/1
$trapname || undef
$trapname
Truth table100%2/2
A
A= $trapname
Aresult
11
00
40 }
41  
42 3 ▶sub packet {
Subroutine packet: called CC=5 SCAR=16.3
Pod documented
43 3 my $self = shift;
44 3 ▶ if ($_[0] && ref \$_[0] eq "SCALAR") {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] and ref \$_[0] eq "SCALAR") 0 3 3
Condition66%2/3
$_[0] and ref \$_[0] eq "SCALAR"
!ll&&!rl&&r
120
MC/DC0%0/2
$_[0] and ref \$_[0] eq "SCALAR"
$_[0] ref \$_[0] eq "SCALAR"
Truth table66%2/3
A and B
A= $_[0]B= ref \$_[0] eq "SCALAR"
ABresult
0X0
100
111
45 0 return $self->{packet} = shift;
46 }
47 3 my $opts;
48 3 ▶ $opts = shift if (ref $_[0] eq "HASH");
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 2 1 3
49 3 $_ = $self->{packet};
50 3 ▶ if ($opts->{hide_passwords}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"hide_passwords"}) 2 1 3
51 2 $_ = $self->_hide_passwords( $_ );
52 }
53 3 return $_;
54 }
55  
56 4 ▶sub _hide_passwords {
Subroutine _hide_passwords: called CC=1 SCAR=0.0
57 4 my ($self, $string) = @_;
58 4 $string =~ s/\nSNMP-COMMUNITY-MIB::snmpTrapCommunity.0 "(.*?)"/\nSNMP-COMMUNITY-MIB::snmpTrapCommunity.0 "*****"/;
59 4 return $string;
60 }
61  
62 # Initialise the Safe compartment once
63 # Operators can be listed as follows
64 # perl -MOpcode=opdump -e 'opdump'
65 #
66 # http://search.cpan.org/~nwclark/perl-5.8.8/ext/Opcode/Opcode.pm
67 #
68 # We want to allow // m// s/// && || ! !~ != >= > == < <= =~ lt gt le ge ne eq not and or + - % * x .
69 #
70 1 my $cmp = new Safe;
71 1 $cmp->permit_only( qw( :base_core :base_mem :base_loop print sprintf prtf padsv padav padhv padany localtime rv2gv ) );
72  
73 73 ▶sub expand {
Subroutine expand: called CC=14 SCAR=26.4
Pod documented
74 73 my $self = shift;
75 73 my $string = shift;
76 73 ▶ return "" if ! defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 1 72 73
77 72 my $key;
78 72 ▶ while ( ($key) = ($string =~ /\$\{([\w\-\.\*:]+)}/) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($key) = $string =~ /\$\{([\w\-\.\*:]+)}/) 79 72 151
79 79 my $newval;
80 79 my ($action, $line) = $key =~ /^([PV])(\d+)?$/;
81 79 ▶ if ($action && $line) {
Branch: 4 branches
BranchTrueFalseTotal
if ($action and $line) { } 37 42 79
elsif ($key eq 'DUMP') { } 1 41 42
elsif ($key eq 'TRAPNAME') { } 3 38 41
elsif ($key eq 'HOSTIP') { } 1 37 38
Condition100%3/3
$action and $line
!ll&&!rl&&r
41137
MC/DC100%2/2
$action and $line
$action $line
Truth table100%3/3
A and B
A= $actionB= $line
ABresult
0X0
100
111
82 37 $newval = $self->$action($line);
83 37 ▶ if (!defined $newval) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $newval) 9 28 37
84 9 $newval = "(null)";
85 }
86 } elsif ($key eq "DUMP") {
87 1 my %h = %{$self->data};
88 1 delete $h{"SNMP-COMMUNITY-MIB::snmpTrapCommunity"};
89 1 $newval = join(" ", map {"$_=".$self->data->{$_}} (sort keys %h) );
90 } elsif ($key eq "TRAPNAME") {
91 3 $newval = $self->trapname;
92 } elsif ($key eq "HOSTIP") {
93 1 $newval = $self->hostip;
94 } else {
95 37 ▶ if ($key =~ /\*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\*/) { } 7 30 37
96 7 $newval = $self->match_key($key);
97 7 ▶ if (!defined $newval) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $newval) 1 6 7
98 1 $newval = "(null)";
99 }
100 } else {
101 30 $newval = $self->data->{$key};
102 30 ▶ if (!defined $newval) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $newval) 4 26 30
103 4 $newval = "(null)";
104 }
105 }
106 }
107  
108 # Must use same match as while loop
109 # Otherwise possible infinite loop
110 # though not sure why (see tests for examples)
111 #$string =~ s/\${$key}/$newval/;
112 79 $string =~ s/\$\{([\w\-\.\*:]+)\}/$newval/;
113 }
114  
115 # eval calculation performed within Safe for security
116 72 my $eval_re = qr/eval\s*{(.*?)}/;
117 72 ▶ while ( ($key) = ($string =~ /$eval_re/) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($key) = $string =~ /$eval_re/) 7 72 79
118 7 ▶ my $eval_result = $cmp->reval($key) || '';
Condition100%2/2
$cmp->reval($key) || ''
l!l
43
MC/DC100%1/1
$cmp->reval($key) || ''
$cmp->reval($key)
Truth table100%2/2
A
A= $cmp->reval($key)
Aresult
11
00
119 7 $string =~ s/$eval_re/$eval_result/;
120 }
121 72 return $string;
122 }
123  
124 22 ▶sub eval {
Subroutine eval: called CC=3 SCAR=11.0
Pod documented
125 22 my ($self, $string) = @_;
126 22 my $code = $self->expand($string);
127 22 $self->last_eval_string($code);
128  
129 22 my $rc = $cmp->reval("$code", 1); # ($code to run, 1 for 'use strict;')
130 22 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 4 18 22
131 4 return undef;
132 } else {
133 18 ▶ return $rc ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rc ? : 12 6 18
134 }
135 }
136  
137 12 ▶sub match_key {
Subroutine match_key: called CC=6 SCAR=17.9
Pod undocumented
138 12 my ($self, $key) = @_;
139 12 my @parts = split('\.', $key);
140 12 POSSIBLE: foreach my $possible (keys %{$self->data}) {
141 91 my @possible = split('\.', $possible);
142 91 ▶ next unless @possible == @parts;
Branch: 1 branch
BranchTrueFalseTotal
unless @possible == @parts 42 49 91
143 49 ▶ for (my $i=0; $i < @parts; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @parts) 66 7 73
144 66 ▶ next if ($parts[$i] eq "*");
Branch: 1 branch
BranchTrueFalseTotal
if $parts[$i] eq "*" 9 57 66
145 57 ▶ if ($parts[$i] ne $possible[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[$i] ne $possible[$i]) 42 15 57
146 42 next POSSIBLE;
147 }
148 }
149 7 return $self->data->{$possible};
150 }
151 5 return undef;
152 }
153  
154 140 ▶sub cleanup_string {
Subroutine cleanup_string: called CC=3 SCAR=11.0
Pod undocumented
155 140 my $self = shift;
156 140 my $string = shift;
157 # This is an SNMP OID name...
158 140 ▶ if ($string =~ /^[A-Za-z].*\:\:[A-Za-z].*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^[A-Za-z].*\:\:[A-Za-z].*$/) 75 65 140
159 # Drop single trailing digits
160 75 ▶ if (! ($string =~ /\d\.\d+$/)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($string =~ /\d\.\d+$/) 69 6 75
161 69 $string =~ s/\.\d+$//;
162 }
163 }
164 # Remove trailing spaces
165 140 $string =~ s/ +$//;
166 140 return $string;
167 }
168  
169 15 ▶sub read {
Subroutine read: called CC=14 SCAR=26.4
Pod undocumented
170 15 my ($self, $data, $opts) = @_;
171 15 ▶ if (ref \$data eq "GLOB") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref \$data eq 'GLOB') { } 3 12 15
elsif (ref \$data eq 'REF') { } 10 2 12
172 3 local $/="#---next trap---#\n";
173 3 ▶ $self->{packet} = <$data> || return undef;
Condition100%2/2
readline $data || (return undef)
l!l
21
MC/DC100%1/1
readline $data || (return undef)
readline $data
Truth table100%2/2
A
A= readline $data
Aresult
11
00
174 2 chomp($self->{packet});
175 } elsif (ref \$data eq "REF") {
176 10 $self->{packet} = $$data;
177 } else {
178 2 croak "Bad ref";
179 }
180 12 ▶ if ($opts->{hide_passwords}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"hide_passwords"}) 2 10 12
181 2 $self->{packet} = $self->_hide_passwords( $self->{packet} );
182 }
183 12 $self->{packet} =~ s/\n*$//;
184 12 my @packet = split("\n", $self->{packet});
185 {
186 # Go through the array and look for lines that might be joinable.
187 # Assume multi-lines are surrounded by quotes, so only one quote
188 # character means join the next line onto the current one
189  
190 # Work from a copy of the packet in case we decide to make no changes
191 12 my @copy_packet = @packet;
192 12 my @new_packet;
193 12 my $within_quotes=0;
194 # use defined here to allow blank lines through
195 12 ▶ while( defined( my $line = shift @copy_packet)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = shift @copy_packet)) 104 12 116
196 104 ▶ if($within_quotes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($within_quotes) 5 99 104
197 5 $new_packet[-1] .= "\n".$line;
198 5 my $quotes = $new_packet[-1] =~ tr/"/"/;
199 5 ▶ $within_quotes = 0 if( $quotes % 2 == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $quotes % 2 == 0 1 4 5
200 5 next;
201 }
202  
203 99 push(@new_packet, $line);
204  
205 {
206 99 my $quotes = $new_packet[-1] =~ tr/"/"/;
207 99 ▶ $within_quotes = 1 if( $quotes % 2 == 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $quotes % 2 == 1 2 97 99
208 }
209 }
210  
211 # Only rewrite the packet if it looks like we have correctly
212 # joined up all the lines
213 12 ▶ @packet=@new_packet if($within_quotes == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $within_quotes == 0 11 1 12
214 }
215 12 chomp($_ = shift @packet);
216 12 $self->hostname($_);
217 12 $self->{P}->[0] = $_;
218  
219 12 ▶ return undef if (!@packet); # No IP address given. This is a malformed packet
Branch: 1 branch
BranchTrueFalseTotal
unless @packet 1 11 12
220  
221 11 chomp($_ = shift @packet);
222 11 $self->{P}->[1] = $_;
223 # Extra stuff around the IP packet in Net-SNMP 5.2.1
224 11 s/^.*?\[//;
225 11 s/\].*$//;
226 11 $self->hostip($_);
227  
228 11 my $i = 1; # Start at 1 because want to increment array at beginning because of next
229 11 foreach $_ (@packet) {
230 77 $i++;
231 # Ignore spaces in middle
232 77 my ($key, $value) = /^([^ ]+) +([^ ].*)$/s;
233 # If syntax is wrong, ignore this line
234 77 ▶ next unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 3 74 77
235 74 $key = $self->cleanup_string($key);
236 74 ▶ if ($key ne "SNMPv2-MIB::snmpTrapOID") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key ne "SNMPv2-MIB::snmpTrapOID") 66 8 74
237 66 $value = $self->cleanup_string($value);
238 }
239 74 $self->data->{$key} = $value;
240 74 $key =~ s/^[^:]+:://;
241 74 $self->{P}->[$i] = $key;
242 74 $self->{V}->[$i] = $value;
243 }
244 11 return $self;
245 }
246  
247 2 ▶sub fully_translated {
Subroutine fully_translated: called CC=2 SCAR=6.9
Pod documented
248 2 my $self = shift;
249 2 ▶ if ($self->trapname =~ /\.\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->trapname =~ /\.\d+$/) { } 1 1 2
250 1 return 0;
251 } else {
252 1 return 1;
253 }
254 }
255  
256 16 ▶sub P {
Subroutine P: called CC=1 SCAR=0.0
Pod undocumented
257 16 my ($self, $line) = @_;
258 16 $_ = $self->{P}->[--$line];
259 16 return $_;
260 }
261  
262 29 ▶sub V {
Subroutine V: called CC=1 SCAR=0.0
Pod undocumented
263 29 my ($self, $line) = @_;
264 29 $_ = $self->{V}->[--$line];
265 29 return $_;
266 }
267  
268 1 1;
269 __END__
↑ Summary