#!/bin/bash # ====================================================================== # # NAGIOS CLIENT CHECK :: check smart status # # REQUIREMENTS # - a physical machine (no vm) # - sudo permissions for monitoring user to "smartctl" binary # # ---------------------------------------------------------------------- # short status in the 1st line: # - count of available harddisks # - found disks with an error # - each device and status # - PASSED - tests in SMART check were successful # - OK - health status OK; No Self-tests have been logged # - [not supported] - Disk available but no SMART data available (not supported) # - [no access] - disk device found but not accessible (maybe corrupt) # - [does not exist] - device does not exist in the system # ---------------------------------------------------------------------- # # ah=axel.hahn@iml.unibe.ch # ds=daniel.schueler@iml.unibe.ch # # 2016-12-23 v0.3 ah,ds # 2018-12-06 v0.4 ah,ds show details for each disc # 2020-02-27 v0.5 ah,ds add /sbin to PATH variable # 2020-03-05 v1.0 <axel.hahn@iml.unibe.ch> switch to ph.* helper functions # 2021-01-22 v1.1 <axel.hahn@iml.unibe.ch> add status [no access] # 2021-04-07 v1.2 <axel.hahn@iml.unibe.ch> update message if sudo smartctl -H fails # ====================================================================== . `dirname $0`/inc_pluginfunctions typeset -i iFound=0 typeset -i iErrors=0 sOut= sDetails= tmpDetailsfile=/tmp/smartstatus-all.log # fix for v0.5 PATH=$PATH:/sbin/ export PATH # ---------------------------------------------------------------------- # functions # ---------------------------------------------------------------------- function checkDrive(){ tmpfile=/tmp/smartstatus.log device=$1 sLabel="^SMART.*Health" sOK="(ok|passed)" sOut="$sOut | ${device}:" ls -ld $device >/dev/null 2>&1 if [ $? -eq 0 ]; then # --- write an info header for the current disc echo "---------------------------------------------------------------------- ">$tmpfile echo >>$tmpfile echo "$device" >>$tmpfile echo >>$tmpfile # sudo smartctl -i $device | grep Mode >>$tmpfile # echo >>$tmpfile # --- check health # sudo smartctl -T permissive -Ha $device >>$tmpfile sudo smartctl -Ha $device >>$tmpfile 2>&1 rcs=$? echo $device - rc=$rcs >>$tmpfile echo >>$tmpfile if [ $rcs -eq 2 ]; then iErrors=$iErrors+1 ph.setStatus "critical" sOut="$sOut [no access]" else grep -i "$sLabel" $tmpfile >/dev/null if [ $? -eq 0 ]; then status=`grep -i "$sLabel" $tmpfile | cut -f 2 -d ":"` sOut="$sOut ${status}" echo $status | egrep -i "$sOK" >>$tmpfile if [ $? -ne 0 ]; then iErrors=$iErrors+1 ph.setStatus "critical" fi else # status=`grep -i "" $tmpfile | cut -f 2 -d ":"` sOut="$sOut [not supported]" fi fi cat $tmpfile >>$tmpDetailsfile # echo >>$tmpfile # echo >>$tmpfile rm -f $tmpfile else sOut="$sOut [does not exist]" fi } # ---------------------------------------------------------------------- # main # ---------------------------------------------------------------------- ph.require smartctl rm -f $tmpDetailsfile 2>/dev/null for mydevice in `ls -1 /dev/sd* | grep -v "[0-9]"` do iFound=$iFound+1 # on first device: check permissions if [ $iFound -eq 1 ]; then sudo smartctl -H $mydevice >/dev/null 2>&1 if [ $? -ne 0 ]; then ph.abort "SKIP: smartctl was found - but did not respond results. Maybe no sudo permissions or additional parameters required. $( sudo smartctl -H $mydevice )" fi fi checkDrive $mydevice done ph.status "SMART check on $iFound HDs - $iErrors errors - $sOut" cat $tmpDetailsfile rm -f $tmpDetailsfile ph.exit # ----------------------------------------------------------------------