File Coverage

blib/lib/Armadito/Agent/Tools/Dmidecode.pm
Criterion Covered Total %
statement 21 21 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 28 28 100.0


line stmt bran cond sub pod time code
1             package Armadito::Agent::Tools::Dmidecode;
2              
3 23     23   1478639 use strict;
  23         35  
  23         532  
4 23     23   75 use warnings;
  23         35  
  23         514  
5 23     23   75 use base 'Exporter';
  23         50  
  23         1461  
6              
7 23     23   89 use English qw(-no_match_vars);
  23         25  
  23         116  
8 23     23   7671 use Memoize;
  23         1891  
  23         851  
9              
10 23     23   428 use Armadito::Agent::Tools qw(trimWhitespaces);
  23         36  
  23         797  
11 23     23   12042 use IPC::System::Simple qw(capture $EXITVAL EXIT_ANY);
  23         222175  
  23         8091  
12              
13             our @EXPORT_OK = qw(
14             getDmidecodeInfos
15             );
16              
17             memoize('getDmidecodeInfos');
18              
19             sub getDmidecodeInfos {
20             my (%params) = @_;
21              
22             my $output = capture( EXIT_ANY, "dmidecode" );
23             my ( $info, $block, $type );
24             my @lines = split( /\n/, $output );
25              
26             foreach my $line (@lines) {
27             chomp $line;
28              
29             if ( $line =~ /DMI type (\d+)/ ) {
30              
31             # push previous block in list
32             if ($block) {
33             push( @{ $info->{$type} }, $block );
34             undef $block;
35             }
36              
37             # switch type
38             $type = $1;
39              
40             next;
41             }
42              
43             next unless defined $type;
44              
45             next unless $line =~ /^\s+ ([^:]+) : \s (.*\S)/x;
46              
47             next
48             if $2 eq 'N/A'
49             || $2 eq 'Not Specified'
50             || $2 eq 'Not Present'
51             || $2 eq 'Unknown'
52             || $2 eq '<BAD INDEX>'
53             || $2 eq '<OUT OF SPEC>'
54             || $2 eq '<OUT OF SPEC><OUT OF SPEC>';
55              
56             $block->{$1} = trimWhitespaces($2);
57             }
58              
59             # do not return anything if dmidecode output is obviously truncated
60             return if keys %$info < 2;
61              
62             return $info;
63             }
64             1;
65             __END__
66              
67             =head1 NAME
68              
69             Armadito::Agent::Tools::Dmidecode - Dmidecode OS-independent function
70              
71             =head1 DESCRIPTION
72              
73             This module provides an OS-independent function for extracting system information with dmidecode.
74              
75             =head1 FUNCTIONS
76              
77             =head2 getDmidecodeInfos
78              
79             Returns a structured view of dmidecode output. Each information block is turned
80             into an hashref, block with same DMI type are grouped into a list, and each
81             list is indexed by its DMI type into the resulting hashref.
82              
83             $info = {
84             0 => [
85             { block }
86             ],
87             1 => [
88             { block },
89             { block },
90             ],
91             ...
92             }