Add check-env-temp.sh script to monitor environment temperature

This commit adds a new bash script called check-env-temp.sh that monitors the internal temperature of the system and returns an exit code based on whether it is within acceptable limits. The script takes in command line arguments for setting warning and critical thresholds, and outputs a message indicating whether the temperature is OK, Warning or Critical along with performance data.
This commit is contained in:
Jeroen De Meerleer 2023-06-19 22:31:01 +02:00
parent 540ad83ef8
commit 27313aec76
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
1 changed files with 119 additions and 0 deletions

119
check-env-temp.sh Normal file
View File

@ -0,0 +1,119 @@
#!/bin/bash
#
#/
#/ Usage:
#/ check-env-temp.sh --critical-min=<centigrade> --warning-min=<centigrade> --warning-max=<centigrade> --critical-min=<centigrade> --help
#/
#/ Checks the memory usage
#/
#/ Options:
#/ -c, --critical-min=<centigrade> The minimal degree centigrade of when to trigger a critical warning
#/ -w, --warning-min=<centigrade> The minimal degree centigrade of when to trigger a warning
#/ -W, --warning-max=<centigrade> The maximum degree centigrade of when to trigger a warning
#/ -C, --critical-max=<centigrade> 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