From 5f1e012346c5e90200e0602dd758552fb4273f4c Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Sat, 22 Nov 2014 17:54:09 +0000 Subject: [PATCH] Add smart-health-check --- bin/smart-health-check | 59 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 bin/smart-health-check diff --git a/bin/smart-health-check b/bin/smart-health-check new file mode 100755 index 0000000..0557b05 --- /dev/null +++ b/bin/smart-health-check @@ -0,0 +1,59 @@ +#! /usr/bin/env bash + +if [ -z $1 ]; then + echo "usage: smart-health-check " + exit 1 +fi + +trim() { + local string="$@" + string="${string#"${string%%[![:space:]]*}"}" + string="${string%"${string##*[![:space:]]}"}" + echo -n "$string" +} + +check-status() { + local raw_status="$(smartctl -H $1)" + local status="$(echo "$raw_status" | grep "^SMART overall-health")" + + if [[ "$(trim "$status")" != *"PASSED" ]]; then + echo "$(tput setaf 1)SMART Status: FAIL:$(tput sgr0)" \ + "smartctl -H $1:" + echo "" + echo "$raw_status" + exit 3 + fi +} + +check-attributes() { + local attr + local fail=0 + local raw_status="$(smartctl -A $1)" + local report="$(echo "$raw_status" | grep "^ID# ")" + + for id in 1 5 10 196 198; do + attr="$(echo "$raw_status" | grep "^\s*${id}\s")" + if [ -n "$(trim "$attr")" ] && [[ "$(trim "$attr")" != *" 0" ]]; then + fail=1 + report="$report\n$attr" + fi + done + + if [ $fail != 0 ]; then + echo "$(tput setaf 2)SMART Status: PASSED:$(tput sgr0)" \ + "$(tput setaf 1)- may FAIL soon:$(tput sgr0)" \ + "smartctl -A $1:" + echo "" + echo -e "$report" + exit 2 + fi +} + +main() { + check-status $1 + check-attributes $1 + echo "SMART Status: $(tput setaf 2)PASSED$(tput sgr0)" +} + + +main $@