Advertisement
Guest User

smart2csv.sh

a guest
Jan 20th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.40 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ## smart2csv.sh - version 0.2 - da support!
  4. ## Jacob Dilles - 1/12/2016
  5. ##
  6. ## This script populates a CSV file with the output
  7. ## of smartctl for several hdds at once. You can specify
  8. ## which attributes to use by editing the run_attrs function
  9.  
  10. DEVICE_TYPES="ada da"
  11. OUTPUT_TIME=`date "+%Y-%m-%d-%H%M"`
  12. OUTPUT_NAME="smartdump-$OUTPUT_TIME.csv"
  13.  
  14.  ###  
  15.  #   This function called last.
  16.  #
  17.  #   You can scp the $OUTPUT_NAME file somehere
  18.  #   or copy it to another directory
  19.  #   or delete it after printing it, etc.
  20.  ###
  21.  
  22. do_something_with_OUTPUT_NAME()
  23. {
  24.   echo "wrote SMART CSV to $OUTPUT_NAME"
  25. }
  26.  
  27.  
  28.  ###
  29.  #   This function called to remove temporary files
  30.  #   If you are having trouble and need to debug
  31.  #   it might be useful to add an echo here in place
  32.  #   of the rm, so you can see what the script is doing
  33.  ###
  34.  
  35. TMP_PRE="/tmp/smart2csv"
  36. rmtmp()
  37. {
  38.   echo ">rm $1"
  39.   rm "$1"
  40. }
  41.  
  42.  
  43. ###################################################
  44.  
  45. appnd()
  46. {
  47.  VALUE=`echo "$1" | sed 's/^ *//g' | sed 's/ *$//g'`
  48.  echo "\"$VALUE\"" >> "$TEMPOUT"
  49. }
  50.  
  51. col()
  52. {
  53. #echo "Column $1"
  54. if [ "$DEVNAME" = header ]; then
  55.   appnd "$1"
  56. else
  57.   VALUE=`cat "$DATAFILE" | grep "$1" | cut -f2 -d ":"`
  58.   appnd "$VALUE"
  59. fi
  60. }
  61.  
  62.  
  63. atr()
  64. {
  65. #echo "Attr $1"
  66.  
  67. if [ "$DEVNAME" = "header" ]; then
  68.   appnd "$1"
  69. else
  70.   VALUE=`cat "$DATAFILE" | grep "$1" | awk -F " " '{print $NF}'`
  71.   appnd "$VALUE"
  72. fi
  73.  
  74. }
  75.  
  76.  
  77.  
  78. # This function is where we define what columns we want to have
  79. run_attrs()
  80. {
  81.  
  82. echo "[SMART2CSV] row: $DEVNAME"
  83.  
  84. TEMPOUT="$TMP_PRE-$DEVNAME.cols"
  85.  
  86. #clear file
  87. echo "\"$2\"" > "$TEMPOUT"
  88.  
  89. col "Model Family"      # Western Digital Red
  90. col "Device Model"      # WDC WD20EFRX-68EUZN0
  91. col "Serial Number"     # WD-WCC4M7NPJF1N
  92. col "LU WWN Device Id"  # 5 0014ee 20cb4db10
  93. col "Firmware Version"  # 82.00A82
  94. col "User Capacity"     # 2,000,398,934,016 bytes [2.00 TB]
  95. col "Sector Sizes"      # 512 bytes logical, 4096 bytes physical
  96. col "Rotation Rate"     # 5400 rpm
  97. col "SATA Version is"   # SATA 3.0, 6.0 Gb/s (current: 6.0 Gb/s)
  98. #...
  99. col "Lifetime    Min/Max Temperature"                   # 22/30 Celsius
  100. col "Under/Over Temperature Limit Count"                # 0/0
  101. col "SMART overall-health self-assessment test result"  # PASSED
  102.  
  103. atr "  1 Raw_Read_Error_Rate     "  # POSR-K   200   200   051    -    0
  104. atr "  3 Spin_Up_Time            "  # POS--K   176   176   021    -    4183
  105. atr "  4 Start_Stop_Count        "  # -O--CK   100   100   000    -    14
  106. atr "  5 Reallocated_Sector_Ct   "  # PO--CK   200   200   140    -    0
  107. atr "  7 Seek_Error_Rate         "  # -OSR-K   200   200   000    -    0
  108. atr "  9 Power_On_Hours          "  # -O--CK   100   100   000    -    154
  109. atr " 10 Spin_Retry_Count        "  # -O--CK   100   253   000    -    0
  110. atr " 11 Calibration_Retry_Count "  # -O--CK   100   253   000    -    0
  111. atr " 12 Power_Cycle_Count       "  # -O--CK   100   100   000    -    14
  112. atr "192 Power-Off_Retract_Count "  # -O--CK   200   200   000    -    10
  113. atr "193 Load_Cycle_Count        "  # -O--CK   200   200   000    -    36
  114. atr "194 Temperature_Celsius     "  # -O---K   119   116   000    -    28
  115. atr "196 Reallocated_Event_Count "  # -O--CK   200   200   000    -    0
  116. atr "197 Current_Pending_Sector  "  # -O--CK   200   200   000    -    0
  117. atr "198 Offline_Uncorrectable   "  # ----CK   100   253   000    -    0
  118. atr "199 UDMA_CRC_Error_Count    "  # -O--CK   200   200   000    -    1
  119. atr "200 Multi_Zone_Error_Rate   "  # ---R--   200   200   000    -    0
  120.  
  121. tr '\012' ',' < "$TEMPOUT" >> "$RAWCOLS"
  122. echo "" >> "$RAWCOLS"
  123. rmtmp "$TEMPOUT"
  124.  
  125. }
  126.  
  127. ###
  128. #   Function run_device_type
  129. #   Prints device rows
  130. ###
  131.  
  132. run_device_type()
  133. {
  134.  
  135. for i in $(seq 0 32); do
  136.  
  137. DEVNAME="$1$i"
  138. CURRENT="/dev/$DEVNAME"    
  139. DATAFILE="$TMP_PRE-smartctl.$DEVNAME.log"
  140.  
  141. if [ -c "$CURRENT" ]
  142. then
  143.  #echo "[SMART] $CURRENT"
  144.  smartctl -x $CURRENT > $DATAFILE
  145.  run_attrs "$DATAFILE" "$DEVNAME"
  146.  rmtmp "$DATAFILE"
  147.  
  148. fi
  149.  
  150. done
  151.  
  152. }
  153.  
  154. ###
  155. ###   Actual Script Starts Here
  156. ###
  157.  
  158. RAWCOLS="$TMP_PRE-smart.csv"
  159.  
  160. if [ -f "$RAWCOLS" ]; then
  161. rmtmp "$RAWCOLS"
  162. fi
  163.  
  164. # Print header row
  165. DEVNAME="header"
  166. run_attrs xxx "Device"
  167.  
  168. # run each kind of device (ada, da)
  169. for tt in $DEVICE_TYPES; do
  170.   run_device_type "$tt"
  171. done
  172.  
  173. # Move temp file into place
  174. mv "$RAWCOLS" "$OUTPUT_NAME"
  175.  
  176. # call the function at the top to make
  177. # this script easier to edit!
  178. do_something_with_OUTPUT_NAME
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement