From 99e4b6e7677e34dd33eb3dbd7ba0c5a93ce60105 Mon Sep 17 00:00:00 2001 From: Stephane Gourichon Date: Sun, 24 Mar 2024 17:13:58 +0100 Subject: [PATCH] Script to install monitoring task. --- tools/monitoring__crontask_install.sh | 52 +++++++++++++++++++++++++++ tools/run_and_output_only_on_error.sh | 22 ++++++++++++ 2 files changed, 74 insertions(+) create mode 100755 tools/monitoring__crontask_install.sh create mode 100755 tools/run_and_output_only_on_error.sh diff --git a/tools/monitoring__crontask_install.sh b/tools/monitoring__crontask_install.sh new file mode 100755 index 0000000..a7a13fa --- /dev/null +++ b/tools/monitoring__crontask_install.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +if [[ "$#" != 1 ]] +then + echo >&2 "usage:" + echo >&2 "$0 install" + echo >&2 "$0 uninstall" + exit 1 +fi + +if [[ "$1" == "install" ]] +then + INSTALL=true +elif [[ "$1" == "uninstall" ]] +then + INSTALL=false +else + echo >&2 "unknown argument : $1" + exit 1 +fi + +cd -P "$(dirname "$(readlink -f "$0")" )" + +if [[ "$PWD" != "$(printf %q "$PWD" )" ]] +then + echo >&2 "WARNING: path seems unsafe: $PWD" +fi + +SCRIPT_ROOT="$PWD" +cd .. +INFRA_ROOT="$PWD" + +CRON_LINES=() + +if [[ "$INSTALL" == true ]] +then + while IFS="" read -u 3 -r MONITOR_SCRIPT_PATH + do + if [[ "$MONITOR_SCRIPT_PATH" != "$(printf %q "$MONITOR_SCRIPT_PATH" )" ]] + then + echo >&2 "WARNING: path seems unsafe: $MONITOR_SCRIPT_PATH" + fi + + CRON_LINES+=( "0 0 * * * $SCRIPT_ROOT/run_and_output_only_on_error.sh $INFRA_ROOT/$MONITOR_SCRIPT_PATH # monitoring_crontask" ) + + done 3< <( find */ -iname "monitor_check_once.sh" ) +fi + +{ + crontab -l | fgrep -v "# monitoring_crontask" + printf '%s\n' "${CRON_LINES[@]}" +} | crontab - diff --git a/tools/run_and_output_only_on_error.sh b/tools/run_and_output_only_on_error.sh new file mode 100755 index 0000000..8b75bf6 --- /dev/null +++ b/tools/run_and_output_only_on_error.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +set -eu + +LOGFILENAME=$(mktemp /tmp/run_and_output_only_on_error_XXXXXX) +# set -xv + +trap "rm -f ${LOGFILENAME}" EXIT + +if + ( + $@ + ) > "${LOGFILENAME}" 2>&1 +then + true +else + echo + echo "Error on running: $@" + echo + echo "Here's the program's output (out and err combined)" + cat "${LOGFILENAME}" +fi