File Coverage

lib/Rex/Inventory/SMBios.pm
Criterion Covered Total %
statement 35 107 32.7
branch 0 24 0.0
condition 0 3 0.0
subroutine 12 21 57.1
pod 0 8 0.0
total 47 163 28.8


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Inventory::SMBios;
6              
7 38     38   493 use v5.12.5;
  38         140  
8 38     38   211 use warnings;
  38         67  
  38         1624  
9              
10             our $VERSION = '1.14.2.2'; # TRIAL VERSION
11              
12 38     38   251 use Data::Dumper;
  38         83  
  38         1882  
13 38     38   262 use Rex::Logger;
  38         122  
  38         247  
14 38     38   953 use Rex::Commands::Run;
  38         190  
  38         259  
15 38     38   240 use Rex::Helper::Run;
  38         211  
  38         2398  
16              
17 38     38   431 use Rex::Inventory::SMBios::BaseBoard;
  38         92  
  38         1565  
18 38     38   465 use Rex::Inventory::SMBios::Bios;
  38         112  
  38         1611  
19 38     38   441 use Rex::Inventory::SMBios::CPU;
  38         455  
  38         1606  
20 38     38   438 use Rex::Inventory::SMBios::Memory;
  38         104  
  38         1675  
21 38     38   442 use Rex::Inventory::SMBios::MemoryArray;
  38         118  
  38         1721  
22 38     38   453 use Rex::Inventory::SMBios::SystemInformation;
  38         109  
  38         38556  
23              
24             sub new {
25 0     0 0   my $that = shift;
26 0   0       my $proto = ref($that) || $that;
27 0           my $self = {@_};
28              
29 0           bless( $self, $proto );
30              
31 0           $self->_read_smbios();
32              
33 0           return $self;
34             }
35              
36             sub get_tree {
37 0     0 0   my ( $self, $section ) = @_;
38              
39 0 0         if ($section) {
40 0           return $self->{"__dmi"}->{$section};
41             }
42              
43 0           return $self->{"__dmi"};
44             }
45              
46             sub get_base_board {
47 0     0 0   my ($self) = @_;
48 0           return Rex::Inventory::SMBios::BaseBoard->new( dmi => $self );
49             }
50              
51             sub get_bios {
52 0     0 0   my ($self) = @_;
53 0           return Rex::Inventory::SMBios::Bios->new( dmi => $self );
54             }
55              
56             sub get_system_information {
57 0     0 0   my ($self) = @_;
58 0           return Rex::Inventory::SMBios::SystemInformation->new( dmi => $self );
59             }
60              
61             sub get_cpus {
62              
63 0     0 0   my ($self) = @_;
64 0           my @cpus = ();
65 0           my $tree = $self->get_tree("processor");
66 0           my $idx = 0;
67 0           for my $cpu ( @{$tree} ) {
  0            
68 0 0         if ( $cpu->{"Socket Status"} =~ m/Populated/ ) {
69 0           push( @cpus,
70             Rex::Inventory::SMBios::CPU->new( dmi => $self, index => $idx ) );
71             }
72 0           ++$idx;
73             }
74              
75 0           return @cpus;
76              
77             }
78              
79             sub get_memory_modules {
80              
81 0     0 0   my ($self) = @_;
82 0           my @mems = ();
83 0           my $tree = $self->get_tree("memory device");
84 0           my $idx = 0;
85 0           for my $mem ( @{$tree} ) {
  0            
86 0 0         if ( $mem->{"Size"} =~ m/\d+/ ) {
87 0           push( @mems,
88             Rex::Inventory::SMBios::Memory->new( dmi => $self, index => $idx ) );
89             }
90 0           ++$idx;
91             }
92              
93 0           return @mems;
94              
95             }
96              
97             sub get_memory_arrays {
98              
99 0     0 0   my ($self) = @_;
100 0           my @mems = ();
101 0           my $tree = $self->get_tree("physical memory array");
102 0           my $idx = 0;
103 0           for my $mema ( @{$tree} ) {
  0            
104 0           push( @mems,
105             Rex::Inventory::SMBios::MemoryArray->new( dmi => $self, index => $idx ) );
106 0           ++$idx;
107             }
108              
109 0           return @mems;
110              
111             }
112              
113             sub _read_smbios {
114 0     0     my ($self) = @_;
115              
116 0           my @data = i_run( "smbios", fail_ok => 1 );
117              
118 0           my ( $current_section, %section, $key, $val, %cur_data );
119 0           for my $line (@data) {
120 0 0         next if ( $line =~ /^$/ );
121 0 0         next if ( $line =~ /^\s*$/ );
122 0 0         next if ( $line =~ /^ID/ );
123              
124 0 0         if ( $line =~ m/^\d/ ) {
125 0 0         push( @{ $section{$current_section} }, {%cur_data} ) if (%cur_data);
  0            
126              
127 0           ($current_section) = ( $line =~ m/\(([^\)]+)\)/ );
128              
129 0 0         if ( !exists $section{$current_section} ) {
130 0           $section{$current_section} = [];
131             }
132              
133 0           %cur_data = ();
134 0           next;
135             }
136              
137             # outer section
138 0 0         if ( $line =~ /^\s\s[a-z]/i ) {
    0          
139 0           $line =~ s/^\s*//;
140 0           ( $key, $val ) = split( /: /, $line );
141 0           $cur_data{$key} = $val;
142             }
143             elsif ( $line =~ /^\t[a-z]/i ) {
144 0 0         if ( !ref( $cur_data{$key} ) ) {
145 0           $cur_data{$key} = [];
146             }
147              
148 0           $line =~ s/^\s*//;
149 0           push( @{ $cur_data{$key} }, $line );
  0            
150             }
151              
152             }
153              
154             # push the last
155 0           push( @{ $section{$current_section} }, {%cur_data} );
  0            
156              
157 0           $self->{"__dmi"} = \%section;
158             }
159              
160             1;