diff --git a/check-env-temp.sh b/check-env-temp.sh new file mode 100644 index 0000000..4fc0d5d --- /dev/null +++ b/check-env-temp.sh @@ -0,0 +1,119 @@ +#!/bin/bash + +# +#/ +#/ Usage: +#/ check-env-temp.sh --critical-min= --warning-min= --warning-max= --critical-min= --help +#/ +#/ Checks the memory usage +#/ +#/ Options: +#/ -c, --critical-min= The minimal degree centigrade of when to trigger a critical warning +#/ -w, --warning-min= The minimal degree centigrade of when to trigger a warning +#/ -W, --warning-max= The maximum degree centigrade of when to trigger a warning +#/ -C, --critical-max= The maximum degree centigrade of when to trigger a critical warning +#/ -h, --help Display this help message +#/ +#/ Exit Codes: +#/ 0 Everything OK +#/ 1 Warning level exceeded +#/ 2 Critical level exceeded +#/ 3 Unknown status +#/ + + +Usage() { + grep '^#/' "${script_dir}/${script_name}" | sed 's/^#\/\w*//' +} + +GetOptions() { + # https://stackoverflow.com/a/29754866 + OPTIONS=c:w:W:C:h + LONGOPTS=critical-min:,warning-min:,:warning-max:,critical-max:,help + + # -use ! and PIPESTATUS to get exit code with errexit set + # -temporarily store output to be able to check for errors + # -activate quoting/enhanced mode (e.g. by writing out “--options”) + # -pass arguments only via -- "$@" to separate them correctly + ! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@") + if [[ ${PIPESTATUS[0]} -ne 0 ]]; then + # e.g. return value is 1 + # then getopt has complained about wrong arguments to stdout + Usage + exit 2 + fi + + # read getopt's output this way to handle the quoting right: + eval set -- "$PARSED" + + # now enjoy the options in order and nicely split until we see -- + while true; do + case "$1" in + -c|--critital-min) + critmin="$2" + shift 2 + ;; + -w|--warning-min) + warnmin="$2" + shift 2 + ;; + -W|--warning-max) + warnmax="$2" + shift 2 + ;; + -C|--critital-max) + critmax="$2" + shift 2 + ;; + -h|--help) + Usage + exit 0 + ;; + --) + shift + break + ;; + *) + echo "MEMORY UNKNOWN - ${1} is not a valid parameter" + exit 3 + ;; + esac + done +} +LC_NUMERIC="C" +critmin=5 +warnmin=15 +warnmax=40 +critmax=45 +script_name=$(basename "${0}") +script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) +GetOptions "$@" + +val=$(temper --json | jq '.[0]."internal temperature"' | bc -l) + +if (( $(echo "$val < ${critmin}" | bc -l) )); then + rval=2 + rmsg="CRITICAL" +elif (( $(echo "$val < ${warnmin}" | bc -l) )); then + rval=1 + rmsg="WARNING" +elif (( $(echo "$val < ${warnmax}" | bc -l) )); then + rval=0 + rmsg="OK" +elif (( $(echo "$val < ${critmax}" | bc -l) )); then + rval=1 + rmsg="WARNING" +else + rval=2 + rmsg="CRITICAL" +fi + +dval=$(printf %.2f $(echo ${val}\ | bc -l)) +dcritmin=$(printf %.2f $(echo ${critmin}\ | bc -l)) +dwarnmin=$(printf %.2f $(echo ${warnmin}\ | bc -l)) +dwarnmax=$(printf %.2f $(echo ${warnmax}\ | bc -l)) +dcritmax=$(printf %.2f $(echo ${critmax}\ | bc -l)) +unit="°" + +echo "ENV TEMP ${rmsg} - Temperature: ${dval} |temp=${dval}${unit};${dwarnmin}:${dwarnmax};${dcritmin}:${dcritmax}" +exit $rval