Skip to content

Instantly share code, notes, and snippets.

@dieechtenilente
Last active November 10, 2023 09:23
Show Gist options
  • Save dieechtenilente/2a4101242224d9d1ade557c073ad9b6d to your computer and use it in GitHub Desktop.
Save dieechtenilente/2a4101242224d9d1ade557c073ad9b6d to your computer and use it in GitHub Desktop.
Nrpe/Icinga2 check for Eaton 3S 700 UPS connected via USB
#!/bin/bash
#Parameters
# https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
-m|--command)
command="$2"
shift # past argument
shift # past value
;;
-w|--warning)
warning="$2"
shift # past argument
shift # past value
;;
-c|--critical)
critical="$2"
shift # past argument
shift # past value
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
if [[ -z "$command" ]] ; then
echo "Missing check command (-m/--command)"!
exit 3
fi
case $command in
runtime)
value=$(/usr/bin/upsc eaton battery.runtime 2>&1 | /usr/bin/sed 's/Init SSL without certificate database//g' | xargs)
text="Remaining runtime"
value_text="$value s"
;;
charge)
value=$(/usr/bin/upsc eaton battery.charge 2>&1 | /usr/bin/sed 's/Init SSL without certificate database//g' | xargs)
text="Charge"
value_text="$value%"
;;
load)
value=$(/usr/bin/upsc eaton ups.load 2>&1 | /usr/bin/sed 's/Init SSL without certificate database//g' | xargs)
text="Current load"
value_text="$value"
;;
status)
value=$(/usr/bin/upsc eaton ups.status 2>&1 | /usr/bin/sed 's/Init SSL without certificate database//g' | xargs)
text="Status"
;;
*)
echo Unknown command!
exit 3
esac
arr=( "runtime" "charge" )
if [[ " ${arr[@]} " =~ " $command " ]]; then
# Check for params
if [[ -z "$warning" || -z "$critical" ]] ; then
echo Missing parameters!
exit 3
fi
if [[ $warning -lt $critical ]] ; then
echo Warning must be greater than critical!
exit 3
fi
if [ $value -lt $critical ] ; then
echo "$text is critical ($value_text)!|'$text'=$value;;;"
exit 2
fi
if [ $value -lt $warning ] ; then
echo "$text is warning ($value_text)!|'$text'=$value;;;"
exit 1
fi
if [ $value -gt $warning ] ; then
echo "$text is OK ($value_text)!|'$text'=$value;;;"
exit 0
fi
echo "$text is unknown ($value_text)!"
exit 3
elif [[ $command == "load" ]]; then
# Check for params
if [[ -z "$warning" || -z "$critical" ]] ; then
echo Missing parameters!
exit 3
fi
if [[ $warning -gt $critical ]] ; then
echo Warning must be less than critical!
exit 3
fi
if [ $value -gt $critical ] ; then
echo "$text is critical ($value_text)!|'$text'=$value;;;"
exit 2
fi
if [ $value -gt $warning ] ; then
echo "$text is warning ($value_text)!|'$text'=$value;;;"
exit 1
fi
if [ $value -lt $warning ] ; then
echo "$text is OK ($value_text)!|'$text'=$value;;;"
exit 0
fi
echo "$text is unknown ($value_text)!"
exit 3
elif [[ $command == "status" ]] ; then
if [ "$value" == "OB" ] ; then
echo "$text is warning (UPS is charging)!"
exit 1
fi
if [ "$value" == "OL LB" ] ; then
echo "$text is warning (UPS is charging)!"
exit 1
fi
if [ "$value"== "OL" ] ; then
echo "$text is OK (UPS is on-line)!"
exit 0
fi
echo "$text is unknown (UPS state: $value)!"
exit 3
fi
echo Unknown error
exit 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment