Refactor check-dummy.sh to add options for warning, critical and unknown status

This commit refactors the check-dummy.sh script to include options for warning, critical and unknown status. The script now accepts the following command line arguments: --ok (default), --warning, --critical and --unknown. The exit codes have also been updated to reflect these new statuses.
This commit is contained in:
Jeroen De Meerleer 2023-06-01 16:06:02 +02:00
parent a1cd78b7e5
commit b01af8010d
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6

View File

@ -3,13 +3,22 @@
# #
#/ #/
#/ Usage: #/ Usage:
#/ check-dummy.sh #/ check-dummy.sh [--ok|--warning|--critical]
#/ #/
#/ Does not check anything. Always gives OK! #/ Does not check anything.
#/
#/ Options:
#/ -o, --ok Trigger a OK status (default)
#/ -w, --warning Trigger a warning
#/ -c, --critical Trigger a critical warning
#/ -u, --unknown Trigger a unknown status
#/ #/
#/ #/
#/ Exit Codes: #/ Exit Codes:
#/ 0 Everything OK #/ 0 Everything OK
#/ 1 Warning level exceeded
#/ 2 Critical level exceeded
#/ 3 Unknown status
#/ #/
Usage() { Usage() {
@ -18,8 +27,8 @@ Usage() {
GetOptions() { GetOptions() {
# https://stackoverflow.com/a/29754866 # https://stackoverflow.com/a/29754866
OPTIONS='' OPTIONS='owcu'
LONGOPTS='' LONGOPTS='ok,warning,critical,unknown'
# -use ! and PIPESTATUS to get exit code with errexit set # -use ! and PIPESTATUS to get exit code with errexit set
# -temporarily store output to be able to check for errors # -temporarily store output to be able to check for errors
@ -30,7 +39,6 @@ GetOptions() {
# e.g. return value is 1 # e.g. return value is 1
# then getopt has complained about wrong arguments to stdout # then getopt has complained about wrong arguments to stdout
Usage Usage
exit 2
fi fi
# read getopt's output this way to handle the quoting right: # read getopt's output this way to handle the quoting right:
@ -39,23 +47,42 @@ GetOptions() {
# now enjoy the options in order and nicely split until we see -- # now enjoy the options in order and nicely split until we see --
while true; do while true; do
case "$1" in case "$1" in
-o|--ok)
rval=0
rmsg="OK"
shift
;;
-w|--warning)
rval=1
rmsg="WARNING"
shift
;;
-c|--critical)
rval=2
rmsg="CRITICAL"
shift
;;
-u|--unknown)
rval=3
rmsg="UNKNOWN"
shift
;;
--) --)
shift shift
break break
;; ;;
*) *)
echo "URL UNKNOWN - ${1} is not a valid parameter" echo "DUMMY UNKNOWN - ${1} is not a valid parameter"
exit 3 exit 3
;; ;;
esac esac
done done
if [ -z ${1+x} ]; then
echo "URL UNKNOWN - url not given"
exit 3
else
url=${1}
fi
} }
echo "DUMMY OK" script_name=$(basename "${0}")
exit 0 script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
rval=0
rmsg="OK"
GetOptions "$@"
echo "DUMMY $rmsg"
exit $rval