Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ## smart2csv.sh - version 0.2 - da support!
- ## Jacob Dilles - 1/12/2016
- ##
- ## This script populates a CSV file with the output
- ## of smartctl for several hdds at once. You can specify
- ## which attributes to use by editing the run_attrs function
- DEVICE_TYPES="ada da"
- OUTPUT_TIME=`date "+%Y-%m-%d-%H%M"`
- OUTPUT_NAME="smartdump-$OUTPUT_TIME.csv"
- ###
- # This function called last.
- #
- # You can scp the $OUTPUT_NAME file somehere
- # or copy it to another directory
- # or delete it after printing it, etc.
- ###
- do_something_with_OUTPUT_NAME()
- {
- echo "wrote SMART CSV to $OUTPUT_NAME"
- }
- ###
- # This function called to remove temporary files
- # If you are having trouble and need to debug
- # it might be useful to add an echo here in place
- # of the rm, so you can see what the script is doing
- ###
- TMP_PRE="/tmp/smart2csv"
- rmtmp()
- {
- echo ">rm $1"
- rm "$1"
- }
- ###################################################
- appnd()
- {
- VALUE=`echo "$1" | sed 's/^ *//g' | sed 's/ *$//g'`
- echo "\"$VALUE\"" >> "$TEMPOUT"
- }
- col()
- {
- #echo "Column $1"
- if [ "$DEVNAME" = header ]; then
- appnd "$1"
- else
- VALUE=`cat "$DATAFILE" | grep "$1" | cut -f2 -d ":"`
- appnd "$VALUE"
- fi
- }
- atr()
- {
- #echo "Attr $1"
- if [ "$DEVNAME" = "header" ]; then
- appnd "$1"
- else
- VALUE=`cat "$DATAFILE" | grep "$1" | awk -F " " '{print $NF}'`
- appnd "$VALUE"
- fi
- }
- # This function is where we define what columns we want to have
- run_attrs()
- {
- echo "[SMART2CSV] row: $DEVNAME"
- TEMPOUT="$TMP_PRE-$DEVNAME.cols"
- #clear file
- echo "\"$2\"" > "$TEMPOUT"
- col "Model Family" # Western Digital Red
- col "Device Model" # WDC WD20EFRX-68EUZN0
- col "Serial Number" # WD-WCC4M7NPJF1N
- col "LU WWN Device Id" # 5 0014ee 20cb4db10
- col "Firmware Version" # 82.00A82
- col "User Capacity" # 2,000,398,934,016 bytes [2.00 TB]
- col "Sector Sizes" # 512 bytes logical, 4096 bytes physical
- col "Rotation Rate" # 5400 rpm
- col "SATA Version is" # SATA 3.0, 6.0 Gb/s (current: 6.0 Gb/s)
- #...
- col "Lifetime Min/Max Temperature" # 22/30 Celsius
- col "Under/Over Temperature Limit Count" # 0/0
- col "SMART overall-health self-assessment test result" # PASSED
- atr " 1 Raw_Read_Error_Rate " # POSR-K 200 200 051 - 0
- atr " 3 Spin_Up_Time " # POS--K 176 176 021 - 4183
- atr " 4 Start_Stop_Count " # -O--CK 100 100 000 - 14
- atr " 5 Reallocated_Sector_Ct " # PO--CK 200 200 140 - 0
- atr " 7 Seek_Error_Rate " # -OSR-K 200 200 000 - 0
- atr " 9 Power_On_Hours " # -O--CK 100 100 000 - 154
- atr " 10 Spin_Retry_Count " # -O--CK 100 253 000 - 0
- atr " 11 Calibration_Retry_Count " # -O--CK 100 253 000 - 0
- atr " 12 Power_Cycle_Count " # -O--CK 100 100 000 - 14
- atr "192 Power-Off_Retract_Count " # -O--CK 200 200 000 - 10
- atr "193 Load_Cycle_Count " # -O--CK 200 200 000 - 36
- atr "194 Temperature_Celsius " # -O---K 119 116 000 - 28
- atr "196 Reallocated_Event_Count " # -O--CK 200 200 000 - 0
- atr "197 Current_Pending_Sector " # -O--CK 200 200 000 - 0
- atr "198 Offline_Uncorrectable " # ----CK 100 253 000 - 0
- atr "199 UDMA_CRC_Error_Count " # -O--CK 200 200 000 - 1
- atr "200 Multi_Zone_Error_Rate " # ---R-- 200 200 000 - 0
- tr '\012' ',' < "$TEMPOUT" >> "$RAWCOLS"
- echo "" >> "$RAWCOLS"
- rmtmp "$TEMPOUT"
- }
- ###
- # Function run_device_type
- # Prints device rows
- ###
- run_device_type()
- {
- for i in $(seq 0 32); do
- DEVNAME="$1$i"
- CURRENT="/dev/$DEVNAME"
- DATAFILE="$TMP_PRE-smartctl.$DEVNAME.log"
- if [ -c "$CURRENT" ]
- then
- #echo "[SMART] $CURRENT"
- smartctl -x $CURRENT > $DATAFILE
- run_attrs "$DATAFILE" "$DEVNAME"
- rmtmp "$DATAFILE"
- fi
- done
- }
- ###
- ### Actual Script Starts Here
- ###
- RAWCOLS="$TMP_PRE-smart.csv"
- if [ -f "$RAWCOLS" ]; then
- rmtmp "$RAWCOLS"
- fi
- # Print header row
- DEVNAME="header"
- run_attrs xxx "Device"
- # run each kind of device (ada, da)
- for tt in $DEVICE_TYPES; do
- run_device_type "$tt"
- done
- # Move temp file into place
- mv "$RAWCOLS" "$OUTPUT_NAME"
- # call the function at the top to make
- # this script easier to edit!
- do_something_with_OUTPUT_NAME
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement