/usr/share/fusioninventory/lib/FusionInventory/Agent/Task/Inventory/Linux/Drives.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 | package FusionInventory::Agent::Task::Inventory::Linux::Drives;
use strict;
use warnings;
use FusionInventory::Agent::Tools;
use FusionInventory::Agent::Tools::Unix;
sub isEnabled {
return
canRun('df') ||
canRun('lshal');
}
sub doInventory {
my (%params) = @_;
my $inventory = $params{inventory};
my $logger = $params{logger};
foreach my $filesystem (_getFilesystems($logger)) {
$inventory->addEntry(
section => 'DRIVES',
entry => $filesystem
);
}
}
sub _getFilesystems {
my ($logger) = @_;
# get filesystems list
my @filesystems =
# exclude virtual file systems
grep { $_->{FILESYSTEM} !~ /^(tmpfs|usbfs|proc|devpts|devshm|udev)$/ }
# get all file systems
getFilesystemsFromDf(logger => $logger, command => 'df -P -T -k');
# get additional informations
if (canRun('blkid')) {
# use blkid if available, as it is filesystem-independent
foreach my $filesystem (@filesystems) {
$filesystem->{SERIAL} = getFirstMatch(
logger => $logger,
command => "blkid $filesystem->{VOLUMN}",
pattern => qr/\sUUID="(\S*)"\s/
);
}
} else {
# otherwise fallback to filesystem-dependant utilities
my $has_dumpe2fs = canRun('dumpe2fs');
my $has_xfs_db = canRun('xfs_db');
my $has_dosfslabel = canRun('dosfslabel');
my %months = (
Jan => 1,
Feb => 2,
Mar => 3,
Apr => 4,
May => 5,
Jun => 6,
Jul => 7,
Aug => 8,
Sep => 9,
Oct => 10,
Nov => 11,
Dec => 12,
);
foreach my $filesystem (@filesystems) {
if ($filesystem->{FILESYSTEM} =~ /^ext(2|3|4|4dev)/ && $has_dumpe2fs) {
my $handle = getFileHandle(
logger => $logger,
command => "dumpe2fs -h $filesystem->{VOLUMN}"
);
next unless $handle;
while (my $line = <$handle>) {
if ($line =~ /Filesystem UUID:\s+(\S+)/) {
$filesystem->{SERIAL} = $1;
} elsif ($line =~ /Filesystem created:\s+\w+\s+(\w+)\s+(\d+)\s+([\d:]+)\s+(\d{4})$/) {
$filesystem->{CREATEDATE} = "$4/$months{$1}/$2 $3";
} elsif ($line =~ /Filesystem volume name:\s*(\S.*)/) {
$filesystem->{LABEL} = $1 unless $1 eq '<none>';
}
}
close $handle;
next;
}
if ($filesystem->{FILESYSTEM} eq 'xfs' && $has_xfs_db) {
$filesystem->{SERIAL} = getFirstMatch(
logger => $logger,
command => "xfs_db -r -c uuid $filesystem->{VOLUMN}",
pattern => qr/^UUID =\s+(\S+)/
);
$filesystem->{LABEL} = getFirstMatch(
logger => $logger,
command => "xfs_db -r -c label $filesystem->{VOLUMN}",
pattern => qr/^label =\s+"(\S+)"/
);
next;
}
if ($filesystem->{FILESYSTEM} eq 'vfat' && $has_dosfslabel) {
$filesystem->{LABEL} = getFirstLine(
logger => $logger,
command => "dosfslabel $filesystem->{VOLUMN}"
);
next;
}
}
}
# complete with hal if available
if (canRun('lshal')) {
my @hal_filesystems = _getFilesystemsFromHal();
my %hal_filesystems = map { $_->{VOLUMN} => $_ } @hal_filesystems;
foreach my $filesystem (@filesystems) {
# retrieve hal informations for this drive
my $hal_filesystem = $hal_filesystems{$filesystem->{VOLUMN}};
next unless $hal_filesystem;
# take hal information if it doesn't exist already
foreach my $key (keys %$hal_filesystem) {
$filesystem->{$key} = $hal_filesystem->{$key}
if !$filesystem->{$key};
}
}
}
return @filesystems;
}
sub _getFilesystemsFromHal {
my $devices = _parseLshal(command => 'lshal');
return @$devices;
}
sub _parseLshal {
my $handle = getFileHandle(@_);
return unless $handle;
my $devices = [];
my $device = {};
while (my $line = <$handle>) {
chomp $line;
if ($line =~ m{^udi = '/org/freedesktop/Hal/devices/(volume|block).*}) {
$device = {};
next;
}
next unless defined $device;
if ($line =~ /^$/) {
if ($device->{ISVOLUME}) {
delete($device->{ISVOLUME});
push(@$devices, $device);
}
undef $device;
} elsif ($line =~ /^\s+ block.device \s = \s '([^']+)'/x) {
$device->{VOLUMN} = $1;
} elsif ($line =~ /^\s+ volume.fstype \s = \s '([^']+)'/x) {
$device->{FILESYSTEM} = $1;
} elsif ($line =~ /^\s+ volume.label \s = \s '([^']+)'/x) {
$device->{LABEL} = $1;
} elsif ($line =~ /^\s+ volume.uuid \s = \s '([^']+)'/x) {
$device->{SERIAL} = $1;
} elsif ($line =~ /^\s+ storage.model \s = \s '([^']+)'/x) {
$device->{TYPE} = $1;
} elsif ($line =~ /^\s+ volume.size \s = \s (\S+)/x) {
my $value = $1;
$device->{TOTAL} = int($value/(1024*1024) + 0.5);
} elsif ($line =~ /block.is_volume\s*=\s*true/i) {
$device->{ISVOLUME} = 1;
}
}
close $handle;
return $devices;
}
1;
|