File Coverage

lib/Rex/Inventory/DMIDecode.pm
Criterion Covered Total %
statement 127 144 88.1
branch 31 40 77.5
condition 5 9 55.5
subroutine 20 20 100.0
pod 0 8 0.0
total 183 221 82.8


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Inventory::DMIDecode;
6              
7 39     39   70724 use v5.12.5;
  39         151  
8 39     39   201 use warnings;
  39         76  
  39         1079  
9 39     39   893 use Data::Dumper;
  39         7311  
  39         2820  
10              
11             our $VERSION = '1.14.2.3'; # TRIAL VERSION
12              
13 39     39   927 use Rex::Inventory::DMIDecode::BaseBoard;
  39         139  
  39         1922  
14 39     39   928 use Rex::Inventory::DMIDecode::Bios;
  39         133  
  39         1659  
15 39     39   874 use Rex::Inventory::DMIDecode::CPU;
  39         136  
  39         1700  
16 39     39   890 use Rex::Inventory::DMIDecode::Memory;
  39         221  
  39         1702  
17 39     39   1055 use Rex::Inventory::DMIDecode::MemoryArray;
  39         127  
  39         1721  
18 39     39   887 use Rex::Inventory::DMIDecode::SystemInformation;
  39         126  
  39         1695  
19 39     39   745 use Rex::Commands::Run;
  39         84  
  39         369  
20 39     39   291 use Rex::Helper::Run;
  39         160  
  39         55338  
21              
22             sub new {
23 54     54 0 634 my $that = shift;
24 54   33     913 my $proto = ref($that) || $that;
25 54         326 my $self = {@_};
26              
27 54         337 bless( $self, $proto );
28              
29 54         786 $self->_read_dmidecode();
30              
31 54         1986 return $self;
32             }
33              
34             sub get_tree {
35 122     122 0 551 my ( $self, $section ) = @_;
36              
37 122 50       416 if ($section) {
38 122         892 return $self->{"__dmi"}->{$section};
39             }
40              
41 0         0 return $self->{"__dmi"};
42             }
43              
44             sub get_base_board {
45 3     3 0 2061 my ($self) = @_;
46              
47 3         20 return Rex::Inventory::DMIDecode::BaseBoard->new( dmi => $self );
48             }
49              
50             sub get_bios {
51 3     3 0 14 my ($self) = @_;
52              
53 3         13 return Rex::Inventory::DMIDecode::Bios->new( dmi => $self );
54             }
55              
56             sub get_system_information {
57 54     54 0 416 my ($self) = @_;
58              
59 54         1970 return Rex::Inventory::DMIDecode::SystemInformation->new( dmi => $self );
60             }
61              
62             sub get_cpus {
63              
64 3     3 0 20 my ($self) = @_;
65 3         7 my @cpus = ();
66 3         7 my $tree = $self->get_tree("Processor Information");
67 3         8 my $idx = 0;
68 3         4 for my $cpu ( @{$tree} ) {
  3         7  
69 3 50       14 if ( $cpu->{"Status"} =~ m/Populated/ ) {
70 3         149 push( @cpus,
71             Rex::Inventory::DMIDecode::CPU->new( dmi => $self, index => $idx ) );
72             }
73 3         8 ++$idx;
74             }
75              
76 3         9 return @cpus;
77              
78             }
79              
80             sub get_memory_modules {
81              
82 3     3 0 13 my ($self) = @_;
83 3         5 my @mems = ();
84 3         7 my $tree = $self->get_tree("Memory Device");
85 3         6 my $idx = 0;
86 3         5 for my $mem ( @{$tree} ) {
  3         7  
87 12 100       38 if ( $mem->{"Size"} =~ m/\d+/ ) {
88 3         14 push( @mems,
89             Rex::Inventory::DMIDecode::Memory->new( dmi => $self, index => $idx ) );
90             }
91 12         17 ++$idx;
92             }
93              
94 3         12 return @mems;
95              
96             }
97              
98             sub get_memory_arrays {
99              
100 3     3 0 12 my ($self) = @_;
101 3         6 my @mems = ();
102 3         5 my $tree = $self->get_tree("Physical Memory Array");
103 3         6 my $idx = 0;
104 3         4 for my $mema ( @{$tree} ) {
  3         7  
105 3         14 push(
106             @mems,
107             Rex::Inventory::DMIDecode::MemoryArray->new(
108             dmi => $self,
109             index => $idx
110             )
111             );
112 3         6 ++$idx;
113             }
114              
115 3         8 return @mems;
116              
117             }
118              
119             sub _read_dmidecode {
120              
121 54     54   347 my ($self) = @_;
122              
123 54         157 my @lines;
124 54 100       292 if ( $self->{lines} ) {
125 3         7 @lines = @{ $self->{lines} };
  3         120  
126             }
127             else {
128 51 50       333 unless ( can_run("dmidecode") ) {
129 51         1471 Rex::Logger::debug("Please install dmidecode on the target system.");
130 51         709 return;
131             }
132              
133 0         0 eval { @lines = i_run "dmidecode"; };
  0         0  
134              
135 0 0       0 if ($@) {
136 0         0 Rex::Logger::debug("Error running dmidecode");
137 0         0 return;
138             }
139             }
140 3         44 chomp @lines;
141              
142 3         5 my %section = ();
143 3         8 my $section = "";
144 3         7 my $new_section = 0;
145 3         7 my $sub_section = "";
146              
147 3         7 for my $l (@lines) {
148              
149 735 100       1453 next if $l =~ m/^Handle/;
150 675 100       1101 next if $l =~ m/^#/;
151 672 100       1142 next if $l =~ m/^SMBIOS/;
152 669 100       1261 next if $l =~ m/^$/;
153 609 100       1005 last if $l =~ m/^End Of Table$/;
154              
155             # for openbsd
156 606         861 $l =~ s/ /\t/g;
157              
158 606 100       1218 unless ( substr( $l, 0, 1 ) eq "\t" ) {
159 66         92 $section = $l;
160 66         82 $new_section = 1;
161 66         97 next;
162             }
163              
164 540         744 my $line = $l;
165 540         1480 $line =~ s/^\t+//g;
166 540         1184 $line =~ s/\s+$//g;
167              
168 540 50       1055 next if $l =~ m/^$/;
169              
170 540 100       1148 if ( $l =~ m/^\t[a-zA-Z0-9]/ ) {
    50          
171 489 50 66     2154 if ( exists $section{$section} && !ref( $section{$section} ) ) {
    100 66        
172 0         0 my $content = $section{$section};
173 0         0 $section{$section} = [];
174 0         0 my @arr = ();
175 0         0 my ( $key, $val ) = split( /: /, $line, 2 );
176 0         0 $key =~ s/:$//;
177 0         0 $sub_section = $key;
178              
179             #push (@{$section{$section}}, $content);
180 0         0 push( @{ $section{$section} }, { $key => $val } );
  0         0  
181 0         0 $new_section = 0;
182 0         0 next;
183             }
184             elsif ( exists $section{$section} && ref( $section{$section} ) ) {
185 453 100       823 if ($new_section) {
186 24         30 push( @{ $section{$section} }, {} );
  24         59  
187 24         36 $new_section = 0;
188             }
189 453         1158 my ( $key, $val ) = split( /: /, $line, 2 );
190 453         784 $key =~ s/:$//;
191 453         585 $sub_section = $key;
192 453         634 my $href = $section{$section}->[-1];
193              
194             #push (@{$section{$section}}, {$key => $val});
195 453         804 $href->{$key} = $val;
196 453         785 next;
197             }
198              
199 36         105 my ( $key, $val ) = split( /: /, $line, 2 );
200 36 50       72 if ( !$val ) { $key =~ s/:$//; }
  0         0  
201 36         50 $sub_section = $key;
202 36         120 $section{$section} = [ { $key => $val } ];
203 36         73 $new_section = 0;
204             }
205             elsif ( $l =~ m/^\t\t[a-zA-Z0-9]/ ) {
206 51         84 my $href = $section{$section}->[-1];
207 51 100       103 if ( !ref( $href->{$sub_section} ) ) {
208 21         36 $href->{$sub_section} = [];
209             }
210              
211 51         70 push( @{ $href->{$sub_section} }, $line );
  51         144  
212             }
213              
214             }
215              
216 3         49 $self->{"__dmi"} = \%section;
217              
218             }
219              
220             1;