#!/usr/bin/sh #---------------------------------------------------------------------------------- # /var/install/bin/nut-simulate-power-failure - simulate power failure # # Copyright (c) 2014-2024 The Eisfair Team, team(at)eisfair(dot)org # # Creation: 2014-09-19 jed # Last Update: $Id: nut-pre-view-log 35516 2014-05-04 21:06:12Z jed $ # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------------- # load eislib etc. . /var/install/include/eislib module_name=nut # debug mode true/false #debug=true if ${debug:-false} then exec 2> /tmp/${module_name}-simulate-power-failure-trace-$$.log set -x ask_debug=true export ask_debug fi configfile=/etc/config.d/${module_name} # name 'ups' need to be used for Synology diskstation compatibility # upsname="`hostname`-ups`" default_upsname='ups' . ${configfile} #---------------------------------------------------------------------------------- # print selection table #---------------------------------------------------------------------------------- print_selection_table () { clrhome mecho --info "Simulate USV power failure" mecho techo --begin '4r 10 30' techo --row --info "Nbr" --info "name" --info "comment" idx=1 while [ ${idx} -le ${ups_n} ] do eval upsname='$ups_'${idx}'_upsname' eval desc='$ups_'${idx}'_desc' techo --row "${idx}" "${upsname}" "${desc}" idx=`expr ${idx} + 1` done techo --end mecho } if [ "${START_NUT}" = "yes" ] then if [ "${NUT_CLIENT_MODE}" = "yes" ] then echo "${NUT_CLIENT_UPSMON_SERVER_IP}" | grep -q ":" if [ $? -eq 0 ] then # ip address and port has been given ipaddr=`echo "${NUT_CLIENT_UPSMON_SERVER_IP}" | cut -d: -f1` ipport=`echo "${NUT_CLIENT_UPSMON_SERVER_IP}" | cut -d: -f2` if [ -z "${ipport}" ] then ipport="3493" fi else # only an ip address has been given ipaddr="${NUT_CLIENT_UPSMON_SERVER_IP}" ipport="3493" fi elif [ "${NUT_SERVER_MODE}" = "yes" ] then ipaddr='127.0.0.1' echo "${NUT_SERVER_LISTEN_ADDRESSES}" | grep -q "127.0.0.1" if [ $? -eq 0 ] then ipport=`echo "${NUT_SERVER_LISTEN_ADDRESSES}" | 's#^.*127\.0\.0\.1:\([[:digit:]]*\),.*$#\1#'` else ipport='3493' fi fi idx=1 upsidx=1 ups_list=':' while [ ${idx} -le ${NUT_SERVER_N} ] do eval active='$NUT_SERVER_'${idx}'_ACTIVE' if [ "${active}" = "yes" ] then eval uname='$NUT_SERVER_'${idx}'_UPSNAME' eval desc='$NUT_SERVER_'${idx}'_COMMENT' eval port='$NUT_SERVER_'${idx}'_PORT' if [ -n "${uname}" ] then upsname="${uname}" else upsname="${default_upsname}" fi # check if ups name has already been used echo "${ups_list}" | grep -q ":${upsname}," if [ $? -ne 0 ] then # ups name is uniq, use it ups_list="${ups_list}${upsname}:" else # create individual ups name upsname="${upsname}-${idx}" ups_list="${ups_list}${upsname}:" fi eval ups_${upsidx}_upsname="${upsname}" eval ups_${upsidx}_desc="${desc}" eval ups_${upsidx}_port="${port}" upsidx=`expr ${upsidx} + 1` fi idx=`expr ${idx} + 1` done ups_n=`expr ${upsidx} - 1` # print table print_selection_table endflag=0 until [ ${endflag} -eq 1 ] do /var/install/bin/ask "Please enter ups number (1-${ups_n}) or (q)uit:" '' '+' > /tmp/ask.$$ rc=$? nbr=`cat /tmp/ask.$$` rm -f /tmp/ask.$$ if [ $rc = 255 ] then exit 1 fi case "${nbr}" in [qQ]) endflag=1 exit 1 ;; [0-9]|[1-9][0-9]) ### number entered ### if [ ${nbr} -ge 1 -a ${nbr} -le ${ups_n} ] then eval upsname='$ups_'${nbr}'_upsname' eval port='$ups_'${nbr}'_port' # check supported commands answer=`upscmd -u ${NUT_SERVER_UPSMON_ADMIN_USER} -p ${NUT_SERVER_UPSMON_ADMIN_PASS} -l ${upsname}@${ipaddr}:${ipport}` success=0 for CMD in test.failure.start test.battery.start.quick do case ${CMD} in test.failure.start ) mecho -n --info "Simulating power failure ... " ;; test.battery.start.quick ) mecho -n --info "Testing the battery ... " ;; esac echo "${answer}" | grep -q "${CMD}" if [ $? -eq 0 ] then # command supported, execute it ... if ${debug:-false} then # debug mode echo echo "executing command: upscmd ${upsname}@${ipaddr}:${ipport} ${CMD}" fi upscmd -u ${NUT_SERVER_UPSMON_ADMIN_USER} -p ${NUT_SERVER_UPSMON_ADMIN_PASS} ${upsname}@${ipaddr}:${ipport} ${CMD} >/dev/null 2>&1 if [ $? -eq 0 ] then # command successfully executed mecho --info "done." success=1 break fi else mecho --warn "not supported." fi done if [ ${success} -eq 0 ] then # an error appeared mecho --error "Your UPS or the used driver doesn't support simulating a power failure or testing the battery!" fi mecho anykey # print table print_selection_table else mecho --warn "Number not in range, try again!" fi ;; *) ;; esac done else mecho --warn "The nut package is currently disabled!" fi exit 0