Files
powerwall/offsite-power-check.sh

84 lines
3.2 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}
SENDNOTICE2(){
TITLE=${1}
MESSAGE="${2}"
MSGSOUND=Cosmic
PUSHOVER_APP_TOKEN="aduyec9mcuoaxir5pavirb8tioixpe"
PUSHOVER_USER_TOKEN="ubt1csryf5rwa1j96de8revt2fvw4j"
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"
echo "https://api.pushover.net/1/messages.json?token=${PUSHOVER_APP_TOKEN}&user=${PUSHOVER_USER_TOKEN}&message=${MESSAGE}&title=${TITLE}&priority=${PRIORITY}&sound=${MSGSOUND}"
}
upsinfo=$(upsc CP1500PFCLCD)
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)) -ge 60 ]; then
SENDNOTICE2 "OFFSITE HOST POWER" "Powering offsite host(s) back on after power was restored"
for offsite_host in "${PW_OFFSITEHOSTS[@]}"; do
# HOSTPOWER ${offsite_host} on >/dev/null 2>&1
done
rm -f ${PW_TMPFOLDER}/power.offsite.*
fi
else
[ $(expr `date +%s` - $(stat -f %m ${PW_LOGPOWER})) -gt 299 ] && 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)) -ge 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
fi
fi
SENDNOTICE2 "OFFSITE HOST POWER" "Powering DOWN offsite host(s) due to power outage" 1
exit 0