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
Branches
No related tags found
1 merge request!271Update docs
......@@ -38,11 +38,14 @@
# 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
# 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
self_APPVERSION=1.6
typeset -i iFound=0
typeset -i iErrors=0
sOut=
......@@ -56,6 +59,35 @@ export PATH
# 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(){
local _mydevice=$1
local _moreparam
......@@ -74,7 +106,8 @@ function checkDrive(){
sLabel="^SMART.*Health"
sOK="(ok|passed)"
sOut="$sOut ; ${device}:"
test -n "$sOut" && sOut="$sOut ; "
sOut="${device}:"
ls -ld $device >/dev/null 2>&1
......@@ -91,7 +124,7 @@ function checkDrive(){
# --- check health
moreparam=$( detectParam $device )
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=$?
echo $device - rc=$rcs >>$tmpfile
echo >>$tmpfile
......@@ -103,7 +136,7 @@ function checkDrive(){
else
grep -i "$sLabel" $tmpfile >/dev/null
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}"
echo $status | grep -Ei "$sOK" >>$tmpfile
if [ $? -ne 0 ]; then
......@@ -124,10 +157,26 @@ function checkDrive(){
fi
}
function listDevices(){
sudo -n smartctl --scan | grep -v "/dev/bus/"
}
# ----------------------------------------------------------------------
# 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
rm -f $tmpDetailsfile 2>/dev/null
......@@ -137,8 +186,15 @@ if ! sudo -n smartctl -h >/dev/null 2>&1; then
ph.abort "UNKNOWN: No sudo permissions to execute smartctl."
fi
if [ "$customaction" == "list" ]; then
echo "Devices to scan:"
listDevices | sed "s#^#- #"
exit 0
fi
# --- 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
iFound=$iFound+1
......@@ -154,12 +210,9 @@ do
checkDrive $mydevice
done
ph.status "SMART check on $iFound HDs - $iErrors errors - $sOut"
cat $tmpDetailsfile
rm -f $tmpDetailsfile
ph.status "SMART check on $iFound Disks - $iErrors errors - $sOut"
cat $tmpDetailsfile 2>&1 && rm -f $tmpDetailsfile
ph.exit
# ----------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment