Files
powerwall/offsite-power-check.sh

93 lines
3.4 KiB
Bash
Executable File

#!/usr/bin/env sh
# PW_FOLDER='/opt/idssys/powerwall'
# PW_LOGFOLDER=${PW_FOLDER}/logs
# PW_LOGFILE=${PW_LOGFOLDER}/logfile
# PW_LOGPOWER=${PW_LOGFOLDER}/log-power
# [ ! -f ${PW_LOGFILE} ] && touch ${PW_LOGFILE}
# [ ! -f ${PW_LOGPOWER} ] && touch ${PW_LOGPOWER}
. /opt/idssys/powerwall/defaults.inc
. /opt/idssys/powerwall/settings.conf
SENDNOTICE2(){
TITLE=${1}
MESSAGE="${2}
$(date)"
MSGSOUND=Cosmic
if [ "${3}" = "2" ]; then
PRIORITY="${3}&retry=60&expire=600"
MSGSOUND=alien
elif [ "${3}" = "1" ]; then
PRIORITY="${3}"
MSGSOUND=siren
elif [ "${3}" != "" ]; then
PRIORITY=${3}
else
PRIORITY=0
fi
echo "SENDING MESSAGE!!!!!!"
curl -d "token=${PUSHOVER_APP_TOKEN}&user=${PUSHOVER_USER_TOKEN}&message=${MESSAGE}&title=${TITLE}&priority=${PRIORITY}&sound=${MSGSOUND}" https://api.pushover.net/1/messages.json
}
while true; do
upsinfo=$(upsc CP1500PFCLCD)
if [ "${upsinfo}" != "" ]; then
OFFSITE_VOLTIN=$(echo "${upsinfo}" | grep 'input.voltage:' | sed 's/.*: //')
OFFSITE_BATT_RUNTIME=$(echo "${upsinfo}" | grep 'battery.runtime:' | sed 's/.*: //')
OFFSITE_BATT_RUNTIME=`echo "scale=2; ${OFFSITE_BATT_RUNTIME}/60" | bc`
OFFSITE_BATT_CAP=$(echo "${upsinfo}" | grep 'battery.charge:' | sed 's/.*: //')
if [ ${OFFSITE_VOLTIN%.*} -gt 105 ]; then
if [ -f ${PW_TMPFOLDER}/power.offsite.ac.low ]; then
echo "($(date +'%Y-%m-%d %H:%M:%S')) - ${OFFSITE_VOLTIN}'Volts - Offsite Back to Normal Voltage" >> ${PW_LOGPOWER}
echo "($(date +'%Y-%m-%d %H:%M:%S')) - ${OFFSITE_VOLTIN}'Volts - OFFSITE POWER NOMINAL AGAIN" >> ${PW_LOGFILE}
SENDNOTICE2 "OFFSITE POWER NOMINAL AGAIN" "Normal voltage detected
VOLTAGE: ${OFFSITE_VOLTIN}'Volts"
rm -f ${PW_TMPFOLDER}/power.offsite.ac.*
fi
if [ -f ${PW_TMPFOLDER}/power.offsite.off ]; then
if [ ! -f ${PW_TMPFOLDER}/power.offsite.restored ]; then
touch ${PW_TMPFOLDER}/power.offsite.restored
elif [ $(expr `date +%s` - $(stat -f %m ${PW_TMPFOLDER}/power.offsite.restored)) -gt 60 ]; then
SENDNOTICE2 "OFFSITE HOST POWER" "Powering offsite host(s) back on after power was restored"
ipmitool -I lanplus -H 10.2.1.21 -U ${PW_ESXI_USER} -P "${PW_ESXI_PASS}" chassis power on
rm -f ${PW_TMPFOLDER}/power.offsite.*
fi
else
[ $(expr `date +%s` - $(stat -f %m ${PW_LOGPOWER})) > 300 ] && echo "($(date +'%Y-%m-%d %H:%M:%S')) - ${OFFSITE_VOLTIN}'Volts - Offsite Normal Voltage" >> ${PW_LOGPOWER}
fi
else
echo "($(date +'%Y-%m-%d %H:%M:%S')) - ${OFFSITE_VOLTIN}'Volts - OFFSITE POWER OFF/LOW POWER" >> ${PW_LOGPOWER}
echo "($(date +'%Y-%m-%d %H:%M:%S')) - ${OFFSITE_VOLTIN}'Volts - OFFSITE POWER OFF/LOW POWER" >> ${PW_LOGFILE}
if [ ! -f ${PW_TMPFOLDER}/power.offsite.ac.low ] || [ $(expr `date +%s` - $(stat -f %m ${PW_TMPFOLDER}/power.offsite.ac.low)) -gt 240 ]; then
SENDNOTICE2 "OFFSITE POWER ALERT - POWER OFF/LOW!!" "Power off or low voltage detected
BATT CAPACITY: ${OFFSITE_BATT_CAP}'(percent)
BATT RUNTIME: ${OFFSITE_BATT_RUNTIME}'Mins" 1
touch ${PW_TMPFOLDER}/power.offsite.ac.low
fi
if [ ${OFFSITE_BATT_CAP%.*} -lt 95 ] && [ ! -f ${PW_TMPFOLDER}/power.offsite.off ]; then
SENDNOTICE2 "OFFSITE HOST POWER" "Powering DOWN offsite host(s) due to power outage" 1
# SHUTDOWN_OFFSITE &
touch ${PW_TMPFOLDER}/power.offsite.off
echo "SHUTTING DOWN HOST"
fi
fi
else
SENDNOTICE2 "NUT DRIVER FAILURE" "Restarting NUT service"
/usr/local/etc/rc.d/nut.sh restart
sleep 10s
fi
sleep 10s
done
exit 0