#!/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 PW_TMPFOLDER=${PW_FOLDER}/.tmp [ ! -f ${PW_LOGFILE} ] && touch ${PW_LOGFILE} [ ! -f ${PW_LOGPOWER} ] && touch ${PW_LOGPOWER} . /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 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 } PWO_START(){ SENDNOTICE2 "OFFSITE POWER MONITOR" "Service starting" while true; do upsinfo=$(upsc CP1500PFCLCD) if [ "$(echo "${upsinfo}" | grep 'input.voltage:' | sed 's/.*: //')" != "" ]; 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 if [ -f ${PW_TMPFOLDER}/power.offsite.off ]; then echo "($(date +'%Y-%m-%d %H:%M:%S')) - ${OFFSITE_VOLTIN}'Volts - ${OFFSITE_BATT_CAP}% Battery - Offsite Power Restored" >> ${PW_LOGPOWER} SENDNOTICE2 "OFFSITE POWER" "Power restored! Will wait 5mins before turning host(s) back on. BATTERY: ${OFFSITE_BATT_CAP}'(percent) VOLTAGE: ${OFFSITE_VOLTIN}'Volts" fi 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 300 ]; then echo "($(date +'%Y-%m-%d %H:%M:%S')) - ${OFFSITE_VOLTIN}'Volts - ${OFFSITE_BATT_CAP}% Battery - Powering Hosts ON After Power Outage" >> ${PW_LOGFILE} SENDNOTICE2 "OFFSITE HOST POWER" "Powering offsite host(s) back on after power outage" 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 fi [ $(expr `date +%s` - $(stat -f %m ${PW_LOGPOWER})) -gt 300 ] && echo "($(date +'%Y-%m-%d %H:%M:%S')) - ${OFFSITE_VOLTIN}'Volts - ${OFFSITE_BATT_CAP}% Battery - Normal" >> ${PW_LOGPOWER} else if [ ! -f ${PW_TMPFOLDER}/power.offsite.ac.low ] || [ $(expr `date +%s` - $(stat -f %m ${PW_TMPFOLDER}/power.offsite.ac.low)) -gt 240 ]; then echo "($(date +'%Y-%m-%d %H:%M:%S')) - ${OFFSITE_VOLTIN}'Volts - ${OFFSITE_BATT_CAP}% Battery - Powering LOW/OFF!" >> ${PW_LOGFILE} SENDNOTICE2 "OFFSITE POWER - 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 70 ] && [ ! -f ${PW_TMPFOLDER}/power.offsite.off ]; then echo "($(date +'%Y-%m-%d %H:%M:%S')) - ${OFFSITE_VOLTIN}'Volts - ${OFFSITE_BATT_CAP}% Battery - Powering Down Hosts!" >> ${PW_LOGFILE} SENDNOTICE2 "OFFSITE HOST POWER" "Powering DOWN offsite host(s) due to power outage" 1 touch ${PW_TMPFOLDER}/power.offsite.off ssh root@10.2.1.11 /vmfs/volumes/652c94ef-ee49895b-365d-00109b3fea40/.SCRIPTS/esxi-shutdown2.sh & fi [ $(expr `date +%s` - $(stat -f %m ${PW_LOGPOWER})) -gt 300 ] && echo "($(date +'%Y-%m-%d %H:%M:%S')) - ${OFFSITE_VOLTIN}'Volts - ${OFFSITE_BATT_CAP}% Battery - OFF/LOW POWER!!" >> ${PW_LOGPOWER} fi else SENDNOTICE2 "NUT DRIVER FAILURE" "Restarting NUT service" /usr/local/etc/rc.d/nut.sh restart sleep 10s fi sleep 5s done } PWO_STOP(){ ps -U root | grep "offsite-power-check.sh" | grep -v "grep" | awk '{print $1}' | xargs kill } PWO_RESTART() { PWO_STOP sleep 2s PWO_START & } case $1 in start) PWO_START &;; stop) PWO_STOP;; restart) PWO_RESTART;; update) cd ${PW_TMPFOLDER} if [ "`git log --pretty=%H ...refs/heads/master^ | head -n 1`" != "`git ls-remote origin -h refs/heads/master |cut -f1`" ]; then git fetch origin master >/dev/null 2>&1 git reset --hard origin/master >/dev/null 2>&1 git reflog expire --expire=now --all >/dev/null 2>&1 git repack -ad >/dev/null 2>&1 git prune >/dev/null 2>&1 git pull >/dev/null 2>&1 # /bin/chmod +x /opt/idssys/powerwall/offsite-power-check.sh 2>&1 PWO_RESTART fi ;; esac exit 0