Jeroen De Meerleer
b01af8010d
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.
88 lines
2.2 KiB
Bash
88 lines
2.2 KiB
Bash
#!/bin/bash
|
|
|
|
#
|
|
#/
|
|
#/ Usage:
|
|
#/ check-dummy.sh [--ok|--warning|--critical]
|
|
#/
|
|
#/ 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:
|
|
#/ 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='owcu'
|
|
LONGOPTS='ok,warning,critical,unknown'
|
|
|
|
# -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
|
|
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
|
|
-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
|
|
break
|
|
;;
|
|
*)
|
|
echo "DUMMY UNKNOWN - ${1} is not a valid parameter"
|
|
exit 3
|
|
;;
|
|
esac
|
|
done
|
|
}
|
|
script_name=$(basename "${0}")
|
|
script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
|
|
rval=0
|
|
rmsg="OK"
|
|
GetOptions "$@"
|
|
|
|
echo "DUMMY $rmsg"
|
|
exit $rval |