refactor logging

use printf instead of echo to improve portability
rename echo_str to log_info
create log_header function to reduce code duplication
This commit is contained in:
Michael Schnerring
2020-09-08 21:45:19 +02:00
parent 8b3c16cebf
commit 3a21b21eca
+47 -42
View File
@@ -259,14 +259,31 @@ Poll_Interval_Seconds=15
# #
######################################################################## ########################################################################
echo_str() ##################################################
# Log informational message.
# Globals:
# Log_File
# Arguments:
# Message to log.
# Outputs:
# Write message to stdout and log file.
##################################################
log_info()
{ {
echo "$1" | tee -a "$Log_File" now="$(date +"%F %T %Z")"
printf "%s\n" "[${now}] $1" | tee -a "${Log_File}"
} }
push_header() ##################################################
# Log emphasized header message.
# Arguments:
# Message to log.
##################################################
log_header()
{ {
echo_str "+-----------------------------------------------------------------------------" log_info "+-----------------------------------------------------------------------------"
log_info "+ $1"
log_info "+-----------------------------------------------------------------------------"
} }
################################################## ##################################################
@@ -288,70 +305,64 @@ poll_selftest_complete()
smartctl --all "/dev/${Drive}" | grep -i "The previous self-test routine completed" > /dev/null 2<&1 smartctl --all "/dev/${Drive}" | grep -i "The previous self-test routine completed" > /dev/null 2<&1
l_status=$? l_status=$?
if [ "${l_status}" -eq 0 ]; then if [ "${l_status}" -eq 0 ]; then
echo_str "SMART self-test succeeded" log_info "SMART self-test succeeded"
return 0 return 0
fi fi
smartctl --all "/dev/${Drive}" | grep -i "of the test failed." > /dev/null 2<&1 smartctl --all "/dev/${Drive}" | grep -i "of the test failed." > /dev/null 2<&1
l_status=$? l_status=$?
if [ "${l_status}" -eq 0 ]; then if [ "${l_status}" -eq 0 ]; then
echo_str "SMART self-test failed" log_info "SMART self-test failed"
return 0 return 0
fi fi
sleep "${Poll_Interval_Seconds}" sleep "${Poll_Interval_Seconds}"
l_poll_duration_seconds="$(( l_poll_duration_seconds + Poll_Interval_Seconds ))" l_poll_duration_seconds="$(( l_poll_duration_seconds + Poll_Interval_Seconds ))"
done done
echo_str "SMART self-test timeout threshold exceeded" log_info "SMART self-test timeout threshold exceeded"
return 1 return 1
} }
run_short_test() run_short_test()
{ {
push_header log_header "Run SMART short test on drive /dev/${Drive}"
echo_str "+ Run SMART short test on drive /dev/${Drive}: $(date)"
push_header
if [ "${Dry_Run}" -eq 0 ]; then if [ "${Dry_Run}" -eq 0 ]; then
smartctl -t short /dev/"$Drive" smartctl -t short /dev/"$Drive"
echo_str "Short test started, sleeping ${Short_Test_Seconds} seconds until it finishes" log_info "Short test started, sleeping ${Short_Test_Seconds} seconds until it finishes"
sleep ${Short_Test_Seconds} sleep ${Short_Test_Seconds}
poll_selftest_complete poll_selftest_complete
smartctl -l selftest /dev/"$Drive" | tee -a "$Log_File" smartctl -l selftest /dev/"$Drive" | tee -a "$Log_File"
else else
echo_str "Dry run: would start the SMART short test and sleep ${Short_Test_Seconds} seconds until the test finishes" log_info "Dry run: would start the SMART short test and sleep ${Short_Test_Seconds} seconds until the test finishes"
fi fi
echo_str "Finished SMART short test on drive /dev/${Drive}: $(date)" log_info "Finished SMART short test on drive /dev/${Drive}: $(date)"
} }
run_extended_test() run_extended_test()
{ {
push_header log_header "Run SMART extended test on drive /dev/${Drive}"
echo_str "+ Run SMART extended test on drive /dev/${Drive}: $(date)"
push_header
if [ "${Dry_Run}" -eq 0 ]; then if [ "${Dry_Run}" -eq 0 ]; then
smartctl -t long /dev/"$Drive" smartctl -t long /dev/"$Drive"
echo_str "Extended test started, sleeping ${Extended_Test_Seconds} seconds until it finishes" log_info "Extended test started, sleeping ${Extended_Test_Seconds} seconds until it finishes"
sleep ${Extended_Test_Seconds} sleep ${Extended_Test_Seconds}
poll_selftest_complete poll_selftest_complete
smartctl -l selftest /dev/"$Drive" | tee -a "$Log_File" smartctl -l selftest /dev/"$Drive" | tee -a "$Log_File"
else else
echo_str "Dry run: would start the SMART extended test and sleep ${Extended_Test_Seconds} seconds until the test finishes" log_info "Dry run: would start the SMART extended test and sleep ${Extended_Test_Seconds} seconds until the test finishes"
fi fi
echo_str "Finished SMART extended test on drive /dev/${Drive}: $(date)" log_info "Finished SMART extended test on drive /dev/${Drive}: $(date)"
} }
run_badblocks_test() run_badblocks_test()
{ {
push_header log_header "Run badblocks test on drive /dev/${Drive}"
echo_str "+ Run badblocks test on drive /dev/${Drive}: $(date)"
push_header
if [ "${Dry_Run}" -eq 0 ]; then if [ "${Dry_Run}" -eq 0 ]; then
# #
# This is the command which erases all data on the disk: # This is the command which erases all data on the disk:
# #
badblocks -b 4096 -wsv -e 1 -o "$BB_File" /dev/"$Drive" badblocks -b 4096 -wsv -e 1 -o "$BB_File" /dev/"$Drive"
else else
echo_str "Dry run: would run badblocks -b 4096 -wsv -e 1 -o ${BB_File} /dev/${Drive}" log_info "Dry run: would run badblocks -b 4096 -wsv -e 1 -o ${BB_File} /dev/${Drive}"
fi fi
echo_str "Finished badblocks test on drive /dev/${Drive}: $(date)" log_info "Finished badblocks test on drive /dev/${Drive}: $(date)"
} }
######################################################################## ########################################################################
@@ -364,19 +375,17 @@ if [ -e "$Log_File" ]; then
rm "$Log_File" rm "$Log_File"
fi fi
push_header log_header "Started burn-in of /dev/${Drive}"
echo_str "+ Started burn-in of /dev/${Drive} : $(date)"
push_header
echo_str "Host: $(hostname)" log_info "Host: $(hostname)"
echo_str "Drive Model: ${Disk_Model}" log_info "Drive Model: ${Disk_Model}"
echo_str "Serial Number: ${Serial_Number}" log_info "Serial Number: ${Serial_Number}"
echo_str "Short test duration: ${Short_Test_Minutes} minutes" log_info "Short test duration: ${Short_Test_Minutes} minutes"
echo_str "Short test sleep duration: ${Short_Test_Seconds} seconds" log_info "Short test sleep duration: ${Short_Test_Seconds} seconds"
echo_str "Extended test duration: ${Extended_Test_Minutes} minutes" log_info "Extended test duration: ${Extended_Test_Minutes} minutes"
echo_str "Extended test sleep duration: ${Extended_Test_Seconds} seconds" log_info "Extended test sleep duration: ${Extended_Test_Seconds} seconds"
echo_str "Log file: ${Log_File}" log_info "Log file: ${Log_File}"
echo_str "Bad blocks file: ${BB_File}" log_info "Bad blocks file: ${BB_File}"
# Run the test sequence: # Run the test sequence:
run_short_test run_short_test
@@ -386,14 +395,10 @@ run_badblocks_test
run_extended_test run_extended_test
# Emit full device information to log: # Emit full device information to log:
push_header log_header "SMART information for drive /dev/${Drive}"
echo_str "+ SMART information for drive /dev/${Drive}: $(date)"
push_header
smartctl -x -v 7,hex48 /dev/"$Drive" | tee -a "$Log_File" smartctl -x -v 7,hex48 /dev/"$Drive" | tee -a "$Log_File"
push_header log_header "Finished burn-in of /dev/${Drive}"
echo_str "+ Finished burn-in of /dev/${Drive} : $(date)"
push_header
# Clean up the log file: # Clean up the log file: