diff options
author | Steve McIntyre <steve@einval.com> | 2022-06-28 22:15:51 +0100 |
---|---|---|
committer | Steve McIntyre <steve@einval.com> | 2022-06-28 22:15:51 +0100 |
commit | 32f4e615ec93d21ccb286ccac2517b71b3809e2c (patch) | |
tree | 319df6679b86650e502ab1952247178b565758fe | |
parent | a8e1efaf14e5a486b822da587c0ce0cfe5aeaca1 (diff) | |
download | steve-scripts-32f4e615ec93d21ccb286ccac2517b71b3809e2c.zip |
Add UPS test script
-rwxr-xr-x | ups-test | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/ups-test b/ups-test new file mode 100755 index 0000000..5812be4 --- /dev/null +++ b/ups-test @@ -0,0 +1,117 @@ +#!/bin/bash + +BAT_TEST_TIMEOUT=1800 + +TEST=quick +UPS=liebert +VERBOSE=0 + +validate_arg() { + local VAR=$1 + local VAROPTS="$(echo $2 | tr ':' ' ')" + + for OPT in $VAROPTS; do + if [ "${!VAR}"x = "$OPT"x ]; then + return + fi + done + # else + echo "$VAR ${!VAR} not supported - use one of $VAROPTS" + exit 1 +} + +while getopts ":t:u:v" o; do + case "${o}" in + t) + TEST=${OPTARG} + ;; + u) + UPS=${OPTARG} + ;; + v) + VERBOSE=1 + ;; + *) + echo "Unknown option ${o}" + exit 1 + ;; + esac +done +shift $((OPTIND-1)) + +validate_arg TEST "quick:deep" +validate_arg UPS "liebert" + +if [ "$VERBOSE"x = "1"x ]; then + set -x +fi + +# Restart the client, in case it's lost connection, then give it time +# to reconnect +service nut-driver restart +sleep 5 + +# Grab the admin password from the config file +PASS=$(awk '/^[^#].*password =/ {print $3}' /etc/nut/upsd.users) + +# Start a battery test +STATUS=$(upscmd -u admin -p $PASS $UPS test.battery.start.$TEST 2>&1) +if [ "$STATUS"x != "OK"x ]; then + echo "FAILED starting battery test: $STATUS" +fi + +sleep 10 + +# Now mute the beeper, we don't want to be *too* anoying if it fails +STATUS=$(upscmd -u admin -p $PASS $UPS beeper.mute 2>&1) +if [ "$STATUS"x != "OK"x ]; then + echo "FAILED disabling beeper: $STATUS" +fi + +# Poll? +# upsc $UPS ups.test.result 2>/dev/null +CURRENT=$(date +%s) +START=$CURRENT +TAKEN=0 +while [ $TAKEN -lt $BAT_TEST_TIMEOUT ]; do + TAKEN=$(($CURRENT - $START)) + OLD_BAT_STATUS="$BAT_STATUS" + BAT_STATUS=$(upsc $UPS 2>&1 | grep -e ^battery.charge: -e ^battery.voltage) + STATUS=$(upsc $UPS 2>&1 | grep ups.test.result) + case "$STATUS"x in + + "ups.test.result: Done and passed"x) + # Completed OK, exit clean + RESULT="UPS test completed OK after $TAKEN seconds: $STATUS" + logger -t ups-test "$RESULT" + logger -t ups-test "Battery status under test: $OLD_BAT_STATUS" + logger -t ups-test "Battery status after test: $BAT_STATUS" + + # Don't echo anything here - succeed silently + exit 0 + ;; + + "ups.test.result: Done and error"x) + # Completed but not OK, fail! + RESULT="UPS test failed after $TAKEN seconds: $STATUS" + logger -t ups-test "$RESULT" + # Now mute the beeper, we don't want to be *too* anoying if it fails + STATUS=$(upscmd -u admin -p $PASS $UPS beeper.mute 2>&1) + if [ "$STATUS"x != "OK"x ]; then + echo "FAILED disabling beeper: $STATUS" + fi + logger -t ups-test "Battery status under test: $OLD_BAT_STATUS" + logger -t ups-test "Battery status after test: $BAT_STATUS" + + # Also echo all our messages on failure here, so they'll + # go into cron job output + echo "$RESULT" + echo "Battery status under test: $OLD_BAT_STATUS" + echo "Battery status after test: $BAT_STATUS" + exit 1 + ;; + esac + sleep 1 + CURRENT=$(date +%s) +done +echo |