2019-01-25 17:28:29 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# usage
|
|
|
|
usage() {
|
2023-01-25 08:26:00 +00:00
|
|
|
USAGE=$(
|
|
|
|
cat <<EOF
|
2023-01-25 22:27:38 +00:00
|
|
|
Usage: check_updates
|
2019-01-25 17:28:29 +00:00
|
|
|
|
2023-01-25 01:16:01 +00:00
|
|
|
Sends out update notifications. Please see man check_updates.
|
2019-01-25 17:28:29 +00:00
|
|
|
EOF
|
2023-01-25 08:26:00 +00:00
|
|
|
)
|
|
|
|
echo "$USAGE"
|
2019-01-25 17:28:29 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 08:26:00 +00:00
|
|
|
set -e
|
2019-01-25 17:28:29 +00:00
|
|
|
|
|
|
|
# check for config file
|
2022-07-10 16:29:44 +00:00
|
|
|
apply_config() {
|
2023-01-25 08:26:00 +00:00
|
|
|
local config=$1
|
2022-07-10 16:29:44 +00:00
|
|
|
|
|
|
|
if [[ ! -f "$config" ]]; then
|
2023-01-25 08:26:00 +00:00
|
|
|
echo "No config file specified"
|
|
|
|
echo ""
|
|
|
|
usage
|
|
|
|
exit 1
|
2022-07-10 16:29:44 +00:00
|
|
|
fi
|
|
|
|
|
2023-01-25 08:26:00 +00:00
|
|
|
set -a
|
2022-07-10 18:47:46 +00:00
|
|
|
# shellcheck disable=SC1090
|
2023-01-25 08:26:00 +00:00
|
|
|
source "$config"
|
|
|
|
set +a
|
2022-07-10 16:29:44 +00:00
|
|
|
}
|
|
|
|
|
2019-01-25 17:28:29 +00:00
|
|
|
source_config() {
|
2023-01-25 22:27:38 +00:00
|
|
|
local configFallback=$1
|
|
|
|
local configGlobalFallback=$2
|
2022-07-10 16:29:44 +00:00
|
|
|
|
|
|
|
if [[ -f "$configFallback" ]]; then
|
2023-01-25 08:26:00 +00:00
|
|
|
apply_config "$configFallback"
|
|
|
|
return
|
2022-07-10 16:29:44 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -f "$configGlobalFallback" ]]; then
|
2023-01-25 08:26:00 +00:00
|
|
|
apply_config "$configGlobalFallback"
|
|
|
|
return
|
2022-07-10 16:29:44 +00:00
|
|
|
fi
|
2019-01-25 17:28:29 +00:00
|
|
|
}
|
2023-01-25 08:26:00 +00:00
|
|
|
|
|
|
|
apply_defaults() {
|
|
|
|
[[ -z ${CHECK_UPDATES_UPDATES_CMD} ]] && CHECK_UPDATES_UPDATES_CMD="/usr/bin/checkupdates"
|
|
|
|
[[ -z ${CHECK_UPDATES_UPDATES_AMOUNT_CMD} ]] && CHECK_UPDATES_UPDATES_AMOUNT_CMD="/usr/bin/checkupdates | wc -l"
|
|
|
|
}
|
|
|
|
|
|
|
|
check_required() {
|
|
|
|
if [[ -z ${CHECK_UPDATES_UPDATES_CMD} ]]; then
|
|
|
|
echo "CHECK_UPDATES_UPDATES_CMD is required"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [[ -z ${CHECK_UPDATES_UPDATES_AMOUNT_CMD} ]]; then
|
|
|
|
echo "CHECK_UPDATES_UPDATES_AMOUNT_CMD is required"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [[ -z ${CHECK_UPDATES_MAIL_ADDRESS} ]]; then
|
|
|
|
echo "CHECK_UPDATES_MAIL_ADDRESS is required"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
type hostname &> /dev/null || { echo "Requiring 'hostname' but it's not installed"; exit 1; }
|
|
|
|
type mailx &> /dev/null || { echo "Requiring 'mailx' but it's not installed"; exit 1; }
|
|
|
|
}
|
|
|
|
|
2023-01-25 22:27:38 +00:00
|
|
|
source_config "$HOME/.check_updates.conf" "/etc/check_updates.conf"
|
2023-01-25 08:26:00 +00:00
|
|
|
apply_defaults
|
|
|
|
check_required
|
|
|
|
|
|
|
|
HOSTNAME=$(hostname)
|
|
|
|
CHECK_UPDATES_UPDATES=$(eval "${CHECK_UPDATES_UPDATES_CMD}")
|
|
|
|
CHECK_UPDATES_UPDATES_AMOUNT=$(eval "${CHECK_UPDATES_UPDATES_AMOUNT_CMD}")
|
2019-01-25 17:28:29 +00:00
|
|
|
|
2023-01-25 08:26:00 +00:00
|
|
|
if [[ "$CHECK_UPDATES_UPDATES_AMOUNT" -gt "0" ]]; then
|
|
|
|
SUBJECT="[updates ${HOSTNAME}]"
|
|
|
|
MESSAGE=$(
|
|
|
|
cat <<EOF
|
|
|
|
There are ${CHECK_UPDATES_UPDATES_AMOUNT} updates available on ${HOSTNAME}.
|
2019-01-25 17:28:29 +00:00
|
|
|
|
2023-01-25 08:26:00 +00:00
|
|
|
${CHECK_UPDATES_UPDATES};
|
2019-01-25 17:28:29 +00:00
|
|
|
EOF
|
2023-01-25 08:26:00 +00:00
|
|
|
)
|
|
|
|
echo "$MESSAGE" | mailx -Ssendwait -s "$SUBJECT" "$CHECK_UPDATES_MAIL_ADDRESS"
|
2019-01-25 17:28:29 +00:00
|
|
|
fi
|