From 07c0c7a04de407e8ca1f82bbe88a9b8929f3c4a0 Mon Sep 17 00:00:00 2001 From: n Date: Thu, 19 Oct 2023 17:53:44 +0200 Subject: [PATCH] shellcheck manual fixes (#4) --- monitoring/check_gemini.sh | 2 +- monitoring/check_lastdate.sh | 13 ++++++++----- monitoring/check_postgresql_replication.sh | 13 +++++++------ monitoring/collectweather.sh | 6 +++--- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/monitoring/check_gemini.sh b/monitoring/check_gemini.sh index 9f629af..40e942c 100755 --- a/monitoring/check_gemini.sh +++ b/monitoring/check_gemini.sh @@ -9,7 +9,7 @@ then return 0 fi -errorOutput=$(echo "gemini://$1/" | ${TLS_CLIENT} $1 2>&1 > /dev/null) +errorOutput=$(echo "gemini://$1/" | ${TLS_CLIENT} "$1" 2>&1 > /dev/null) errorCode=$? if [ $errorCode -gt 0 ] diff --git a/monitoring/check_lastdate.sh b/monitoring/check_lastdate.sh index f8d1b25..052fe1e 100755 --- a/monitoring/check_lastdate.sh +++ b/monitoring/check_lastdate.sh @@ -25,6 +25,9 @@ while getopts "hF:C:W:" opt; do F) file=$OPTARG ;; + *) + echo "Usage: $0 [-h] [-w (default: $warning)] [-c (default: $critical) -F ]" + exit 3 esac done @@ -32,13 +35,13 @@ shift $((OPTIND-1)) [ "$1" = "--" ] && shift -if [ -z ${file} ] +if [ -z "${file}" ] then echo "ERROR: please provide filename to parse with $0 -F " exit 3 fi -lastdate=$(tail -2 ${file} | grep -o -E '^[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2} [[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}') +lastdate=$(tail -2 "${file}" | grep -o -E '^[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2} [[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}') lastts=$(date -d "${lastdate}" +%s) currentts=$(date +%s) @@ -50,13 +53,13 @@ else exit 3 fi -message=$(echo "${file} not updated since ${delay} seconds") +message="${file} not updated since ${delay} seconds" -if [ ${delay} -gt ${critical} ] +if [ "${delay}" -gt "${critical}" ] then echo "CRITICAL: ${message}" exit 2 -elif [ ${delay} -gt ${warning} ] +elif [ "${delay}" -gt "${warning}" ] then echo "WARNING: ${message}" exit 1 diff --git a/monitoring/check_postgresql_replication.sh b/monitoring/check_postgresql_replication.sh index d75302e..31c9184 100755 --- a/monitoring/check_postgresql_replication.sh +++ b/monitoring/check_postgresql_replication.sh @@ -1,4 +1,5 @@ #!/bin/sh + # check postgresql (streaming) replication # tested with postgresql 11.3 on FreeBSD 11.2 # @@ -6,15 +7,15 @@ # warning (-w) and critical (-c) thresholds can be set in seconds # on a low trafic server don't hesitate to set them to a high value -args=`getopt hw:c: $*` -set -- $args +args=$(getopt hw:c: "$*") +set -- "$args" warning=600 critical=3600 while :; do case "$1" in - -h) echo "Usage: $0 [-h] [-w (default: $warning)] [-c (default: $critical)]" + -h) echo "Usage $0: [-h] [-w warning (default: $warning)] [-c critical (default: $critical)]" shift; exit 2; ;; @@ -30,13 +31,13 @@ while :; do esac done -delay=`/usr/local/bin/psql -U postgres --pset expanded=yes -c "select now() - pg_last_xact_replay_timestamp() AS replication_delay;" | tail -2 | head -1 | cut -d '|' -f 2 | cut -d ' ' -f 2 | cut -d '.' -f 1 | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }'` +delay=$(/usr/local/bin/psql --port=6432 --username=postgres --pset="expanded=yes" --command="select now() - pg_last_xact_replay_timestamp() AS replication_delay;" | tail -2 | head -1 | cut -d '|' -f 2 | cut -d ' ' -f 2 | cut -d '.' -f 1 | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }') -if [ $delay -gt $critical ] +if [ "$delay" -gt "$critical" ] then echo "CRITICAL: $delay seconds behind master" exit 2 -elif [ $delay -gt $warning ] +elif [ "$delay" -gt "$warning" ] then echo "WARNING: $delay seconds behind master" exit 1 diff --git a/monitoring/collectweather.sh b/monitoring/collectweather.sh index 6b21bae..aca6730 100755 --- a/monitoring/collectweather.sh +++ b/monitoring/collectweather.sh @@ -1,6 +1,6 @@ #!/bin/sh -HOSTNAME="${COLLECTD_HOSTNAME:-`hostname -f`}" +HOSTNAME="${COLLECTD_HOSTNAME:-$(hostname -f)}" INTERVAL="${COLLECTD_INTERVAL:-300}" FILE="/home/weather/tmp/openweathermap.json" @@ -9,8 +9,8 @@ FILE="/home/weather/tmp/openweathermap.json" while sleep "$INTERVAL"; do for field in main.temp main.feels_like main.pressure main.humidity wind.speed wind.gust clouds.all rain.\"1h\" rain.\"3h\" snow.\"1h\" snow.\"3h\" sys.sunrise sys.sunset visibility;do - key=`echo ${field} | sed 's/\./-/g' | sed 's/"//g'` - echo "PUTVAL $HOSTNAME/weather/gauge-${key} interval=$INTERVAL N:`jq .${field} $FILE | sed s/null/0/g`" ; + key=$(echo "$field" | sed 's/\./-/g' | sed 's/"//g') + echo "PUTVAL $HOSTNAME/weather/gauge-${key} interval=$INTERVAL N:$(jq ."$field" $FILE | sed s/null/0/g)" ; done done