#!/usr/bin/env perl # Copyright (c) 2025, R. W. Rodolico # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # freebsd_storage # FreeBSD storage section (Spec 1.4): storage.disks (identity, size, slot, health) and # storage.partitions, from sysctl, geom, glabel, sesutil and smartctl, plus the # old diskinfo raw-disk keys (replaces bsd_disk). # # Revision History: # 2026-10-18 Claude v1.0.0 new sensor, split from unix_storage (bug 210) package freebsd_storage; use strict; use warnings; use version; our $VERSION = version->declare('v1.0.0'); use FindBin; use File::Spec; use lib File::Spec->catdir($FindBin::Bin), File::Spec->catdir("$FindBin::Bin/.."); use library qw(:all); my $OLD_CATEGORY = 'diskinfo'; ####################################################### # # parseSesutilMap( $text ) # # FreeBSD: maps each disk named in sesutil map to its slot description. # Elements without a description are not given a slot (never guessed). # # Parameters: # $text - output of sesutil map # # Returns: # hash reference dev => slot # # Side Effects: # none # ####################################################### sub parseSesutilMap { my $text = shift; my %slots; my $description; foreach my $line (split(/\n/, defined($text) ? $text : '')) { if ($line =~ /^\s*Element\s+\d+/) { $description = undef; } elsif ($line =~ /^\s*Description:\s*(.*?)\s*$/) { $description = $1; } elsif ($line =~ /^\s*Device Names:\s*(.*?)\s*$/) { next unless defined($description) && $description ne ''; foreach my $name (split(/\s*,\s*/, $1)) { next if $name eq '' || $name =~ /^pass\d+$/; $slots{$name} = $description; } } } return \%slots; } ####################################################### # # oldBsdRawDiskLines( $kernDisks, $geomDisks ) # # Old diskinfo FreeBSD raw-disk keys, as v3.7.1 bsd_disk: one # /dev/ size per kern.disks entry, the media size in KiB (not # truncated). bsd_disk read the size from diskinfo; geom disk list # reports the same Mediasize, so no extra command is run per disk. # # Parameters: # $kernDisks - sysctl -n kern.disks text # $geomDisks - geom disk list text # # Returns: # array reference of lines # # Side Effects: # none # ####################################################### sub oldBsdRawDiskLines { my ($kernDisks, $geomDisks) = @_; my (%mediasize, $current); foreach my $line (split(/\n/, defined($geomDisks) ? $geomDisks : '')) { if ($line =~ /^Geom name:\s*(\S+)/) { $current = $1; } elsif (defined($current) && !defined($mediasize{$current}) && $line =~ /^\s*Mediasize:\s*(\d+)/) { $mediasize{$current} = $1; } } my @out; foreach my $device (split(/\s+/, defined($kernDisks) ? $kernDisks : '')) { next unless defined($mediasize{$device}); push @out, "$OLD_CATEGORY\t/dev/$device\tsize\t" . ($mediasize{$device} / 1024); } return \@out; } ####################################################### # # main() # # Emits storage.disks, storage.partitions and old diskinfo raw-disk keys. # # Parameters: # none # # Returns: # integer exit status (1 wrong OS, 2 not applicable) # # Side Effects: # runs sysctl, geom, glabel, sesutil and smartctl, writes STDOUT/STDERR # ####################################################### sub main { if (!checkOS({ 'freebsd' => undef })) { print STDERR "unsupported os\n"; return 1; } my $inv = localDiskInventory(); if (defined($inv->{'missing'})) { print STDERR "$inv->{'missing'} not found\n"; return 2; } my $disks = $inv->{'disks'}; my $slots = haveCommand('sesutil') ? parseSesutilMap(runCommand('sesutil', 'map')) : {}; my @out; push @out, @{oldBsdRawDiskLines($inv->{'raw'}{'kernDisks'}, $inv->{'raw'}{'geomDisks'})}; my $health = collectHealth($disks); push @out, @{emitStorageDisks($disks, $slots)}; push @out, @{healthLines($_, $health->{$_})} foreach sort keys %{$disks}; push @out, @{partitionLines($inv->{'resolver'})}; unless (@out) { print STDERR "no disks found\n"; return 2; } print join("\n", @out), "\n"; return 0; } exit main() unless caller; 1;