scripts/xymon/plugins/client/ext/maco.sh

203 lines
5.7 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/sh
# This script will check if upgrade is available for local Maco script
# Vars {{{
readonly PROGNAME=$(basename "${0}")
[ -z "${DEBUG}" ] && DEBUG=1
## Xymon
readonly TEST="maco"
readonly PLUGIN_RESULT="${XYMONTMP}/${MACHINEDOTS}.${TEST}.plugin_result"
readonly PLUGIN_STATE="${XYMONTMP}/${MACHINEDOTS}.${TEST}.plugin_state"
readonly XYMON_USERNAME="xymon"
readonly XYMON_GROUPNAME="xymon"
# By default, don't empty files newer than 10hours (600 minutes)
readonly DEFAULT_MTIME_MINUTES="600"
# Temp files
readonly TEMP_MACO_UPDATE="${XYMONTMP}/${MACHINEDOTS}.${TEST}.maco_update"
# define colours for graphics {{{
# Comment these out if using older BB versions
CLEAR_PIC="&clear"
RED_PIC="&red"
YELLOW_PIC="&yellow"
GREEN_PIC="&green"
UNKNOWN_PIC="&purple"
# }}}
# }}}
# Print message with DEBUG prefix and color {{{
debug_message() {
local_debug_message="${1}"
## Print message if DEBUG is enable (=0)
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG ${PROGNAME}: ${local_debug_message}"
}
# }}}
# 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_message "regenerate_if_too_old \
Need at least 1 argument."
exit 1
;;
1 )
_file="${1}"
debug_message "regenerate_if_too_old \
Use DEFAULT_MTIME_MINUTES value: ${DEFAULT_MTIME_MINUTES}."
_max_mtime_minutes="${DEFAULT_MTIME_MINUTES}"
;;
2 )
_file="${1}"
_max_mtime_minutes="${2}"
;;
esac
## }}}
## If the file exists
if [ -f "${_file}" ]; then
_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_message "regenerate_if_too_old \
Need to empty ${_file} last modification happened ${_file_mtime_seconds} seconds ago (maximum is ${_max_mtime_seconds})."
true > "${_file}"
chown -- "${XYMON_USERNAME}":"${XYMON_GROUPNAME}" "${_file}"
else
debug_message "regenerate_if_too_old \
Don't need to empty ${_file} last modification happened ${_file_mtime_seconds} seconds ago (maximum is ${_max_mtime_seconds})."
fi
else ## Else if the file don't exists, just create it
debug_message "regenerate_if_too_old \
Need to create ${_file}."
true > "${_file}"
fi
}
# }}}
# Test if a file is empty {{{
is_file_empty() {
_file_empty=$1
if [ ! -s "${_file_empty}" ]; then
debug_message "is_file_empty \
${_file_empty} file is empty."
return 0
else
debug_message "is_file_empty \
${_file_empty} file is not empty."
return 1
fi
}
# }}}
# Get Maco update state {{{
get_maco_update() {
maco_update_script_path="/opt/repos/ipr.scripts/cluster/maco.check.update.sh"
maco_update_script_options="--simulate"
## Run update_script and store result in TEMP_MACO_UPDATE file
debug_message "get_maco_update \
Run \"${maco_update_script_path}\" script with \"${maco_update_script_options}\" options."
sh "${maco_update_script_path}" "${maco_update_script_options}" >> "${TEMP_MACO_UPDATE}"
}
# }}}
# Analyze maco result to define colors to send to Xymon {{{
analyze_maco_update() {
debug_message "analyze_maco_update \
Try to detect Maco update with ${TEMP_MACO_UPDATE} content."
if grep -qE '^Local.* up to date.*' "${TEMP_MACO_UPDATE}"
then
## Add color and message to PLUGIN_RESULT for detailed informations
sed "s/^/\&${GREEN_PIC} /" "${TEMP_MACO_UPDATE}" >> "${PLUGIN_RESULT}"
## Add information to PLUGIN_STATE
echo "3${GREEN_PIC} Maco is uptodate" >> "${PLUGIN_STATE}"
elif grep -qE '^Latest.* available.*' "${TEMP_MACO_UPDATE}"
then
sed "s/^/\&${YELLOW_PIC} /" "${TEMP_MACO_UPDATE}" >> "${PLUGIN_RESULT}"
echo "2${YELLOW_PIC} Maco needs upgrade" >> "${PLUGIN_STATE}"
elif grep -qE '^Urgent.* available.*' "${TEMP_MACO_UPDATE}"
then
sed "s/^/\&${RED_PIC} /" "${TEMP_MACO_UPDATE}" >> "${PLUGIN_RESULT}"
echo "1${RED_PIC} Maco needs urgent upgrade" >> "${PLUGIN_STATE}"
else
sed "s/^/\&${CLEAR_PIC} /" "${TEMP_MACO_UPDATE}" >> "${PLUGIN_RESULT}"
echo "4${CLEAR_PIC} Maco update state is unknown" >> "${PLUGIN_STATE}"
fi
}
# }}}
# Send plugin results to Xymon server {{{
send_result_to_xymon_server() {
# Set the main color according to the highest alert
COLOR=$(< "${PLUGIN_STATE}" awk '{print $1}' | sort | uniq | head -1 | cut -c3-)
SUBJECT=$(< "${PLUGIN_STATE}" sort | uniq | head -1 | cut -d" " -f2-)
# Send informations to Xymon server
$XYMON "${XYMSRV}" "status ${MACHINE}.${TEST} ${COLOR} ${SUBJECT}
==================== Detailed status ====================
$(cat ${PLUGIN_RESULT})"
# For testing only
#echo "$XYMON ${XYMSRV} status ${MACHINE}.${TEST} ${COLOR} ${SUBJECT}
#==================== Detailed status ====================
#$(cat ${PLUGIN_RESULT})" > /tmp/xymon.maco.tmp
}
# }}}
main() { # {{{
# Create or empty previous file only if too old
regenerate_if_too_old "${PLUGIN_RESULT}"
regenerate_if_too_old "${PLUGIN_STATE}"
## If any previous file is empty
if is_file_empty "${PLUGIN_RESULT}" || is_file_empty "${PLUGIN_STATE}"; then
## (re)check Maco update
get_maco_update
## Analyze Maco update result (add color,…)
analyze_maco_update
fi
# Send data to Xymon server
## and exit
send_result_to_xymon_server \
&& exit 0
}
# }}}
main
exit 255