/usr/share/fusioninventory/lib/FusionInventory/Agent/Tools/MacOS.pm is in fusioninventory-agent 1:2.3.10.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | package FusionInventory::Agent::Tools::MacOS;
use strict;
use warnings;
use base 'Exporter';
use English qw(-no_match_vars);
use Memoize;
use FusionInventory::Agent::Tools;
our @EXPORT = qw(
getSystemProfilerInfos
getIODevices
);
memoize('getSystemProfilerInfos');
sub getSystemProfilerInfos {
my (%params) = @_;
my $command = $params{type} ?
"/usr/sbin/system_profiler $params{type}" : "/usr/sbin/system_profiler";
my $handle = getFileHandle(command => $command, %params);
my $info = {};
my @parents = (
[ $info, -1 ]
);
while (my $line = <$handle>) {
chomp $line;
next unless $line =~ /^(\s*)(\S[^:]*):(?: (.*\S))?/;
my $level = defined $1 ? length($1) : 0;
my $key = $2;
my $value = $3;
my $parent = $parents[-1];
my $parent_level = $parent->[1];
my $parent_node = $parent->[0];
if (defined $value) {
# check indentation level against parent node
if ($level <= $parent_level) {
if (keys %$parent_node == 0) {
# discard just created node, and fix its parent
my $parent_key = $parent->[2];
$parents[-2]->[0]->{$parent_key} = undef;
}
# unstack nodes until a suitable parent is found
while ($level <= $parents[-1]->[1]) {
pop @parents;
}
$parent_node = $parents[-1]->[0];
}
# add the value to the current node
$parent_node->{$key} = $value;
} else {
# compare level with parent
if ($level > $parent_level) {
# down the tree: no change
} elsif ($level < $parent_level) {
# up the tree: unstack nodes until a suitable parent is found
while ($level <= $parents[-1]->[1]) {
pop @parents;
}
} else {
# same level: unstack last node
pop @parents;
}
# create a new node, and push it to the stack
my $parent_node = $parents[-1]->[0];
my $i;
my $keyL = $key;
while (defined($parent_node->{$key})) {
$key = $keyL . '_' . $i++;
}
$parent_node->{$key} = {};
push (@parents, [ $parent_node->{$key}, $level, $key ]);
}
}
close $handle;
return $info;
}
sub getIODevices {
my (%params) = @_;
# passing expected class to the command ensure only instance of this class
# are present in the output, reducing the size of the content to be parsed,
# but still requires some manual filtering to avoid subclasses instances
my $command = $params{class} ? "ioreg -c $params{class}" : "ioreg -l";
my $filter = $params{class} || '[^,]+';
my $handle = getFileHandle(command => $command, %params);
return unless $handle;
my @devices;
my $device;
while (my $line = <$handle>) {
if ($line =~ /<class $filter,/) {
# new device block
$device = {};
next;
}
next unless $device;
if ($line =~ /\| }/) {
# end of device block
push @devices, $device;
undef $device;
next;
}
if ($line =~ /"([^"]+)" \s = \s <? (?: "([^"]+)" | (\d+)) >?/x) {
# string or numeric property
$device->{$1} = $2 || $3;
next;
}
}
close $handle;
return @devices;
}
1;
__END__
=head1 NAME
FusionInventory::Agent::Tools::MacOS - MacOS generic functions
=head1 DESCRIPTION
This module provides some generic functions for MacOS.
=head1 FUNCTIONS
=head2 getSystemProfilerInfos(%params)
Returns a structured view of system_profiler output. Each information block is
turned into a hashref, hierarchically organised.
$info = {
'Hardware' => {
'Hardware Overview' => {
'SMC Version (system)' => '1.21f4',
'Model Identifier' => 'iMac7,1',
...
}
}
}
=over
=item logger a logger object
=item command the exact command to use (default: /usr/sbin/system_profiler)
=item file the file to use, as an alternative to the command
=back
=head2 getIODevices(%params)
Returns a flat list of devices as a list of hashref, by parsing ioreg output.
Relationships are not extracted.
=over
=item logger a logger object
=item class the class of devices wanted
=item file the file to use, as an alternative to the command
=back
|