lib/Rex/Inventory/DMIDecode.pm

filter: Statementstmt 88.3% 129 / 146 Branchbran 77.5% 31 / 40 Conditioncond 55.5% 5 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 8 totaltotal 81.2% 186 / 229 CC 28 SCAR 25.5
CC
28
Cov
85%
_read_dmidecode
26.6
get_cpus
3.0
get_memory_modules
3.0
CRAP 12.8
« lib/Rex/Pkg/SunOS/pkg.pm ↑ Summary blib/lib/Rex/Output/JUnit.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Inventory::DMIDecode;
6  
7 40 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 our $VERSION = '1.16.1'; # VERSION
12  
13 40 ▶use Rex::Inventory::DMIDecode::BaseBoard;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 40 ▶use Rex::Inventory::DMIDecode::Bios;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 40 ▶use Rex::Inventory::DMIDecode::CPU;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 40 ▶use Rex::Inventory::DMIDecode::Memory;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 40 ▶use Rex::Inventory::DMIDecode::MemoryArray;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 40 ▶use Rex::Inventory::DMIDecode::SystemInformation;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 40 ▶use Rex::Commands::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 40 ▶use Rex::Helper::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 54 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
23 54 my $that = shift;
24 54 ▶ my $proto = ref($that) || $that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
0540
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
25 54 my $self = {@_};
26  
27 54 bless( $self, $proto );
28  
29 54 $self->_read_dmidecode();
30  
31 54 return $self;
32 }
33  
34 122 ▶sub get_tree {
Subroutine get_tree: called CC=2 SCAR=7.6
Pod undocumented
35 122 my ( $self, $section ) = @_;
36  
37 122 ▶ if ($section) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section) 122 0 122
38 122 return $self->{"__dmi"}->{$section};
39 }
40  
41 0 return $self->{"__dmi"};
42 }
43  
44 3 ▶sub get_base_board {
Subroutine get_base_board: called CC=1 SCAR=0.0
Pod undocumented
45 3 my ($self) = @_;
46  
47 3 return Rex::Inventory::DMIDecode::BaseBoard->new( dmi => $self );
48 }
49  
50 3 ▶sub get_bios {
Subroutine get_bios: called CC=1 SCAR=0.0
Pod undocumented
51 3 my ($self) = @_;
52  
53 3 return Rex::Inventory::DMIDecode::Bios->new( dmi => $self );
54 }
55  
56 54 ▶sub get_system_information {
Subroutine get_system_information: called CC=1 SCAR=0.0
Pod undocumented
57 54 my ($self) = @_;
58  
59 54 return Rex::Inventory::DMIDecode::SystemInformation->new( dmi => $self );
60 }
61  
62 3 ▶sub get_cpus {
Subroutine get_cpus: called CC=3 SCAR=11.0
Pod undocumented
63  
64 3 my ($self) = @_;
65 3 my @cpus = ();
66 3 my $tree = $self->get_tree("Processor Information");
67 3 my $idx = 0;
68 3 for my $cpu ( @{$tree} ) {
69 3 ▶ if ( $cpu->{"Status"} =~ m/Populated/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cpu->{"Status"} =~ /Populated/u) 3 0 3
70 3 push( @cpus,
71 Rex::Inventory::DMIDecode::CPU->new( dmi => $self, index => $idx ) );
72 }
73 3 ++$idx;
74 }
75  
76 3 return @cpus;
77  
78 }
79  
80 3 ▶sub get_memory_modules {
Subroutine get_memory_modules: called CC=3 SCAR=11.0
Pod undocumented
81  
82 3 my ($self) = @_;
83 3 my @mems = ();
84 3 my $tree = $self->get_tree("Memory Device");
85 3 my $idx = 0;
86 3 for my $mem ( @{$tree} ) {
87 12 ▶ if ( $mem->{"Size"} =~ m/\d+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mem->{"Size"} =~ /\d+/u) 3 9 12
88 3 push( @mems,
89 Rex::Inventory::DMIDecode::Memory->new( dmi => $self, index => $idx ) );
90 }
91 12 ++$idx;
92 }
93  
94 3 return @mems;
95  
96 }
97  
98 3 ▶sub get_memory_arrays {
Subroutine get_memory_arrays: called CC=2 SCAR=6.9
Pod undocumented
99  
100 3 my ($self) = @_;
101 3 my @mems = ();
102 3 my $tree = $self->get_tree("Physical Memory Array");
103 3 my $idx = 0;
104 3 for my $mema ( @{$tree} ) {
105 3 push(
106 @mems,
107 Rex::Inventory::DMIDecode::MemoryArray->new(
108 dmi => $self,
109 index => $idx
110 )
111 );
112 3 ++$idx;
113 }
114  
115 3 return @mems;
116  
117 }
118  
119 54 ▶sub _read_dmidecode {
Subroutine _read_dmidecode: called CC=21 SCAR=32.8
120  
121 54 my ($self) = @_;
122  
123 54 my @lines;
124 54 ▶ if ( $self->{lines} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'lines'}) { } 3 51 54
125 3 @lines = @{ $self->{lines} };
126 }
127 else {
128 51 ▶ unless ( can_run("dmidecode") ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (can_run("dmidecode")) 51 0 51
129 51 Rex::Logger::debug("Please install dmidecode on the target system.");
130 51 return;
131 }
132  
133 0 eval { @lines = i_run "dmidecode"; };
134  
135 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
136 0 Rex::Logger::debug("Error running dmidecode");
137 0 return;
138 }
139 }
140 3 chomp @lines;
141  
142 3 my %section = ();
143 3 my $section = "";
144 3 my $new_section = 0;
145 3 my $sub_section = "";
146  
147 3 for my $l (@lines) {
148  
149 735 ▶ next if $l =~ m/^Handle/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /^Handle/u 60 675 735
150 675 ▶ next if $l =~ m/^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /^#/u 3 672 675
151 672 ▶ next if $l =~ m/^SMBIOS/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /^SMBIOS/u 3 669 672
152 669 ▶ next if $l =~ m/^$/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /^$/u 60 609 669
153 609 ▶ last if $l =~ m/^End Of Table$/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /^End Of Table$/u 3 606 609
154  
155 # for openbsd
156 606 $l =~ s/ /\t/g;
157  
158 606 ▶ unless ( substr( $l, 0, 1 ) eq "\t" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (substr($l, 0, 1) eq "\t") 66 540 606
159 66 $section = $l;
160 66 $new_section = 1;
161 66 next;
162 }
163  
164 540 my $line = $l;
165 540 $line =~ s/^\t+//g;
166 540 $line =~ s/\s+$//g;
167  
168 540 ▶ next if $l =~ m/^$/;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /^$/u 0 540 540
169  
170 540 ▶ if ( $l =~ m/^\t[a-zA-Z0-9]/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($l =~ /^\t[a-zA-Z0-9]/u) { } 489 51 540
elsif ($l =~ /^\t\t[a-zA-Z0-9]/u) { } 51 0 51
171 489 ▶ if ( exists $section{$section} && !ref( $section{$section} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $section{$section} and not ref $section{$section}) { } 0 489 489
elsif (exists $section{$section} and ref $section{$section}) { } 453 72 525
Condition66%2/3
exists $section{$section} and not ref $section{$section}
!ll&&!rl&&r
364530
MC/DC0%0/2
exists $section{$section} and not ref $section{$section}
exists $section{$section} not ref $section{$section}
Truth table66%2/3
A and not(B)
A= exists $section{$section}B= not ref $section{$section}
ABresult
0X0
100
111
172 0 my $content = $section{$section};
173 0 $section{$section} = [];
174 0 my @arr = ();
175 0 my ( $key, $val ) = split( /: /, $line, 2 );
176 0 $key =~ s/:$//;
177 0 $sub_section = $key;
178  
179 #push (@{$section{$section}}, $content);
180 0 push( @{ $section{$section} }, { $key => $val } );
181 0 $new_section = 0;
182 0 next;
183 }
184 ▶ elsif ( exists $section{$section} && ref( $section{$section} ) ) {
Condition66%2/3
exists $section{$section} and ref $section{$section}
!ll&&!rl&&r
360453
MC/DC50%1/2
exists $section{$section} and ref $section{$section}
exists $section{$section} ref $section{$section}
Truth table66%2/3
A and B
A= exists $section{$section}B= ref $section{$section}
ABresult
0X0
100
111
185 453 ▶ if ($new_section) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_section) 24 429 453
186 24 push( @{ $section{$section} }, {} );
187 24 $new_section = 0;
188 }
189 453 my ( $key, $val ) = split( /: /, $line, 2 );
190 453 $key =~ s/:$//;
191 453 $sub_section = $key;
192 453 my $href = $section{$section}->[-1];
193  
194 #push (@{$section{$section}}, {$key => $val});
195 453 $href->{$key} = $val;
196 453 next;
197 }
198  
199 36 my ( $key, $val ) = split( /: /, $line, 2 );
200 0 ▶ if ( !$val ) { $key =~ s/:$//; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($val) 0 36 36
201 36 $sub_section = $key;
202 36 $section{$section} = [ { $key => $val } ];
203 36 $new_section = 0;
204 }
205 elsif ( $l =~ m/^\t\t[a-zA-Z0-9]/ ) {
206 51 my $href = $section{$section}->[-1];
207 51 ▶ if ( !ref( $href->{$sub_section} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $href->{$sub_section}) 21 30 51
208 21 $href->{$sub_section} = [];
209 }
210  
211 51 push( @{ $href->{$sub_section} }, $line );
212 }
213  
214 }
215  
216 3 $self->{"__dmi"} = \%section;
217  
218 }
219  
220 40 1;
« lib/Rex/Pkg/SunOS/pkg.pm ↑ Summary blib/lib/Rex/Output/JUnit.pm »