summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve McIntyre <steve@einval.com>2023-10-30 12:28:20 +0000
committerSteve McIntyre <steve@einval.com>2023-10-30 12:28:20 +0000
commit3b2d3a9ca1b31b23f784816142affb09d0d60366 (patch)
treee8c355bcc2c2f6c90ed45d757a4c3933b315360d
parentc5b18ffe1d021ebce203902783edb93e3f56ebaa (diff)
downloadsteve-scripts-3b2d3a9ca1b31b23f784816142affb09d0d60366.zip
disk-details: switch to perl for parsing sg_* output
Easier to just grab things with perl regexps than from a varying number of fields in awk. Also refactor slightly.
-rwxr-xr-xdisk-details94
1 files changed, 41 insertions, 53 deletions
diff --git a/disk-details b/disk-details
index 0c3d329..49e9776 100755
--- a/disk-details
+++ b/disk-details
@@ -9,6 +9,15 @@ else
DEVICES="/dev/sd? /dev/nvme?n1"
fi
+print_capacity () {
+ DEVICE=$1
+ sg_readcap ${DEVICE} | perl -e '
+ while (<STDIN>) {
+ chomp;
+ m/Device size:.*\s+(\S+\s+GB)/ and print "$1\n";
+ }'
+}
+
# Parse the output of sg_inq
sg_data () {
DEVICE=$1
@@ -21,33 +30,24 @@ sg_data () {
if [ $? -ne 0 ]; then
echo " [ERRORS]"
else
- sg_inq ${DEVICE} | awk '
- /Vendor/ {
- VENDOR=$3
- }
- /Product identification/ {
- if (NF == 3) {
- PRODUCT=$3
- } else {
- PRODUCT=sprintf("%s %s", $3, $4)
- }
- }
- /Product revision level/ {
- FW=$4
- }
- /Unit serial/ {
- SERIAL=$4
- printf("%-8.8s %-16.16s %-8.8s %-24s ", VENDOR, PRODUCT, FW, SERIAL)
- }'
+ 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
- sg_readcap ${DEVICE} | awk '/Device size:/ {print $7,$8}'
+ print_capacity ${DEVICE}
fi
# smartctl -a $DEVICE | grep -e "Start_Stop_Count" -e "Load_Cycle_Count"
}
-# Sigh, NVME devices show a different formate
+# Sigh, NVME devices output a different format
nvme_data () {
DEVICE=$1
@@ -55,46 +55,34 @@ nvme_data () {
if [ $? -ne 0 ]; then
echo " [ERRORS]"
else
- sg_inq ${DEVICE} | awk '
- /Model number:/ {
- VENDOR_PROD=$0
- gsub("^.*Model number: ","",VENDOR_PROD)
- }
- /Serial number:/ {
- SERIAL=$0
- gsub("^.*Serial number: ","",SERIAL)
- }
- /Firmware revision:/ {
- FW=$0
- gsub("^.*Firmware revision: ","",FW)
- printf("%-25.25s %-8.8s %-24s ", VENDOR_PROD, FW, SERIAL)
- }'
+ 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
- sg_readcap ${DEVICE} | awk '/Device size:/ {print $7,$8}'
+ print_capacity ${DEVICE}
fi
# smartctl -a $DEVICE | grep -e "Start_Stop_Count" -e "Load_Cycle_Count"
}
for DEVICE in ${DEVICES}; do
-
- if [ ! -e "$DEVICE" ]; then
- continue
+ 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
-
- PD="${DEVICE}: "
-
- printf "%-15.15s" ${PD}
-
- case $DEVICE in
- /dev/sd*)
- sg_data $DEVICE;;
- /dev/nvme*)
- nvme_data $DEVICE;;
- *)
- echo "Unknown device type $DEVICE"
- ;;
- esac
-
done