New function to regenerate a file if too old

This commit is contained in:
Jeremy Gardais 2020-03-11 10:52:44 +01:00
parent af5c5d4ef4
commit d49b19bdc4
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 41 additions and 0 deletions

View File

@ -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).