Manage files with variables...

This commit is contained in:
Jeremy Gardais 2020-02-28 15:14:36 +01:00
parent 4d90168df5
commit ff84243229
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 16 additions and 12 deletions

View File

@ -32,6 +32,10 @@ c_reset='\033[0m'
plugin_name=$(basename "${0}") plugin_name=$(basename "${0}")
plugin_result="/tmp/dres"
plugin_state="/tmp/dcheck"
device_list="/tmp/dscan"
# ]]] # ]]]
# Functions # Functions
@ -105,15 +109,15 @@ choose_correct_type() {
## ]]] ## ]]]
# Create or empty previous files # Create or empty previous files
true > /tmp/dres true > "${plugin_result}"
true > /tmp/dcheck true > "${plugin_state}"
true > /tmp/dscan true > "${device_list}"
# Get the list of all available devices # Get the list of all available devices
smartctl --scan >> /tmp/dscan smartctl --scan >> "${device_list}"
# If the file is not empty # If the file is not empty
if test -s /tmp/dscan; then if test -s "${device_list}"; then
while IFS= read -r LINE; do while IFS= read -r LINE; do
## Get device path ## Get device path
DISK=$(echo "${LINE}" | cut -d" " -f1) DISK=$(echo "${LINE}" | cut -d" " -f1)
@ -184,7 +188,7 @@ https://www.smartmontools.org/wiki/FAQ#SmartmontoolsDatabase"
fi fi
## Avoid duplicate device ## Avoid duplicate device
if ! grep -q "${DID}" /tmp/dres; then if ! grep -q "${DID}" "${plugin_result}"; then
## For summary ## For summary
echo "${COLOR} $DISK ${TYPE}" echo "${COLOR} $DISK ${TYPE}"
@ -200,22 +204,22 @@ https://www.smartmontools.org/wiki/FAQ#SmartmontoolsDatabase"
echo "------------------------------------------------------------" echo "------------------------------------------------------------"
echo "" echo ""
echo "" echo ""
} >> /tmp/dres } >> "${plugin_result}"
fi fi
done < /tmp/dscan >> /tmp/dcheck done < "${device_list}" >> "${plugin_state}"
# If the file is empty # If the file is empty
else else
echo "1&red Error while scanning devices with smartctl" >> /tmp/dcheck echo "1&red Error while scanning devices with smartctl" >> "${plugin_state}"
fi fi
# Set the global color according to the highest alert # Set the global color according to the highest alert
COLOR=$(< /tmp/dcheck awk '{print $1}' | sort | uniq | head -1 | cut -c3-) COLOR=$(< "${plugin_state}" awk '{print $1}' | sort | uniq | head -1 | cut -c3-)
# Send informations to Xymon server # Send informations to Xymon server
$XYMON "${XYMSRV}" "status ${MACHINE}.${plugin_name} ${COLOR} SMART health check $XYMON "${XYMSRV}" "status ${MACHINE}.${plugin_name} ${COLOR} SMART health check
$(< /tmp/dcheck cut -c2-) $(< "${plugin_state}" cut -c2-)
==================== Detailed status ==================== ==================== Detailed status ====================
@ -223,6 +227,6 @@ $(cat /tmp/dres)
" "
# Clean temp files # Clean temp files
rm -f -- /tmp/dres /tmp/dcheck /tmp/dscan rm -f -- "${plugin_result}" "${plugin_state}" "${device_list}"
exit 0 exit 0