/
Drives.pm
148 lines (121 loc) · 3.97 KB
/
Drives.pm
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
package FusionInventory::Agent::Task::Inventory::BSD::Drives;
use strict;
use warnings;
use parent 'FusionInventory::Agent::Task::Inventory::Module';
use FusionInventory::Agent::Tools;
use FusionInventory::Agent::Tools::Unix;
sub isEnabled {
my (%params) = @_;
return 0 if $params{no_category}->{drive};
return canRun('df');
}
sub doInventory {
my (%params) = @_;
my $inventory = $params{inventory};
my $logger = $params{logger};
my $zpool = canRun('zpool');
# Check we can run geli command to lookup encrypted fs
my ($geli);
if (canRun('geom')) {
$geli = _getGeliList(%params);
}
# get filesystem types
my @types =
grep { ! /^(?:fdesc|devfs|procfs|linprocfs|linsysfs|tmpfs|fdescfs)$/ }
getFilesystemsTypesFromMount(logger => $logger);
# get filesystem for each type
my @filesystems;
foreach my $type (@types) {
my $foundfs = getFilesystemsFromDf(
logger => $logger,
command => "df -P -k -t $type",
type => $type
);
# Check for geli encryption
if ($geli) {
foreach my $fs (@{$foundfs}) {
my $encrypted;
if ($type eq 'zfs' && $zpool) {
my $status = _getZpoolStatus(
volumn => $fs->{VOLUMN},
%params
);
($encrypted) = grep { /.eli$/ } keys(%{$status->{config}});
} else {
($encrypted) = $fs->{VOLUMN} =~ m|/([^/]+.eli)$|;
}
if ($encrypted && $geli->{$encrypted}) {
$fs->{ENCRYPT_NAME} = "geli";
$fs->{ENCRYPT_STATUS} = $geli->{$encrypted}->{state} =~ /^ACTIVE$/i ? 'Yes' : 'No';
$fs->{ENCRYPT_ALGO} = $geli->{$encrypted}->{algo};
$fs->{ENCRYPT_TYPE} = $geli->{$encrypted}->{type};
}
}
}
push @filesystems, @{$foundfs};
}
# add filesystems to the inventory
foreach my $filesystem (@filesystems) {
$inventory->addEntry(
section => 'DRIVES',
entry => $filesystem
);
}
}
sub _getZpoolStatus {
my (%params) = @_;
my $volumn = $params{volumn}
or return;
my @lines = getAllLines(
command => "zpool status $volumn",
%params
);
my $status = {};
foreach my $line (@lines) {
next unless $line;
if ($line =~ /^\s*(\w+)\s*:\s*(\w.*)$/) {
$status->{$1} = $2;
} elsif ($line =~ /^\s*config\s*:/) {
$status->{config} = {};
} elsif ($status->{config} && $line =~ /^\s*([\w.]+)\s+(\w+)\s+\w+\s+\w+\s+\w+/) {
next if $1 eq "NAME";
$status->{config}->{$1} = $2;
}
}
return $status;
}
sub _getGeliList {
my (%params) = @_;
my $geli;
my @status = getAllLines(
command => "geom eli status -s",
%params
);
foreach my $status (@status) {
next unless $status =~ /^(\S+)\s/;
my $volumn = $1;
my @lines = getAllLines(
command => "geom eli list $volumn",
%params
);
foreach my $line (@lines) {
next unless $line;
if ($line =~ /^State:\s*(\S+)$/) {
$geli->{$volumn}->{state} = $1;
} elsif ($line =~ /^EncryptionAlgorithm:\s*(\S+)$/) {
$geli->{$volumn}->{algo} = $1;
} elsif ($line =~ /^KeyLength:\s*(\S+)$/) {
$geli->{$volumn}->{keysize} = $1;
} elsif ($line =~ /^Crypto:\s*(\S+)$/) {
$geli->{$volumn}->{type} = $1;
}
}
# Fix algo with keysize
if ($geli->{$volumn}->{algo} && $geli->{$volumn}->{keysize}) {
$geli->{$volumn}->{algo} = $geli->{$volumn}->{algo}."-".$geli->{$volumn}->{keysize};
delete $geli->{$volumn}->{keysize};
}
}
return $geli;
}
1;