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

318 lines
11 KiB
Bash
Raw Normal View History

2020-06-15 15:48:09 +02:00
#!/bin/sh
# This script will check:
## every 10hours if upgrade is available for local Maco script (green or yellow)
## if normal upgrade is planned (green or yellow)
## if urgent upgrade is planned (green or yellow)
## status of the last update (green, yellow or red)
2020-06-15 15:48:09 +02:00
# 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"
readonly MACO_VERSION_RESULT="${XYMONTMP}/${MACHINEDOTS}.${TEST}.maco_version"
2020-06-15 15:48:09 +02:00
# By default, don't empty files newer than 10hours (600 minutes)
readonly DEFAULT_MTIME_MINUTES="600"
2021-01-13 12:40:03 +01:00
# Maco
readonly MACO_LOCAL_DIR="/opt/maco"
readonly MACO_PENDING_UPDATE_FILE="${MACO_LOCAL_DIR}/.maco.upgrade"
2021-01-13 12:57:40 +01:00
readonly MACO_PENDING_URGENT_UPDATE_FILE="${MACO_LOCAL_DIR}/.maco.urgent.upgrade"
readonly MACO_STATUS_FILE="/var/fr.univ-rennes1.ipr.maco.machinestate.txt"
2021-01-13 12:40:03 +01:00
# 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"
# }}}
2020-06-15 15:48:09 +02:00
# }}}
# 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
}
# }}}
2021-01-13 13:02:32 +01:00
# Add HTML header {{{
add_header()
{
echo ""
#echo "<FONT SIZE=+2><b>$1</b></FONT> ($2)<BR>"
echo "<FONT SIZE=+2><b>$1</b></FONT> <BR>"
# If you do not want the header in a bigger font use line below instead
#echo "<b>$1</b> ($2)"
# If you want the "Paul Luzzi" look uncomment this section and comment
# out the above sections:
#echo "<P><DIV ALIGN=\"CENTER\"><HR>"
#echo "<B>============== $1 ==============</B>"
#echo "<B>--- ($2) ---</B>"
#echo "<HR></DIV>"
#echo "<BLOCKQUOTE>"
}
# }}}
# Compare current Maco version to last available version {{{
check_maco_version() {
2020-06-15 15:48:09 +02:00
maco_update_script_path="/opt/repos/ipr.scripts/cluster/maco.check.update.sh"
maco_update_script_options="--simulate"
2020-06-15 15:48:09 +02:00
## Run update_script and store result in MACO_VERSION_RESULT file
debug_message "check_maco_version \
Run \"${maco_update_script_path}\" script with \"${maco_update_script_options}\" options."
sh "${maco_update_script_path}" "${maco_update_script_options}" >> "${MACO_VERSION_RESULT}"
2020-06-15 15:48:09 +02:00
}
# }}}
# Analyze maco result to define colors to send to Xymon {{{
analyze_maco_update() {
2020-06-15 15:48:09 +02:00
debug_message "analyze_maco_update \
Try to detect Maco update with ${MACO_VERSION_RESULT} content."
2020-06-15 15:48:09 +02:00
if grep -qE '^Local.* up to date.*' "${MACO_VERSION_RESULT}"
2020-06-15 15:48:09 +02:00
then
## Add color and message to PLUGIN_RESULT for detailed informations
sed "s/^/\&${GREEN_PIC} /" "${MACO_VERSION_RESULT}" >> "${PLUGIN_RESULT}"
## Add information to PLUGIN_STATE
2021-01-13 12:40:03 +01:00
echo "36${GREEN_PIC} Maco is uptodate" >> "${PLUGIN_STATE}"
elif grep -qE '^Latest.* available.*' "${MACO_VERSION_RESULT}"
2020-06-15 15:48:09 +02:00
then
sed "s/^/\&${YELLOW_PIC} /" "${MACO_VERSION_RESULT}" >> "${PLUGIN_RESULT}"
2021-01-13 12:40:03 +01:00
echo "26${YELLOW_PIC} Maco needs upgrade" >> "${PLUGIN_STATE}"
elif grep -qE '^Urgent.* available.*' "${MACO_VERSION_RESULT}"
2020-06-15 15:48:09 +02:00
then
sed "s/^/\&${YELLOW_PIC} /" "${MACO_VERSION_RESULT}" >> "${PLUGIN_RESULT}"
echo "25${YELLOW_PIC} Maco needs urgent upgrade" >> "${PLUGIN_STATE}"
2020-06-15 15:48:09 +02:00
else
sed "s/^/\&${CLEAR_PIC} /" "${MACO_VERSION_RESULT}" >> "${PLUGIN_RESULT}"
2021-01-13 12:40:03 +01:00
echo "46${CLEAR_PIC} Maco update state is unknown" >> "${PLUGIN_STATE}"
fi
}
# }}}
# Verify if a normal Maco update is planned {{{
check_pending_update() {
if [ -f "${MACO_PENDING_UPDATE_FILE}" ]; then
debug_message "check_pending_update \
A normal Maco update is planned."
## Add color and message to PLUGIN_RESULT for detailed informations
echo "${YELLOW_PIC} A normal update is planned (${MACO_PENDING_UPDATE_FILE} file exists)." >> "${PLUGIN_RESULT}"
## Add information to PLUGIN_STATE
echo "24${YELLOW_PIC} A normal update is planned" >> "${PLUGIN_STATE}"
else
debug_message "check_pending_update \
No Maco normal update is planned."
## Add color and message to PLUGIN_RESULT for detailed informations
echo "${GREEN_PIC} No Maco normal update is planned (${MACO_PENDING_UPDATE_FILE} file doesn't exists)." >> "${PLUGIN_RESULT}"
## No need to add information to PLUGIN_STATE
2020-06-15 15:48:09 +02:00
fi
2021-01-13 12:57:40 +01:00
}
# }}}
# Verify if a urgent Maco update is planned {{{
check_pending_urgent_update() {
if [ -f "${MACO_PENDING_URGENT_UPDATE_FILE}" ]; then
debug_message "check_pending_urgent_update \
An urgent Maco update is planned."
## Add color and message to PLUGIN_RESULT for detailed informations
echo "${YELLOW_PIC} An urgent update is planned (${MACO_PENDING_URGENT_UPDATE_FILE} file exists)." >> "${PLUGIN_RESULT}"
2021-01-13 12:57:40 +01:00
## Add information to PLUGIN_STATE
echo "23${YELLOW_PIC} An urgent update is planned" >> "${PLUGIN_STATE}"
2021-01-13 12:57:40 +01:00
else
debug_message "check_pending_urgent_update \
No Maco urgent update is planned."
## Add color and message to PLUGIN_RESULT for detailed informations
echo "${GREEN_PIC} No Maco urgent update is planned (${MACO_PENDING_URGENT_UPDATE_FILE} file doesn't exists)." >> "${PLUGIN_RESULT}"
## No need to add information to PLUGIN_STATE
fi
}
# }}}
2021-01-14 11:59:44 +01:00
# Verify the status of last Maco update {{{
check_last_update_status() {
## Check if Maco status file is present and readable
if [ -r "${MACO_STATUS_FILE}" ]; then
debug_message "check_last_update_status \
Maco status file (${MACO_STATUS_FILE}) exists."
local_maco_status=$(grep --max-count=1 -- MacoStatus "${MACO_STATUS_FILE}" | cut --delimiter="=" --fields=2)
if [ "${local_maco_status}" = "last-update-succeeded" ]; then
## Add color and message to PLUGIN_RESULT for detailed informations
echo "${GREEN_PIC} Last Maco update succeed (MacoStatus=${local_maco_status})." >> "${PLUGIN_RESULT}"
## Add information to PLUGIN_STATE
echo "32${GREEN_PIC} Maco is uptodate and last update succeed" >> "${PLUGIN_STATE}"
elif [ "${local_maco_status}" = "update-process-running" ]; then
echo "${YELLOW_PIC} Last Maco update is in progress (MacoStatus=${local_maco_status})." >> "${PLUGIN_RESULT}"
echo "22${YELLOW_PIC} Maco update is running" >> "${PLUGIN_STATE}"
elif [ "${local_maco_status}" = "waiting-for-additional-maco-run" ]; then
echo "${YELLOW_PIC} Last Maco update wait for an extra run (MacoStatus=${local_maco_status})." >> "${PLUGIN_RESULT}"
echo "22${YELLOW_PIC} Maco update needs an extra run" >> "${PLUGIN_STATE}"
elif [ "${local_maco_status}" = "last-update-failed" ]; then
echo "${RED_PIC} Last Maco update failed (MacoStatus=${local_maco_status})." >> "${PLUGIN_RESULT}"
echo "12${RED_PIC} Maco update failed" >> "${PLUGIN_STATE}"
else
echo "${RED_PIC} Maco last update status is unknown or absent (MacoStatus=${local_maco_status})." >> "${PLUGIN_RESULT}"
echo "13${RED_PIC} Maco last update status is unknown or absent" >> "${PLUGIN_STATE}"
fi
else
debug_message "check_last_update_status \
Maco status file (${MACO_STATUS_FILE}) doesn't exists."
echo "${RED_PIC} Maco last update status is unknown (${MACO_STATUS_FILE} doesn't exists or isn't available)." >> "${PLUGIN_RESULT}"
echo "14${RED_PIC} Maco last update status is unknown" >> "${PLUGIN_STATE}"
fi
2020-06-15 15:48:09 +02:00
}
# }}}
# Send plugin results to Xymon server {{{
send_result_to_xymon_server() {
# Set the main color according to the highest alert
2021-01-13 13:48:33 +01:00
COLOR=$(< "${PLUGIN_STATE}" awk '{print $1}' | sort | uniq | head -1 | cut -c4-)
SUBJECT=$(< "${PLUGIN_STATE}" sort | uniq | head -1 | cut -d" " -f2-)
2020-06-15 15:48:09 +02:00
# Send informations to Xymon server
$XYMON "${XYMSRV}" "status ${MACHINE}.${TEST} ${COLOR} ${SUBJECT}
$(cat ${PLUGIN_RESULT})"
2021-01-13 11:05:11 +01:00
# For testing only
#echo "$XYMON ${XYMSRV} status ${MACHINE}.${TEST} ${COLOR} ${SUBJECT}
#$(cat ${PLUGIN_RESULT})" > /tmp/xymon.maco.tmp
2020-06-15 15:48:09 +02:00
}
# }}}
main() { # {{{
# Create or empty previous file only if too old
regenerate_if_too_old "${MACO_VERSION_RESULT}"
2021-01-14 11:59:44 +01:00
## If MACO_VERSION_RESULT file is empty
if is_file_empty "${MACO_VERSION_RESULT}"; then
2021-01-14 11:59:44 +01:00
## (re)check Maco update
check_maco_version
2021-01-14 11:59:44 +01:00
fi
2020-06-15 15:48:09 +02:00
## Add HTML header for this part
add_header "Compare version" >> "${PLUGIN_RESULT}"
## Analyze Maco update result (add color,…)
analyze_maco_update
## Add HTML header for this part
add_header "Pending update" >> "${PLUGIN_RESULT}"
## Check for pending update for Maco
check_pending_update
## Check for urgent pending update for Maco
check_pending_urgent_update
## Add HTML header for this part
add_header "Last update" >> "${PLUGIN_RESULT}"
## Check status of last update
check_last_update_status
# Send data to Xymon server then
## delete useless files
2020-06-15 15:48:09 +02:00
## and exit
send_result_to_xymon_server \
&& rm -f -- "${PLUGIN_RESULT}" "${PLUGIN_STATE}" \
2020-06-15 15:48:09 +02:00
&& exit 0
}
# }}}
main
exit 255