[plug] More bash

Brad Campbell brad at fnarfbargle.com
Mon May 8 08:40:18 AWST 2017


G'day all,

In the spirit of posting random bash snippets to solicit responses on 
better ways to skin the cat, here's script that queries all the drives 
on the system and tells you their best-guess operating age.


#!/bin/bash
declare -a ARR
CNT=0
echo -n Scanning drives.
for i in /dev/sd? ; do
	echo -n "."
	SMART=$(smartctl -x $i)
	hrs=$(echo "$SMART" | grep Power_On_Hours | awk '{print $8}' | egrep -v 
'[[:alpha:]]')
	[ -z "$hrs" ] && hrs=$(echo "$SMART" | grep 'Power-on Hours' | awk 
'{print $4}')
	[ -z "$hrs" ] && hrs=$(echo "$SMART" | egrep '^# 1' | cut -c 60- | awk 
'{print $1}')
	model="$(echo "$SMART" | grep 'Device Model:' | cut -f2 -d':' | xargs)"
	if [ -n "$hrs" ]  ; then
		ARR[$CNT]="$hrs $i $model"
		CNT=$((CNT+1))
	fi;
done
echo Done scanning
IFS=$'\n' sorted=($(sort -n <<<"${ARR[*]}"))
for i in ${sorted[*]} ; do
	hrs=`awk '{print $1}' <<< $i`
	dev=`awk '{print $2}' <<< $i`
	model=`cut -f3- -d" " <<< $i`
	Y=$((hrs/(365*24)))
	hrs=$((hrs%(365*24)))
	D=$((hrs/24))
	hrs=$((hrs%24))
	echo $dev" - "$model" - "$Y years $D days $hrs hours
done



srv:/home/brad# ./hours
Scanning drives..........................Done scanning
/dev/sda - WDC WDS120G1G0A-00SS50 - 0 years 59 days 4 hours
/dev/sdb - GeIL ZENITH R3_120GB - 0 years 60 days 20 hours
/dev/sdy - WDC WD60EFRX-68L0BN1 - 0 years 292 days 7 hours
/dev/sdd - WDC WD20EFRX-68EUZN0 - 0 years 292 days 8 hours
/dev/sdv - WDC WD10EFRX-68FYTN0 - 0 years 292 days 8 hours
/dev/sdx - WDC WD10EFRX-68FYTN0 - 0 years 292 days 8 hours
/dev/sdc - WDC WD20EFRX-68EUZN0 - 1 years 358 days 1 hours
/dev/sdk - WDC WD20EFRX-68AX9N0 - 3 years 10 days 12 hours
/dev/sdl - WDC WD20EFRX-68AX9N0 - 3 years 10 days 13 hours
/dev/sdt - INTEL SSDSC2CT240A3 - 3 years 280 days 2 hours
/dev/sdu - INTEL SSDSC2CT240A3 - 3 years 281 days 7 hours
/dev/sds - INTEL SSDSC2CT240A3 - 3 years 281 days 17 hours
/dev/sdh - WDC WD20EFRX-68AX9N0 - 3 years 297 days 13 hours
/dev/sdw - WDC WD20EFRX-68AX9N0 - 3 years 297 days 13 hours
/dev/sdq - WDC WD20EARS-60MVWB0 - 4 years 111 days 11 hours
/dev/sde - SAMSUNG SSD 830 Series - 4 years 200 days 5 hours
/dev/sdf - SAMSUNG SSD 830 Series - 4 years 200 days 8 hours
/dev/sdg - SAMSUNG SSD 830 Series - 4 years 200 days 9 hours
/dev/sdr - WDC WD20EARS-60MVWB0 - 6 years 26 days 10 hours
/dev/sdi - WDC WD20EARS-60MVWB0 - 6 years 26 days 11 hours
/dev/sdm - WDC WD20EARS-60MVWB0 - 6 years 26 days 12 hours
/dev/sdn - WDC WD20EARS-60MVWB0 - 6 years 26 days 12 hours
/dev/sdo - WDC WD20EARS-60MVWB0 - 6 years 26 days 13 hours
/dev/sdp - WDC WD20EARS-60MVWB0 - 6 years 26 days 14 hours
/dev/sdj - WDC WD20EARS-60MVWB0 - 6 years 27 days 19 hours

Regards,
Brad


More information about the plug mailing list