From 71977c9dca644ee506351eb61b3f08be4f3f2906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Sch=C3=A4ferdiek?= Date: Wed, 25 Jan 2023 09:42:22 +0100 Subject: [PATCH] Fail properly in all scripts when required commands are not available --- usr/local/bin/disk_space_alert | 18 +++++++++++++----- usr/local/bin/docker_check | 11 ++++++++++- usr/local/bin/docker_compose_update | 2 ++ usr/local/bin/docker_housekeeping | 7 +++++++ usr/local/bin/dynv6 | 6 ++++++ usr/local/bin/memory_usage_alert | 12 ++++++++++-- usr/local/bin/perms_default_umask | 7 +++++++ usr/local/bin/perms_user_home_default_umask | 7 +++++++ usr/local/bin/smartctl_tests_long | 10 +++++++++- usr/local/bin/smartdnotify | 8 ++++++++ usr/local/bin/ssh-keygen-rsa | 7 +++++++ usr/local/bin/systemd_check | 14 ++++++++++++-- usr/local/bin/systemd_failure_notify | 2 ++ 13 files changed, 100 insertions(+), 11 deletions(-) diff --git a/usr/local/bin/disk_space_alert b/usr/local/bin/disk_space_alert index 0935018..f4e6d93 100755 --- a/usr/local/bin/disk_space_alert +++ b/usr/local/bin/disk_space_alert @@ -13,11 +13,6 @@ EOF set -e; -HOSTNAME=$(hostname) -DISK_SPACE_ALERT_MAIL_ADDRESS=""; -DISK_SPACE_ALERT_THRESHOLD=93 -DISK_SPACE_ALERT_MOUNTPOINTS=("/") - # check for config file apply_config() { local config=$1; @@ -55,7 +50,20 @@ source_config() { return; fi } + +check_required() { + 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; } + type df &> /dev/null || { echo "Requiring 'df' but it's not installed"; exit 1; } +} + source_config "$1" "$HOME/.disk_space_alert.conf" "/etc/disk_space_alert.conf" +check_required + +HOSTNAME=$(hostname) +DISK_SPACE_ALERT_MAIL_ADDRESS=""; +DISK_SPACE_ALERT_THRESHOLD=93 +DISK_SPACE_ALERT_MOUNTPOINTS=("/") for point in "${DISK_SPACE_ALERT_MOUNTPOINTS[@]}" do diff --git a/usr/local/bin/docker_check b/usr/local/bin/docker_check index 79fa4e9..08032ce 100755 --- a/usr/local/bin/docker_check +++ b/usr/local/bin/docker_check @@ -13,7 +13,6 @@ EOF set -e; -HOSTNAME=$(hostname) DOCKER_CHECK_LIST_FILE="$HOME/.docker_check.list" DOCKER_CHECK_GLOBAL_LIST_FILE="/etc/docker_check.list" DOCKER_CHECK_NOTIFY_LEVELS=("UNKNOWN" "WARNING" "CRITICAL") @@ -58,7 +57,17 @@ source_config() { return; fi } + +check_required() { + 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; } + type docker &> /dev/null || { echo "Requiring 'docker' but it's not installed"; exit 1; } +} + source_config "$1" "$HOME/.docker_check.conf" "/etc/docker_check.conf" +check_required + +HOSTNAME=$(hostname) listFileToUse="" if [[ -f "$DOCKER_CHECK_LIST_FILE" ]]; then diff --git a/usr/local/bin/docker_compose_update b/usr/local/bin/docker_compose_update index 39b3a5b..d9bf3df 100755 --- a/usr/local/bin/docker_compose_update +++ b/usr/local/bin/docker_compose_update @@ -67,6 +67,8 @@ check_required() { echo "DOCKER_COMPOSE_UPDATE_COMMAND is required" exit 1; fi + + type docker-compose &> /dev/null || { echo "Requiring 'docker-compose' but it's not installed"; exit 1; } } # check requirements diff --git a/usr/local/bin/docker_housekeeping b/usr/local/bin/docker_housekeeping index 725e9b7..94c33eb 100755 --- a/usr/local/bin/docker_housekeeping +++ b/usr/local/bin/docker_housekeeping @@ -1,5 +1,12 @@ #!/usr/bin/env bash +check_required() { + type docker &> /dev/null || { echo "Requiring 'docker' but it's not installed"; exit 1; } + type yes &> /dev/null || { echo "Requiring 'yes' but it's not installed"; exit 1; } +} + +check_required + echo "Doing docker housekeeping for containers, images and volumes"; yes | docker container prune diff --git a/usr/local/bin/dynv6 b/usr/local/bin/dynv6 index 4c4ab88..b66105f 100755 --- a/usr/local/bin/dynv6 +++ b/usr/local/bin/dynv6 @@ -82,6 +82,12 @@ check_required() { echo "DYNV6_HOSTNAME 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; } + type ip &> /dev/null || { echo "Requiring 'ip' but it's not installed"; exit 1; } + type grep &> /dev/null || { echo "Requiring 'grep' but it's not installed"; exit 1; } + type curl &> /dev/null || { echo "Requiring 'curl' but it's not installed"; exit 1; } } # check requirements diff --git a/usr/local/bin/memory_usage_alert b/usr/local/bin/memory_usage_alert index 47e852f..ab827e7 100755 --- a/usr/local/bin/memory_usage_alert +++ b/usr/local/bin/memory_usage_alert @@ -13,8 +13,6 @@ EOF set -e; -HOSTNAME=$(hostname) -SUBJECT="[memory $HOSTNAME] memory is low" MEMORY_USAGE_ALERT_MAIL_ADDRESS=""; MEMORY_USAGE_ALERT_THRESHOLD=512; @@ -56,7 +54,17 @@ source_config() { fi } +check_required() { + 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; } + type free &> /dev/null || { echo "Requiring 'free' but it's not installed"; exit 1; } +} + source_config "$1" "$HOME/.memory_usage_alert.conf" "/etc/memory_usage_alert.conf" +check_required + +HOSTNAME=$(hostname) +SUBJECT="[memory $HOSTNAME] memory is low" # DO NOT TOUCH BELOW total=$(free -mt | grep Total | awk '{print $2}') diff --git a/usr/local/bin/perms_default_umask b/usr/local/bin/perms_default_umask index fc0caae..e5a0594 100755 --- a/usr/local/bin/perms_default_umask +++ b/usr/local/bin/perms_default_umask @@ -6,6 +6,13 @@ # - align with umask 022 (755 on folders, 644 on files) # - keep +x flag for already executable files if enabled (default) +check_required() { + type find &> /dev/null || { echo "Requiring 'find' but it's not installed"; exit 1; } + type chmod &> /dev/null || { echo "Requiring 'chmod' but it's not installed"; exit 1; } +} + +check_required + DIR="$1" KEEP_EXECUTABLE_FILES="$2" diff --git a/usr/local/bin/perms_user_home_default_umask b/usr/local/bin/perms_user_home_default_umask index e315ccf..9e59e9e 100755 --- a/usr/local/bin/perms_user_home_default_umask +++ b/usr/local/bin/perms_user_home_default_umask @@ -7,6 +7,13 @@ # - keep +x flag for already executable files if enabled (default, otherwise set to no) # - set 700 (only allow owner to go into directory) +check_required() { + type find &> /dev/null || { echo "Requiring 'find' but it's not installed"; exit 1; } + type chmod &> /dev/null || { echo "Requiring 'chmod' but it's not installed"; exit 1; } +} + +check_required + DIR="$1" KEEP_EXECUTABLE_FILES="$2" diff --git a/usr/local/bin/smartctl_tests_long b/usr/local/bin/smartctl_tests_long index ffc1462..198ff1c 100755 --- a/usr/local/bin/smartctl_tests_long +++ b/usr/local/bin/smartctl_tests_long @@ -22,7 +22,6 @@ EOF set -e; -HOSTNAME=$(hostname) SMARTCTL_TESTS_LONG_MAIL_ADDRESS=""; SMARTCTL_TESTS_LONG_DEVICES=(sda) @@ -63,8 +62,17 @@ source_config() { return; fi } + +check_required() { + 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; } + type smartctl &> /dev/null || { echo "Requiring 'smartctl' but it's not installed"; exit 1; } +} + source_config "$1" "$HOME/.smartctl_tests_long.conf" "/etc/smartctl_tests_long.conf" +HOSTNAME=$(hostname) + echo "Starting long tests for ..."; for d in "${SMARTCTL_TESTS_LONG_DEVICES[@]}"; do diff --git a/usr/local/bin/smartdnotify b/usr/local/bin/smartdnotify index d4b8b96..c5e760f 100755 --- a/usr/local/bin/smartdnotify +++ b/usr/local/bin/smartdnotify @@ -1,5 +1,13 @@ #!/usr/bin/env bash +check_required() { + 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; } + type wall &> /dev/null || { echo "Requiring 'wall' but it's not installed"; exit 1; } +} + +check_required + HOSTNAME=$(hostname) echo "$SMARTD_MESSAGE" | mail -s "[smartd $HOSTNAME] $SMARTD_FAILTYPE" "$SMARTD_ADDRESS" wall "$SMARTD_MESSAGE" \ No newline at end of file diff --git a/usr/local/bin/ssh-keygen-rsa b/usr/local/bin/ssh-keygen-rsa index 2255852..d00262c 100755 --- a/usr/local/bin/ssh-keygen-rsa +++ b/usr/local/bin/ssh-keygen-rsa @@ -2,6 +2,11 @@ set -e; +check_required() { + type openssl &> /dev/null || { echo "Requiring 'openssl' but it's not installed"; exit 1; } + type ssh-keygen &> /dev/null || { echo "Requiring 'ssh-keygen' but it's not installed"; exit 1; } +} + name=$1; encryption=$2; @@ -14,5 +19,7 @@ if [[ -z ${encryption} ]]; then encryption=4096; fi +check_required + openssl genrsa -out "${name}" "${encryption}" ssh-keygen -y -f "${name}" > "${name}.pub" \ No newline at end of file diff --git a/usr/local/bin/systemd_check b/usr/local/bin/systemd_check index e8e7f26..694c9fc 100755 --- a/usr/local/bin/systemd_check +++ b/usr/local/bin/systemd_check @@ -19,8 +19,6 @@ SYSTEMD_CHECK_LIST_FILE="$HOME/.systemd_check.list" SYSTEMD_CHECK_GLOBAL_LIST_FILE="/etc/systemd_check.list" SYSTEMD_CHECK_MAIL_ENABLED=fase SYSTEMD_CHECK_MAIL_ADDRESS="" -SYSTEMD_CHECK_HOSTNAME=$(hostname) -SYSTEMD_CHECK_USER=$(whoami) SYSTEMD_CHECK_SERVICES=() # check for config file @@ -60,7 +58,19 @@ source_config() { return; fi } + +check_required() { + 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; } + type whoami &> /dev/null || { echo "Requiring 'whoami' but it's not installed"; exit 1; } + type systemctl &> /dev/null || { echo "Requiring 'systemctl' but it's not installed"; exit 1; } +} + source_config "$1" "$HOME/.systemd_check.conf" "/etc/systemd_check.conf" +check_required + +SYSTEMD_CHECK_HOSTNAME=$(hostname) +SYSTEMD_CHECK_USER=$(whoami) listFileToUse="" if [[ -f "$SYSTEMD_CHECK_LIST_FILE" ]]; then diff --git a/usr/local/bin/systemd_failure_notify b/usr/local/bin/systemd_failure_notify index ef096ae..f9f513c 100755 --- a/usr/local/bin/systemd_failure_notify +++ b/usr/local/bin/systemd_failure_notify @@ -58,6 +58,8 @@ check_requirements() { if [[ "${gotifyEnabled}" == "true" ]]; then type gotify &> /dev/null || { echo "Requiring 'gotify' but it's not installed"; exit 1; } fi + + type whoami &> /dev/null || { echo "Requiring 'whoami' but it's not installed"; exit 1; } } source_config() {