From d49b19bdc43f73ea246f9b4f0b42b8000190bb74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gardais=20J=C3=A9r=C3=A9my?= Date: Wed, 11 Mar 2020 10:52:44 +0100 Subject: [PATCH] New function to regenerate a file if too old --- xymon/plugins/client/ext/smartoverall | 41 +++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/xymon/plugins/client/ext/smartoverall b/xymon/plugins/client/ext/smartoverall index 3aa8350..7c3e00a 100755 --- a/xymon/plugins/client/ext/smartoverall +++ b/xymon/plugins/client/ext/smartoverall @@ -39,9 +39,50 @@ device_list="${XYMONTMP}/${MACHINEDOTS}.smartoverall.dscan" ## This file might be used by a more advanced script such as skazi0's one drivedb_list="${XYMONTMP}/${MACHINEDOTS}.smart.drivedb.list" +# By default, don't empty files newer than 10hours (600 minutes) +default_mtime_minutes="600" # ]]] # Functions +## Create or empty a file if it's too old [[[ +## First argument (required): Absolut path to the file +## Second argument (optionnal): Maximum number of minutes since last modification +regenerate_if_too_old() { + ## Set variables according to the number of passed arguments [[[ + case $# in + 0 ) + [ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG : regenerate_if_too_old func − Need at least 1 argument." + exit 1 + ;; + 1 ) + _file="${1}" + [ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG : regenerate_if_too_old func − Use default_mtime_minutes value: ${default_mtime_minutes}." + _max_mtime_minutes="${default_mtime_minutes}" + ;; + 2 ) + _file="${1}" + _max_mtime_minutes="${2}" + ;; + esac + ## ]]] + _current_timestamp=$(date "+%s") + _file_mtime_timestamp=$(stat --format="%Y" -- "${_file}") + + ## Substract last modification timestamp of the file to current timestamp + : $(( _file_mtime_seconds=_current_timestamp-_file_mtime_timestamp )) + ## Get maximum allowed mtime in seconds + : $(( _max_mtime_seconds=_max_mtime_minutes*60 )) + + ## Compare last modification mtime with the maximum allowed + if [ "${_file_mtime_seconds}" -gt "${_max_mtime_seconds}" ]; then + [ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG : regenerate_if_too_old func − Need to empty or create ${_file} last modification happened ${_file_mtime_seconds} seconds ago (maximum is ${_max_mtime_seconds})." + true > "${_file}" + else + [ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG : regenerate_if_too_old func − Don't need to empty ${_file} last modification happened ${_file_mtime_seconds} seconds ago (maximum is ${_max_mtime_seconds})." + fi + +} +## ]]] ## Test if a disk really support SMART [[[ ## Smartctl can give an health status even without a full support ## of SMART for some type (eg. scsi or megaraid).