diff options
author | Steve McIntyre <steve@einval.com> | 2023-11-01 10:24:47 +0000 |
---|---|---|
committer | Steve McIntyre <steve@einval.com> | 2023-11-01 10:24:47 +0000 |
commit | f4f7e14672b091889d89b7750519c47f57b50e6e (patch) | |
tree | f0ba14f91cb65e03671fc1fd63c60e1570cb644d | |
parent | dab831621b8a90e7ea9ace737d29fa0a4f17bf39 (diff) | |
download | steve-scripts-f4f7e14672b091889d89b7750519c47f57b50e6e.zip |
disk-details: switch to 100% perl
-rwxr-xr-x | disk-details | 141 |
1 files changed, 64 insertions, 77 deletions
diff --git a/disk-details b/disk-details index 03dbf3b..978c4ce 100755 --- a/disk-details +++ b/disk-details @@ -1,88 +1,75 @@ -#!/bin/sh +#!/usr/bin/perl -PATH=/usr/sbin:$PATH -export PATH +use strict; +use warnings; -if [ "$1"x != ""x ] ; then - DEVICES="$@" -else - DEVICES="/dev/sd? /dev/nvme?n1" -fi +$ENV{"PATH"} = "/usr/sbin:" . $ENV{"PATH"}; -print_capacity () { - DEVICE=$1 - sg_readcap ${DEVICE} | perl -e ' - while (<STDIN>) { - chomp; - m/Device size:.*\s+(\S+\s+GB)/ and printf("%11.11s\n", $1); - }' +sub print_capacity ($) { + my $device = shift; + + open(IN, "sg_readcap $device |"); + if ($? != 0) { + print "[READCAP ERROR]\n"; + return; + } + while (<IN>) { + chomp; + m/Device size:.*\s+(\S+\s+GB)/ and printf("%11.11s\n", $1); + } + close IN; } # Parse the output of sg_inq -sg_data () { - DEVICE=$1 +sub sg_data ($) { + my $device = shift; + my ($vendor, $product, $fw, $serial, $vendor_prod); - if [ ! -e "$DEVICE" ]; then - return - fi + open(IN, "sg_inq $device |"); + if ($? != 0) { + printf("%-14.14s [ERRORS]\n", "$device:"); + return; + } - sg_inq ${DEVICE} >/dev/null 2>&1 - if [ $? -ne 0 ]; then - echo " [ERRORS]" - else - sg_inq ${DEVICE} | perl -e ' - while (<STDIN>) { - chomp; - m/Vendor identification:\s+(.*)/ and $vendor = $1; - m/Product identification:\s+(.*)/ and $product = $1; - m/Product revision level:\s+(.*)/ and $fw = $1; - m/Unit serial number:\s+(.*)/ and $serial = $1; - } - printf("%-8.8s %-16.16s %-8.8s %-24s ", $vendor, $product, $fw, $serial)' - if [ $? -ne 0 ]; then - echo " [ERRORS]" - fi - print_capacity ${DEVICE} - fi -# smartctl -a $DEVICE | grep -e "Start_Stop_Count" -e "Load_Cycle_Count" + # Sigh, different device types output different formats + if ($device =~ m,^/dev/sd,) { + while (<IN>) { + chomp; + m/Vendor identification:\s+(.*)/ and $vendor = $1; + m/Product identification:\s+(.*)/ and $product = $1; + m/Product revision level:\s+(.*)/ and $fw = $1; + m/Unit serial number:\s+(.*)/ and $serial = $1; + } + printf("%-14.14s %-8.8s %-16.16s %-8.8s %-24s ", "$device:", $vendor, $product, $fw, $serial); + } + elsif ($device =~ m,^/dev/nvme,) { + while (<IN>) { + chomp; + m/Model number:\s+(.*)/ and $vendor_prod = $1; + m/Serial number:\s+(.*)/ and $serial = $1; + m/Firmware revision:\s+(.*)/ and $fw = $1; + m/Unit serial number:\s+(.*)/ and $serial = $1; + } + printf("%-14.14s %-25.25s %-8.8s %-24s ", "$device:", $vendor_prod, $fw, $serial) + } else { + print "Unknown device type $device\n"; + close IN; + return; + } + + close IN; + print_capacity($device); } -# Sigh, NVME devices output a different format -nvme_data () { - DEVICE=$1 - - sg_inq ${DEVICE} >/dev/null 2>&1 - if [ $? -ne 0 ]; then - echo " [ERRORS]" - else - sg_inq ${DEVICE} | perl -e ' - while (<STDIN>) { - chomp; - m/Model number:\s+(.*)/ and $vendor_prod = $1; - m/Serial number:\s+(.*)/ and $serial = $1; - m/Firmware revision:\s+(.*)/ and $fw = $1; - m/Unit serial number:\s+(.*)/ and $serial = $1; - } - printf("%-25.25s %-8.8s %-24s ", $vendor_prod, $fw, $serial)' - if [ $? -ne 0 ]; then - echo " [ERRORS]" - fi - print_capacity ${DEVICE} - fi -# smartctl -a $DEVICE | grep -e "Start_Stop_Count" -e "Load_Cycle_Count" -} +my @devices; +if (scalar(@ARGV) > 0) { + @devices = @ARGV; +} else { + @devices = glob "/dev/sd? /dev/nvme?n1"; +} -for DEVICE in ${DEVICES}; do - if [ -e "$DEVICE" ]; then - printf "%-15.15s" "${DEVICE}:" - case $DEVICE in - /dev/sd*) - sg_data $DEVICE;; - /dev/nvme*) - nvme_data $DEVICE;; - *) - echo "Unknown device type $DEVICE" - ;; - esac - fi -done +foreach my $device (@devices) { + if (-e $device) { + sg_data($device); + } +} |