From 0ee7b03df84aced2597520c1d1fccac61c33b2a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Sch=C3=A4ferdiek?= Date: Sun, 10 Jul 2022 20:47:46 +0200 Subject: [PATCH] Apply shellcheck recommendations and add simple pipeline --- .drone.yml | 8 ++++++++ etc/systemd/system/dynv6.service | 10 ++++++++++ etc/systemd/system/dynv6.timer | 9 +++++++++ usr/local/bin/check_updates | 3 ++- usr/local/bin/disk_space_alert | 9 +++++---- usr/local/bin/docker_check | 15 ++++++++++----- usr/local/bin/docker_compose_update | 3 +++ usr/local/bin/dynv6 | 3 ++- usr/local/bin/memory_usage_alert | 2 ++ usr/local/bin/smartctl_tests_long | 11 ++++++----- usr/local/bin/systemd_check | 5 ++++- usr/local/bin/systemd_failure_notify | 3 ++- 12 files changed, 63 insertions(+), 18 deletions(-) create mode 100644 .drone.yml create mode 100644 etc/systemd/system/dynv6.service create mode 100644 etc/systemd/system/dynv6.timer diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..b408d23 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,8 @@ +kind: pipeline +name: default + +steps: + - name: test + image: koalaman/shellcheck-alpine:latest + commands: + - find usr/local/bin -type f -exec shellcheck {} \; diff --git a/etc/systemd/system/dynv6.service b/etc/systemd/system/dynv6.service new file mode 100644 index 0000000..68701d7 --- /dev/null +++ b/etc/systemd/system/dynv6.service @@ -0,0 +1,10 @@ +[Unit] +Description=Run dynv6 +OnFailure=systemd_failure_notify@%n.service + +[Service] +Type=oneshot +ExecStart=dynv6 + +[Install] +WantedBy=default.target diff --git a/etc/systemd/system/dynv6.timer b/etc/systemd/system/dynv6.timer new file mode 100644 index 0000000..4ba6be1 --- /dev/null +++ b/etc/systemd/system/dynv6.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Run dynv6 + +[Timer] +OnCalendar=hourly +Persistent=true + +[Install] +WantedBy=timers.target diff --git a/usr/local/bin/check_updates b/usr/local/bin/check_updates index 2204a85..c537d46 100755 --- a/usr/local/bin/check_updates +++ b/usr/local/bin/check_updates @@ -38,6 +38,7 @@ apply_config() { fi set -a; + # shellcheck disable=SC1090 source "$config"; set +a; } @@ -65,7 +66,7 @@ source_config() { source_config "$1" "$HOME/.check_updates.conf" "/etc/check_updates.conf" if [ "$UPDATES_AMOUNT" -gt "0" ]; then - mailx -s "[updates $HOSTNAME]" $CHECK_UPDATES_MAIL_ADDRESS << EOF + mailx -s "[updates $HOSTNAME]" "$CHECK_UPDATES_MAIL_ADDRESS" << EOF There are $UPDATES_AMOUNT updates available on $HOSTNAME. $UPDATES diff --git a/usr/local/bin/disk_space_alert b/usr/local/bin/disk_space_alert index 7d54f39..64d2266 100755 --- a/usr/local/bin/disk_space_alert +++ b/usr/local/bin/disk_space_alert @@ -42,6 +42,7 @@ apply_config() { fi set -a; + # shellcheck disable=SC1090 source "$config"; set +a; } @@ -70,12 +71,12 @@ source_config "$1" "$HOME/.disk_space_alert.conf" "/etc/disk_space_alert.conf" for point in "${DISK_SPACE_ALERT_MOUNTPOINTS[@]}" do - CURRENT=$(df $point | grep $point | awk '{ print $5}' | sed 's/%//g') - CURRENT_SPACE=$(df $point | grep $point | awk '{ print $5}' | sed 's/%//g') - CURRENT_INODES=$(df -i $point | grep $point | awk '{ print $5}' | sed 's/%//g') + CURRENT=$(df "$point" | grep "$point" | awk '{ print $5}' | sed 's/%//g') + CURRENT_SPACE=$(df "$point" | grep "$point" | awk '{ print $5}' | sed 's/%//g') + CURRENT_INODES=$(df -i "$point" | grep "$point" | awk '{ print $5}' | sed 's/%//g') if [ "$CURRENT" -gt "$DISK_SPACE_ALERT_THRESHOLD" ] ; then - mailx -s "[disk $HOSTNAME] $point" $DISK_SPACE_ALERT_MAIL_ADDRESS << EOF + mailx -s "[disk $HOSTNAME] $point" "$DISK_SPACE_ALERT_MAIL_ADDRESS" << EOF Your $point partition remaining free space is critically low. Used space: $CURRENT_SPACE%. Used inodes: $CURRENT_INODES%. EOF fi diff --git a/usr/local/bin/docker_check b/usr/local/bin/docker_check index 7684e02..eeb54e3 100755 --- a/usr/local/bin/docker_check +++ b/usr/local/bin/docker_check @@ -44,6 +44,7 @@ apply_config() { fi set -a; + # shellcheck disable=SC1090 source "$config"; set +a; } @@ -110,13 +111,14 @@ function log() { local STATUS=$2; local MESSAGE=$3; + # shellcheck disable=SC2155 local SHOULD_LOG=$(shouldLog "$STATUS"); if [ "$SHOULD_LOG" = "true" ]; then local SUBJECT="[docker $HOSTNAME] $STATUS $CONTAINER"; echo "$SUBJECT: $MESSAGE"; if [ "$DOCKER_CHECK_MAIL_ENABLED" = true ]; then - echo $MESSAGE|mailx -Ssendwait -s "$SUBJECT" $DOCKER_CHECK_MAIL_ADDRESS; + echo "$MESSAGE"|mailx -Ssendwait -s "$SUBJECT" "$DOCKER_CHECK_MAIL_ADDRESS"; fi fi } @@ -125,35 +127,38 @@ function log() { function checkContainer() { local CONTAINER=$1; + # shellcheck disable=SC2268 if [ "x${CONTAINER}" == "x" ]; then log "$CONTAINER" "UNKNOWN" "Container ID or Friendly Name Required" return; fi + # shellcheck disable=SC2268 if [ "x$(which docker)" == "x" ]; then log "$CONTAINER" "UNKNOWN" "Missing docker binary" return; fi docker info > /dev/null 2>&1 + # shellcheck disable=SC2181 if [ $? -ne 0 ]; then log "$CONTAINER" "UNKNOWN" "Unable to talk to the docker daemon" return; fi - RUNNING=$(docker inspect --format="{{.State.Running}}" $CONTAINER 2> /dev/null) + RUNNING=$(docker inspect --format="{{.State.Running}}" "$CONTAINER" 2> /dev/null) if [ $? -eq 1 ]; then - log "$CONTAINER" "UNKNOWN" "$CONTAINER does not exist." + log "${CONTAINER}" "UNKNOWN" "$CONTAINER does not exist." return; fi if [ "$RUNNING" == "false" ]; then - log "$CONTAINER" "CRITICAL" "$CONTAINER is not running." + log "${CONTAINER}" "CRITICAL" "$CONTAINER is not running." return; fi - RESTARTING=$(docker inspect --format="{{.State.Restarting}}" $CONTAINER) + RESTARTING=$(docker inspect --format="{{.State.Restarting}}" "$CONTAINER") if [ "$RESTARTING" == "true" ]; then log "$CONTAINER" "WARNING" "$CONTAINER state is restarting." diff --git a/usr/local/bin/docker_compose_update b/usr/local/bin/docker_compose_update index 4670f09..8fa14b5 100755 --- a/usr/local/bin/docker_compose_update +++ b/usr/local/bin/docker_compose_update @@ -40,6 +40,7 @@ apply_config() { fi set -a; + # shellcheck disable=SC1090 source "$config"; set +a; } @@ -91,6 +92,7 @@ check_required; # DO NOT TOUCH BELOW currentDir=$(pwd) +echo "Current directory is '${currentDir}'" cd "${DOCKER_COMPOSE_UPDATE_BASEDIR}"; for dir in *; do @@ -131,6 +133,7 @@ for dir in *; do set +e; fi + # shellcheck disable=SC2034 res=$(eval "${updateCommand}") if [[ "${DOCKER_COMPOSE_UPDATE_FAIL_ON_UPDATE_ERROR}" == "false" ]]; then diff --git a/usr/local/bin/dynv6 b/usr/local/bin/dynv6 index 877d19c..7b812ac 100755 --- a/usr/local/bin/dynv6 +++ b/usr/local/bin/dynv6 @@ -38,6 +38,7 @@ apply_config() { fi set -a; + # shellcheck disable=SC1090 source "$config"; set +a; } @@ -124,7 +125,7 @@ if [ "${DYNV6_IPV6_ENABLED}" = "true" ]; then touch "${ipv6AddressLocalFile}"; fi - if grep --color=never -Fxq ${ipv6Address} "${ipv6AddressLocalFile}"; then + if grep --color=never -Fxq "${ipv6Address}" "${ipv6AddressLocalFile}"; then echo "IPv6 is still ${ipv6Address}"; else echo "IPv6 changed" diff --git a/usr/local/bin/memory_usage_alert b/usr/local/bin/memory_usage_alert index 3f17559..e31fcc3 100755 --- a/usr/local/bin/memory_usage_alert +++ b/usr/local/bin/memory_usage_alert @@ -41,6 +41,7 @@ apply_config() { fi set -a; + # shellcheck disable=SC1090 source "$config"; set +a; } @@ -71,6 +72,7 @@ source_config "$1" "$HOME/.memory_usage_alert.conf" "/etc/memory_usage_alert.con # DO NOT TOUCH BELOW total=$(free -mt | grep Total | awk '{print $2}') used=$(free -mt | grep Total | awk '{print $3}') +# shellcheck disable=SC2004 free=$(($total-$used)) if [[ "$free" -le $MEMORY_USAGE_ALERT_THRESHOLD ]]; then diff --git a/usr/local/bin/smartctl_tests_long b/usr/local/bin/smartctl_tests_long index e95cc7c..ffc1462 100755 --- a/usr/local/bin/smartctl_tests_long +++ b/usr/local/bin/smartctl_tests_long @@ -38,6 +38,7 @@ apply_config() { fi set -a; + # shellcheck disable=SC1090 source "$config"; set +a; } @@ -66,11 +67,11 @@ source_config "$1" "$HOME/.smartctl_tests_long.conf" "/etc/smartctl_tests_long.c echo "Starting long tests for ..."; -for d in ${SMARTCTL_TESTS_LONG_DEVICES[@]}; do - DEVICE="/dev/"$d; - echo $DEVICE; - smartctl -t long $DEVICE; - mailx -s "[smartd $HOSTNAME] queued long test" $SMARTCTL_TESTS_LONG_MAIL_ADDRESS << EOF +for d in "${SMARTCTL_TESTS_LONG_DEVICES[@]}"; do + DEVICE="/dev/$d"; + echo "$DEVICE"; + smartctl -t long "$DEVICE"; + mailx -s "[smartd $HOSTNAME] queued long test" "$SMARTCTL_TESTS_LONG_MAIL_ADDRESS" << EOF Queued a smartd long test for $DEVICE. EOF done diff --git a/usr/local/bin/systemd_check b/usr/local/bin/systemd_check index 6500483..fdf47b7 100755 --- a/usr/local/bin/systemd_check +++ b/usr/local/bin/systemd_check @@ -48,6 +48,7 @@ apply_config() { fi set -a; + # shellcheck disable=SC1090 source "$config"; set +a; } @@ -105,10 +106,12 @@ function checkService() { local RESULT=0; if [ "$(id -u)" != "0" ]; then + # shellcheck disable=SC2091 if ! $($USER_COMMAND); then RESULT=1; fi else + # shellcheck disable=SC2091 if ! $($ROOT_COMMAND) && ! $($USER_COMMAND); then RESULT=1; fi @@ -119,7 +122,7 @@ function checkService() { echo "-> $SUBJECT: $MESSAGE"; if [ "$SYSTEMD_CHECK_MAIL_ENABLED" = true ]; then - echo $MESSAGE|mailx -Ssendwait -s "$SUBJECT" $SYSTEMD_CHECK_MAIL_ADDRESS; + echo "$MESSAGE"|mailx -Ssendwait -s "$SUBJECT" "$SYSTEMD_CHECK_MAIL_ADDRESS"; fi fi diff --git a/usr/local/bin/systemd_failure_notify b/usr/local/bin/systemd_failure_notify index 8890424..3573d24 100755 --- a/usr/local/bin/systemd_failure_notify +++ b/usr/local/bin/systemd_failure_notify @@ -54,6 +54,7 @@ apply_config() { fi set -a; + # shellcheck disable=SC1090 source "$config"; set +a; } @@ -82,4 +83,4 @@ source_config "$2" "$HOME/.systemd_failure_notify.conf" "/etc/systemd_failure_no SUBJECT="[systemd $SYSTEMD_FAILURE_NOTFY_HOSTNAME for $SYSTEMD_FAILURE_NOTFY_USER] $SYSTEMD_FAILURE_NOTFY_SERVICE RUN FAILED" MESSAGE="$SYSTEMD_FAILURE_NOTFY_SERVICE run failed" -echo $MESSAGE|mailx -Ssendwait -s "$SUBJECT" $SYSTEMD_FAILURE_NOTIFY_MAIL_ADDRESS; \ No newline at end of file +echo "$MESSAGE"|mailx -Ssendwait -s "$SUBJECT" "$SYSTEMD_FAILURE_NOTIFY_MAIL_ADDRESS"; \ No newline at end of file