Skip to content
Snippets Groups Projects
Commit 0e2ed820 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

add help page; use smartctl --scan to detect devices

parent 06a567a2
No related branches found
No related tags found
1 merge request!271Update docs
...@@ -38,11 +38,14 @@ ...@@ -38,11 +38,14 @@
# 2021-10-28 v1.3 <axel.hahn@iml.unibe.ch> detect -d param for smartctl # 2021-10-28 v1.3 <axel.hahn@iml.unibe.ch> detect -d param for smartctl
# 2022-07-08 v1.4 <axel.hahn@iml.unibe.ch> remove pipe in status line # 2022-07-08 v1.4 <axel.hahn@iml.unibe.ch> remove pipe in status line
# 2023-10-20 v1.5 <axel.hahn@unibe.ch> harden sudo command execution # 2023-10-20 v1.5 <axel.hahn@unibe.ch> harden sudo command execution
# 2024-06-07 v1.6 <axel.hahn@unibe.ch> add help page; use smartctl --scan to detect devices
# ====================================================================== # ======================================================================
. $(dirname $0)/inc_pluginfunctions . $(dirname $0)/inc_pluginfunctions
self_APPVERSION=1.6
typeset -i iFound=0 typeset -i iFound=0
typeset -i iErrors=0 typeset -i iErrors=0
sOut= sOut=
...@@ -56,6 +59,35 @@ export PATH ...@@ -56,6 +59,35 @@ export PATH
# functions # functions
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# show help
function showHelp(){
local self=$( basename $0 )
cat <<EOH
$( ph.showImlHelpHeader )
Show status of local S.M.A.R.T. devices.
SYNTAX:
$self [-h] [-l] [devices]
OPTIONS:
-h|--help show this help.
-l|--list list devices only.
PARAMETERS:
EXAMPLES
$self
Scan all local disks
$self -l
List all local disks without scanning them.
EOH
}
function detectParam(){ function detectParam(){
local _mydevice=$1 local _mydevice=$1
local _moreparam local _moreparam
...@@ -74,7 +106,8 @@ function checkDrive(){ ...@@ -74,7 +106,8 @@ function checkDrive(){
sLabel="^SMART.*Health" sLabel="^SMART.*Health"
sOK="(ok|passed)" sOK="(ok|passed)"
sOut="$sOut ; ${device}:" test -n "$sOut" && sOut="$sOut ; "
sOut="${device}:"
ls -ld $device >/dev/null 2>&1 ls -ld $device >/dev/null 2>&1
...@@ -91,7 +124,7 @@ function checkDrive(){ ...@@ -91,7 +124,7 @@ function checkDrive(){
# --- check health # --- check health
moreparam=$( detectParam $device ) moreparam=$( detectParam $device )
echo sudo smartctl -Ha $device $moreparam >>$tmpfile 2>&1 echo sudo smartctl -Ha $device $moreparam >>$tmpfile 2>&1
sudo smartctl -Ha $device $moreparam >>$tmpfile 2>&1 sudo -n smartctl -Ha $device $moreparam >>$tmpfile 2>&1
rcs=$? rcs=$?
echo $device - rc=$rcs >>$tmpfile echo $device - rc=$rcs >>$tmpfile
echo >>$tmpfile echo >>$tmpfile
...@@ -103,7 +136,7 @@ function checkDrive(){ ...@@ -103,7 +136,7 @@ function checkDrive(){
else else
grep -i "$sLabel" $tmpfile >/dev/null grep -i "$sLabel" $tmpfile >/dev/null
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
status=`grep -i "$sLabel" $tmpfile | cut -f 2 -d ":"` status=$( grep -i "$sLabel" $tmpfile | cut -f 2 -d ":")
sOut="$sOut ${status}" sOut="$sOut ${status}"
echo $status | grep -Ei "$sOK" >>$tmpfile echo $status | grep -Ei "$sOK" >>$tmpfile
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
...@@ -124,10 +157,26 @@ function checkDrive(){ ...@@ -124,10 +157,26 @@ function checkDrive(){
fi fi
} }
function listDevices(){
sudo -n smartctl --scan | grep -v "/dev/bus/"
}
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# main # main
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# parse params
customaction=
while [[ "$#" -gt 0 ]]; do case $1 in
-h|--help) showHelp; exit 0;;
-l|--list) customaction="list"; shift 1 ;;
*) if grep "^-" <<< "$1" >/dev/null ; then
echo; echo "ERROR: Unknown parameter: $1"; echo; exit 2
fi
break;
;;
esac; done
ph.require smartctl ph.require smartctl
rm -f $tmpDetailsfile 2>/dev/null rm -f $tmpDetailsfile 2>/dev/null
...@@ -137,8 +186,15 @@ if ! sudo -n smartctl -h >/dev/null 2>&1; then ...@@ -137,8 +186,15 @@ if ! sudo -n smartctl -h >/dev/null 2>&1; then
ph.abort "UNKNOWN: No sudo permissions to execute smartctl." ph.abort "UNKNOWN: No sudo permissions to execute smartctl."
fi fi
if [ "$customaction" == "list" ]; then
echo "Devices to scan:"
listDevices | sed "s#^#- #"
exit 0
fi
# --- loop over sd devices # --- loop over sd devices
for mydevice in $(ls -1 /dev/sd* | grep -v "[0-9]") # for mydevice in $(ls -1 /dev/sd* | grep -v "[0-9]")
for mydevice in $( listDevices | cut -f 1 -d " " )
do do
iFound=$iFound+1 iFound=$iFound+1
...@@ -154,12 +210,9 @@ do ...@@ -154,12 +210,9 @@ do
checkDrive $mydevice checkDrive $mydevice
done done
ph.status "SMART check on $iFound HDs - $iErrors errors - $sOut" ph.status "SMART check on $iFound Disks - $iErrors errors - $sOut"
cat $tmpDetailsfile cat $tmpDetailsfile 2>&1 && rm -f $tmpDetailsfile
rm -f $tmpDetailsfile
ph.exit ph.exit
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment