diff --git a/config.example.sh b/config.example.sh index 1a29015..7429968 100644 --- a/config.example.sh +++ b/config.example.sh @@ -1,10 +1,7 @@ #!/bin/bash -# degrees in C -THERMAL_ALERT=75 - -# see: /sys/class/thermal/thermal_zone* -THERMAL_ZONE=0 +# degrees in Celsius which we regard as too high +THERMAL_ALERT=70 STORAGE_MONITOR=("/") @@ -32,7 +29,5 @@ DISCORD_GENERAL_HOOK="?wait=true" DISCORD_RESTAKE_HOOK="?wait=true" -DISCORD_THERMAL_HOOK="?wait=true" - DISCORD_STORAGE_HOOK="?wait=true" diff --git a/crons/thermal-alert.sh b/crons/thermal-alert.sh index 2ff7ec7..8953c5a 100755 --- a/crons/thermal-alert.sh +++ b/crons/thermal-alert.sh @@ -1,21 +1,32 @@ #!/bin/bash -LOCATION=`dirname "$0"` +# degrees in Celsius which we regard as too high, defaults to config +alertLevel="$1" -source "${LOCATION}/../config.sh" +# load in defaults from config +scriptRoot="$(dirname "$0")/.." +configFile="$scriptRoot/config.sh" +if [[ -f "$configFile" ]]; then + source "$configFile" -thermal=`${LOCATION}/../thermal.sh ${THERMAL_ALERT}` -alert=$? + if [[ -z "$alertLevel" ]]; then + alertLevel="$THERMAL_ALERT" + fi +fi -if [[ "0" != "${alert}" ]]; then - echo "${thermal}, above threshold ${THERMAL_ALERT} C" - ${LOCATION}/../discord.sh thermal "${thermal}, above threshold ${THERMAL_ALERT} C" - touch "${LOCATION}/thermal.status" -elif [[ -f "${LOCATION}/thermal.status" ]]; then - rm "${LOCATION}/thermal.status" - echo "${thermal}, previously above threshold ${THERMAL_ALERT} C" - ${LOCATION}/../discord.sh thermal "${thermal}, previously above threshold ${THERMAL_ALERT} C" -else - echo "${thermal}, below threshold ${THERMAL_ALERT} C" +# get current status +thermal=$("$scriptRoot/thermal.sh" "$alertLevel") +alert="$?" + +if [[ "0" -ne "$alert" ]]; then + # if currently in an alert status + alertLevelF=`echo "scale=1 ; $alertLevel * 9 / 5 + 32" | bc -l` + "$scriptRoot/discord.sh" "$thermal, above threshold $alertLevel C ($alertLevelF F)" + touch "$HOME/.thermal.alert" +elif [[ -f "$HOME/.thermal.alert" ]]; then + # if previously in an alert status + alertLevelF=`echo "scale=1 ; $alertLevel * 9 / 5 + 32" | bc -l` + "$scriptRoot/discord.sh" "$thermal, previously above threshold $alertLevel C ($alertLevelF F)" + rm "$HOME/.thermal.alert" fi diff --git a/crons/thermal-log.sh b/crons/thermal-log.sh index ee1776a..7c6c4dc 100755 --- a/crons/thermal-log.sh +++ b/crons/thermal-log.sh @@ -1,9 +1,8 @@ #!/bin/bash -LOCATION=`dirname "$0"` +scriptRoot="$(dirname "$0")/.." -thermal=`${LOCATION}/../thermal.sh` +thermal=$("$scriptRoot/thermal.sh") -echo "${thermal}" -${LOCATION}/../discord.sh thermal "${thermal}" +"$scriptRoot/discord.sh" "$thermal" diff --git a/discord-if-distinct.sh b/discord-if-distinct.sh index bdba6c1..9f1bd2b 100755 --- a/discord-if-distinct.sh +++ b/discord-if-distinct.sh @@ -4,10 +4,16 @@ LOCATION=`dirname "$0"` source "${LOCATION}/config.sh" -HOOK_ARG="$1" -BOBBY="$2" -# sed removes spaces before each line -MESSAGE=`echo "${@:3}" | sed 's/^ *//'` +if [[ $# -gt 2 ]]; then + HOOK_ARG="$1" + BOBBY="$2" + # sed removes spaces before each line + MESSAGE=`echo "${@:3}" | sed 's/^ *//'` +else + BOBBY="$1" + # sed removes spaces before each line + MESSAGE=`echo "${@:2}" | sed 's/^ *//'` +fi # sed removes blank lines from the end of the message FULL_MESSAGE=`echo "${MESSAGE}" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}'` @@ -47,10 +53,6 @@ case $HOOK_ARG in HOOK="restake" HOOK_URL=$DISCORD_RESTAKE_HOOK ;; - thermal) - HOOK="thermal" - HOOK_URL=$DISCORD_THERMAL_HOOK - ;; storage) HOOK="storage" HOOK_URL=$DISCORD_STORAGE_HOOK @@ -71,7 +73,7 @@ fi if [ "${FULL_MESSAGE}" != "${OLD_MSG}" ]; then echo "${FULL_MESSAGE}" > "${LOCATION}/distinct/${BOBBY}.msg" - curl -s -H "Content-Type: application/json" -X POST -d "{\"username\": \"${DISCORD_SERVER_NAME}\", \"content\": ${MESSAGE}}" $HOOK_URL | jq -r '.id' + curl -s -H "Content-Type: application/json" -X POST -d "{\"username\": \"${DISCORD_SERVER_NAME}\", \"content\": ${MESSAGE}}" $HOOK_URL | jq -r '.id,.message' if [ -n "${EXTRA}" ]; then "${LOCATION}/discord.sh" "${HOOK}" "${EXTRA}" diff --git a/discord.sh b/discord.sh index 8a236d3..065694f 100755 --- a/discord.sh +++ b/discord.sh @@ -4,9 +4,14 @@ LOCATION=`dirname "$0"` source "${LOCATION}/config.sh" -HOOK_ARG="$1" -# sed removes spaces before each line -MESSAGE=`echo "${@:2}" | sed 's/^ *//'` +if [[ $# -gt 1 ]]; then + HOOK_ARG="$1" + # sed removes spaces before each line + MESSAGE=`echo "${@:2}" | sed 's/^ *//'` +else + # sed removes spaces before each line + MESSAGE=`echo "${@}" | sed 's/^ *//'` +fi # if message is too long, split it, and after curl, recursively call with remainder # 2k max, but we must account for escaping text in json, so limit to 90% capacity @@ -44,10 +49,6 @@ case $HOOK_ARG in HOOK="restake" HOOK_URL=$DISCORD_RESTAKE_HOOK ;; - thermal) - HOOK="thermal" - HOOK_URL=$DISCORD_THERMAL_HOOK - ;; storage) HOOK="storage" HOOK_URL=$DISCORD_STORAGE_HOOK @@ -60,7 +61,7 @@ if [[ "$MESSAGE" == *"\\u001b"* ]]; then # just here to resolve formatting issue in vim "`" fi -curl -s -H "Content-Type: application/json" -X POST -d "{\"username\": \"${DISCORD_SERVER_NAME}\", \"content\": ${MESSAGE}}" $HOOK_URL | jq -r '.id' +curl -s -H "Content-Type: application/json" -X POST -d "{\"username\": \"${DISCORD_SERVER_NAME}\", \"content\": ${MESSAGE}}" $HOOK_URL | jq -r '.id,.message' if [ -n "${EXTRA}" ]; then "${LOCATION}/discord.sh" "${HOOK}" "${EXTRA}" diff --git a/fetch-all.sh b/fetch-all.sh index 94556ed..7bf37e1 100755 --- a/fetch-all.sh +++ b/fetch-all.sh @@ -39,5 +39,5 @@ function process_git_fetch () { # and call process_git_fetch on each location that was found. # we then work on the folder that contained the ".git" folder. -find "$followLinks" "$@" -type d -name ".git" | while read file; do process_git_fetch "$file"; done +find "$followLinks" "$@" -type d -name ".git" | while read -r file; do process_git_fetch "$file"; done diff --git a/pull-all.sh b/pull-all.sh index 96d2da5..b57c994 100755 --- a/pull-all.sh +++ b/pull-all.sh @@ -39,5 +39,5 @@ function process_git_pull () { # and call process_git_pull on each location that was found. # we then work on the folder that contained the ".git" folder. -find "$followLinks" "$@" -type d -name ".git" | while read file; do process_git_pull "$file"; done +find "$followLinks" "$@" -type d -name ".git" | while read -r file; do process_git_pull "$file"; done diff --git a/thermal.sh b/thermal.sh index 263cfb0..ef95d34 100755 --- a/thermal.sh +++ b/thermal.sh @@ -1,21 +1,43 @@ #!/bin/bash -LOCATION=`dirname "$0"` +alertLevel="$1" -source "${LOCATION}/config.sh" +# load in defaults from config +scriptRoot=$(dirname "$0") +configFile="$scriptRoot/config.sh" +if [[ -f "$configFile" ]]; then + source "$configFile" -# this is currently specific to raspberry-pi -# since that is the only kind of physical server I have -# TODO make this work with all temp in class/thermal -name=$( zone temp (milli-celsius) + # "./type" zone title + titleFile=$(echo "$mcFile" | sed 's/temp/type/') + title=$( < "$titleFile") + mc=$( < "$mcFile") -echo "${name}: ${c} C ($f F)" + # calculate Celcius and Fahrenheit + c=$(echo "scale=1 ; $mc / 1000.0" | bc -l) + f=$(echo "scale=1 ; $c * 9 / 5 + 32" | bc -l) + hot=$(echo "$c >= $alertLevel" | bc -l) -if (( "$#" > "0" )); then - exit `echo "$c >= $1" | bc` + echo "$title: $c C ($f F)" + + # if we have reached our alert threshold, mark it as active + if [[ -n "$alertLevel" ]] && [[ "$hot" -gt 0 ]]; then + alertActive=1 + fi + +done <<< $(find /sys/class/thermal/thermal*/ -name 'temp') + +# we exit with a status code of 1 to indicate alert level has been reached +if [[ "$alertActive" -eq 1 ]]; then + exit 1 fi