File Coverage

blib/lib/FusionInventory/Agent/Task/Inventory/MacOS/Memory.pm
Criterion Covered Total %
statement 31 41 75.6
branch 7 10 70.0
condition 3 3 100.0
subroutine 6 8 75.0
pod 0 2 0.0
total 47 64 73.4


line stmt bran cond sub pod time code
1             package FusionInventory::Agent::Task::Inventory::MacOS::Memory;
2              
3 2     2   89091597 use strict;
  2         6  
  2         90  
4 2     2   17 use warnings;
  2         4  
  2         123  
5              
6 2     2   884 use FusionInventory::Agent::Tools;
  2         4  
  2         383  
7 2     2   1320 use FusionInventory::Agent::Tools::MacOS;
  2         6  
  2         1077  
8              
9             sub isEnabled {
10 0     0 0 0 my (%params) = @_;
11 0 0       0 return 0 if $params{no_category}->{memory};
12 0         0 return canRun('/usr/sbin/system_profiler');
13             }
14              
15             sub doInventory {
16 0     0 0 0 my (%params) = @_;
17              
18 0         0 my $inventory = $params{inventory};
19 0         0 my $logger = $params{logger};
20              
21 0         0 foreach my $memory (_getMemories(logger => $logger)) {
22 0         0 $inventory->addEntry(
23             section => 'MEMORIES',
24             entry => $memory
25             );
26             }
27              
28 0         0 my $memory = _getMemory(logger => $logger);
29 0         0 $inventory->setHardware({
30             MEMORY => $memory,
31             });
32              
33             }
34              
35             sub _getMemories {
36 4     4   869 my (%params) = @_;
37              
38             my $infos = getSystemProfilerInfos(
39             type => 'SPMemoryDataType',
40             logger => $params{logger},
41             file => $params{file}
42 4         107 );
43              
44 4 50       53 return unless $infos->{Memory};
45              
46             # the memory slot informations may appears directly under
47             # 'Memory' top-level node, or under Memory/Memory Slots
48             my $parent_node = $infos->{Memory}->{'Memory Slots'} ?
49             $infos->{Memory}->{'Memory Slots'} :
50 4 100       11 $infos->{Memory};
51              
52 4         6 my @memories;
53 4         28 foreach my $key (sort keys %$parent_node) {
54 22 100       81 next unless $key =~ /DIMM(\d)/;
55 20         31 my $slot = $1;
56              
57 20         34 my $info = $parent_node->{$key};
58 20         26 my $description = $info->{'Part Number'};
59              
60             # convert hexa to ascii
61 20 100 100     81 if ($description && $description =~ /^0x/) {
62 4         24 $description = pack 'H*', substr($description, 2);
63 4         24 $description =~ s/\s*$//;
64             }
65              
66             my $memory = {
67             NUMSLOTS => $slot,
68             DESCRIPTION => $description,
69             CAPTION => "Status: $info->{'Status'}",
70             TYPE => $info->{'Type'},
71             SERIALNUMBER => $info->{'Serial Number'},
72             SPEED => getCanonicalSpeed($info->{'Speed'}),
73 20         83 CAPACITY => getCanonicalSize($info->{'Size'})
74             };
75              
76 20         48 push @memories, $memory;
77             }
78              
79 4         22 return @memories;
80             }
81              
82             sub _getMemory {
83 4     4   1451 my (%params) = @_;
84              
85             my $infos = getSystemProfilerInfos(
86             type => 'SPMemoryDataType',
87             logger => $params{logger},
88             file => $params{file}
89 4         93 );
90              
91             return getCanonicalSize(
92 4         115 $infos->{'Hardware'}{'Hardware Overview'}{'Memory'},
93             1024
94             );
95             }
96              
97             1;