Update powerwall.sh

This commit is contained in:
2023-08-14 22:39:40 -05:00
parent 517072499a
commit 74687e04ee

View File

@@ -94,16 +94,13 @@ CHECKTEMPSENSOR(){
fi
elif [ "${PW_SENSOR_TYPE[${1}]}" == "esxi" ]; then
temp_c=${2}
temp_f=$(bc <<< "scale=1; ${temp_c}*1.8+32")
# temp_warn=$(bc <<< "scale=2; (${3}*1.8+32)/1")
temp_warn=`echo $(bc <<< "scale=2; (${3}*1.8+32)/1") | awk '{print int($1+0.5)}'`
# temp_crit=$(bc <<< "scale=2; (${4}*1.8+32)/1")
temp_crit=`echo $(bc <<< "scale=2; (${4}*1.8+32)/1") | awk '{print int($1+0.5)}'`
fi
if [ "${temp_c}" != "null" ] && [ "${temp_c}" != "" ]; then
temp_f=`echo "scale=1; ${temp_c}*1.8 + 32" | bc`
temp_f=$(bc <<< "scale=1; ${temp_c}*1.8+32")
#echo "$temp_c -> $temp_f"
c=0; spct=''; spc1=`expr 6 - ${#temp_f}`; until [ $c = ${spc1} ]; do spct="${spct} "; c=`expr ${c} + 1`; done
c=0; spca=''; spc1=`expr 6 - ${#average}`; until [ $c = ${spc1} ]; do spca="${spca} "; c=`expr ${c} + 1`; done
@@ -154,152 +151,153 @@ CHECKTEMP_SERVICE(){
for SENSOR in ${!PW_SENSOR_ID[@]}; do
logtemp=${PW_LOGFOLDER}/log-temp-${SENSOR}
[ ! -f ${logtemp} ] && touch ${logtemp}
errtime=$(expr `date +%s` - $(stat -c %Y ${logtemp}))
[ $errtime -ge 3600 ] && relog=1 || relog=0
logtime=$(expr `date +%s` - $(stat -c %Y ${logtemp}))
[ ${logtime} -ge 3600 ] && relog=1 || relog=0
if [ "${PW_SENSOR_TYPE[${SENSOR}]}" != "esxi" ]; then
if [ "${PW_REMOTE_SENSORS[${SENSOR}]}" == "" ] || ([ "${PW_REMOTE_SENSORS[${SENSOR}]}" != "" ] && [ "$(CHECK_HOST ${PW_REMOTE_SENSORS[${SENSOR}]})" != "false" ]); then
if [ -f ${PW_TMPFOLDER}/${SENSOR}.down ]; then
SENDNOTICE "${SENSOR} Online" "${SENSOR} Sensor is back online"
echo "($datetime) - ${SENSOR} - ${SENSOR} Sensor is back onlione" >> ${logtemp}
rm -f ${PW_TMPFOLDER}/${SENSOR}.down
fi
if [ "${PW_REMOTE_SENSORS[${SENSOR}]}" == "" ] || ([ "${PW_REMOTE_SENSORS[${SENSOR}]}" != "" ] && [ "$(CHECK_HOST ${PW_REMOTE_SENSORS[${SENSOR}]})" != "false" ]); then
if [ -f ${PW_TMPFOLDER}/${SENSOR}.down ]; then
SENDNOTICE "${SENSOR} Online" "${SENSOR} Sensor is back online"
echo "($datetime) - ${SENSOR} - ${SENSOR} Sensor is back onlione" >> ${logtemp}
rm -f ${PW_TMPFOLDER}/${SENSOR}.down
fi
if [ "${PW_SENSOR_TYPE[${SENSOR}]}" == "mqtt" ]; then
mqtt_message=`${mqtt_conn} -t tele/${SENSOR}/SENSOR -C 1`
echo $mqtt_message | cut -d':' -f 7 | cut -d',' -f 1 | read temp_h
echo $mqtt_message | cut -d':' -f 6 | cut -d',' -f 1 | read temp_c
if [ "${PW_SENSOR_TYPE[${SENSOR}]}" == "mqtt" ]; then
mqtt_message=`${mqtt_conn} -t tele/${SENSOR}/SENSOR -C 1`
echo $mqtt_message | cut -d':' -f 7 | cut -d',' -f 1 | read temp_h
echo $mqtt_message | cut -d':' -f 6 | cut -d',' -f 1 | read temp_c
elif [ "${PW_SENSOR_TYPE[${SENSOR}]}" == "system" ]; then
temp_c=$(vcgencmd measure_temp)
temp_c=${temp_c%\'*}; temp_c=${temp_c#*=}
temp_h=''
elif [ "${PW_SENSOR_TYPE[${SENSOR}]}" == "remotesystem" ]; then
temp_c=$(ssh root@${PW_REMOTE_SENSORS[${SENSOR}]} vcgencmd measure_temp)
temp_c=${temp_c%\'*}; temp_c=${temp_c#*=}
temp_h=''
fi
datetime=`date +'%Y-%m-%d %H:%M:%S'`
elif [ "${PW_SENSOR_TYPE[${SENSOR}]}" == "system" ]; then
temp_c=$(vcgencmd measure_temp)
temp_c=${temp_c%\'*}; temp_c=${temp_c#*=}
temp_h=''
if [ "$temp_c" != "null" ] && [ "$temp_c" != "" ]; then
if [ -f ${PW_TMPFOLDER}/${SENSOR}-error.reading ]; then
SENDNOTICE "${SENSOR} Data" "${SENSOR} is reporting data again"
echo "($datetime) - ${SENSOR} - ${SENSOR} is reporting data again" >> ${logtemp}
rm -f ${PW_TMPFOLDER}/${SENSOR}-error.reading
fi
temp_f=`echo "scale=2; $temp_c*1.8 + 32" | bc`
echo ${PW_TEMP_THRESHOLDS[${SENSOR}]} | cut -d',' -f 1 | read temp_warn
echo ${PW_TEMP_THRESHOLDS[${SENSOR}]} | cut -d',' -f 2 | read temp_crit
echo ${PW_TEMP_THRESHOLDS[${SENSOR}]} | cut -d',' -f 3 | read temp_crit_sys
if [ "${last_temp[${SENSOR}]}" != "" ]; then
temp_diff=`expr ${last_temp[${SENSOR}]%.*} - ${temp_f%.*}`
[ $temp_diff -lt 0 ] && temp_diff=$(($temp_diff * -1))
else
last_temp[${SENSOR}]=0
temp_diff=0
fi
if [ $(bc -l <<< "$temp_f >= $temp_warn") -eq 1 ] && [ $(bc -l <<< "$temp_f < $temp_crit") -eq 1 ]; then
touch ${PW_TMPFOLDER}/temp.warn
if [ $temp_diff -gt 1 ] || [ "${last_temp[${SENSOR}]}" = "1" ]; then
SENDNOTICE "${SENSOR} TEMP WARNING" "($datetime) WARNING TEMP: ${temp_f}'F - (Difference of ${temp_diff}')" 1
echo "($datetime) - ${temp_f}'F - alert sent" >> ${logtemp}
last_temp[${SENSOR}]=$temp_f
echo "($datetime) - ${temp_f}'F - WARNING TEMP - (Difference of ${temp_diff}')" >> ${logtemp}
fi
elif [ "${PW_SENSOR_TYPE[${SENSOR}]}" == "remotesystem" ]; then
temp_c=$(ssh root@${PW_REMOTE_SENSORS[${SENSOR}]} vcgencmd measure_temp)
temp_c=${temp_c%\'*}; temp_c=${temp_c#*=}
temp_h=''
elif [ $(bc -l <<< "$temp_f >= $temp_crit") -eq 1 ] && [ $(bc -l <<< "$temp_f < $temp_crit_sys") -eq 1 ]; then
touch ${PW_TMPFOLDER}/temp.crit
if [ $temp_diff -gt 1 ] || [ "${last_temp[${SENSOR}]}" = "1" ]; then
SENDNOTICE "${SENSOR} TEMP CRITICAL" "($datetime) CRITICAL TEMP: ${temp_f}'F - (Difference of ${temp_diff}')
Shutting down servers!!" 1
echo "($datetime) - ${temp_f}'F - CRITICAL - (Difference of ${temp_diff}'F)" >> ${logtemp}
echo "($datetime) - ${temp_f}'F - alert sent" >> ${logtemp}
echo -e "($datetime) - ${SENSOR} - ${temp_f}'F - CRITICAL TEMP - (Difference of ${temp_diff}')" >> ${PW_LOGFILE}
last_temp[${SENSOR}]=$temp_f
SHUTDOWN_MAIN ${SENSOR} &
echo -e "($datetime) - ${temp_f}'F - Shutting down MAIN servers" >> ${logtemp}
fi
elif [ $(bc -l <<< "$temp_f >= $temp_crit_sys") -eq 1 ]; then
touch ${PW_TMPFOLDER}/temp.critsys
if [ $temp_diff -gt 1 ] || [ "${last_temp[${SENSOR}]}" = "1" ]; then
SENDNOTICE "${SENSOR} TEMP CRITICAL" "($datetime) SYSTEM CRITICAL TEMP: ${temp_f}'F - (Difference of ${temp_diff}')
Shutting down system servers!!" 1
echo "($datetime) - ${temp_f}'F - SYSTEM CRITICAL - (Difference of ${temp_diff})" >> ${logtemp}
echo "($datetime) - ${temp_f}'F - alert sent" >> ${logtemp}
echo -e "($datetime) - ${temp_f}'F - ${SENSOR} - CRITICAL TEMP SYSTEM - (Difference of ${temp_diff}')" >> ${PW_LOGFILE}
last_temp[${SENSOR}]=$temp_f
SHUTDOWN_SYS ${SENSOR} TEMP &
SHUTDOWN_CRIT ${SENSOR} &
echo -e "($datetime) - ${temp_f}'F - Shutting down SYS servers" >> ${logtemp}
fi
else
[ ! -f ${PW_TMPFOLDER}/temp.* ] && [ $(expr `date +%s` - $(stat -c %Y ${PW_TMPFOLDER}/.${SENSOR}.sentdaily)) -gt 600 ] && [ $(date +%H)$(date +%M) -ge 0700 ] && [ $(date +%H)$(date +%M) -lt 0705 ] && touch ${PW_TMPFOLDER}/.${SENSOR}.sentdaily && SENDNOTICE "Daily Temp Readings" "$(DAILYTEMP)" &
if [ ${last_temp[${SENSOR}]} -gt 1 ]; then
SENDNOTICE "${SENSOR} BACK TO NORMAL" "($datetime) NORMAL TEMP: ${temp_f}'F
Previous Temp: ${last_temp[${SENSOR}]}'F"
echo -e "($datetime) - ${temp_f}'F - Back to NORMAL TEMP - Previous Temp: ${last_temp[${SENSOR}]}'F" >> ${logtemp}
echo "($datetime) - ${temp_f}'F - alert sent" >> ${logtemp}
echo -e "($datetime) - ${temp_f}'F - ${SENSOR} - Back to NORMAL TEMP" >> ${PW_LOGFILE}
[ "$(compgen -G "${PW_FOLDER}/shutdown.*")" != "" ] && rm -f ${PW_FOLDER}/shutdown.*
elif [ ${last_temp[${SENSOR}]} -eq 0 ]; then
SENDNOTICE "${SENSOR} TEMP NORMAL" "($datetime) Service Startup
NORMAL TEMP: ${temp_f}'F"
echo -e "($datetime) - ${temp_f}'F - Service Startup - NORMAL TEMP" >> ${logtemp}
echo -e "($datetime) - ${temp_f}'F - ${SENSOR} - Service Startup" >> ${PW_LOGFILE}
[ "$(compgen -G "${PW_FOLDER}/shutdown.*")" != "" ] && rm -f ${PW_FOLDER}/shutdown.*
elif [ $relog -eq 1 ]; then
echo "($datetime) - ${temp_f}'F - Normal Temp" >> ${logtemp}
fi
rm -f ${PW_TMPFOLDER}/temp.*
last_temp[${SENSOR}]=1
fi
[ -f ${PW_TMPFOLDER}/${SENSOR}.insert ] && lastinsert=$(expr `date +%s` - $(stat -c %Y ${PW_TMPFOLDER}/${SENSOR}.insert)) || lastinsert=100000
if [ "$(compgen -G "${PW_TMPFOLDER}/temp.*")" != "" ] || [ "$(compgen -G "${PW_TMPFOLDER}/power.ac.*")" != "" ] || [ "$(compgen -G "${PW_TMPFOLDER}/power.dc.*")" != "" ]; then
wait=60
else
wait=300
fi
if [ ${lastinsert} -ge ${wait} ]; then
if [ "${temp_h}" != "" ]; then
QRY="USE servermonitor; INSERT INTO sensor_data (\`sensorid\`, \`date\`, \`temp\`, \`hum\`) VALUES ('${PW_SENSOR_ID[${SENSOR}]}','${datetime}','${temp_f}','${temp_h}')"
else
QRY="USE servermonitor; INSERT INTO sensor_data (\`sensorid\`, \`date\`, \`temp\`) VALUES ('${PW_SENSOR_ID[${SENSOR}]}','${datetime}','${temp_f}')"
fi
${mysql_conn} -e "${QRY}"
touch ${PW_TMPFOLDER}/${SENSOR}.insert
fi
else
if [ ! -f ${PW_TMPFOLDER}/${SENSOR}-error.reading ] || [ $(expr `date +%s` - $(stat -c %Y ${PW_TMPFOLDER}/${SENSOR}-error.reading)) -ge 1800 ]; then
SENDNOTICE "${SENSOR} Sensor ERROR" "($datetime) ERROR reading sensor data" 1
echo "($datetime) - ${SENSOR} Sensor - ERROR reading sensor data" >> ${logtemp}
touch ${PW_TMPFOLDER}/${SENSOR}-error.reading
fi
fi
else #sensor down
if [ ! -f ${PW_TMPFOLDER}/${SENSOR}.down ] || [ $(expr `date +%s` - $(stat -c %Y ${PW_TMPFOLDER}/${SENSOR}.down)) -ge 1800 ]; then
SENDNOTICE "${SENSOR} ERROR" "${SENSOR} Sensor is down" 1
echo "($datetime) - ${SENSOR} - ${SENSOR} Sensor is down" >> ${logtemp}
touch ${PW_TMPFOLDER}/${SENSOR}.down
fi
fi
datetime=`date +'%Y-%m-%d %H:%M:%S'`
logtemp=${PW_LOGFOLDER}/log-temp-${SENSOR}
if [ "$temp_c" != "null" ] && [ "$temp_c" != "" ]; then
if [ -f ${PW_TMPFOLDER}/${SENSOR}-error.reading ]; then
SENDNOTICE "${SENSOR} Data" "${SENSOR} is reporting data again"
echo "($datetime) - ${SENSOR} - ${SENSOR} is reporting data again" >> ${logtemp}
rm -f ${PW_TMPFOLDER}/${SENSOR}-error.reading
fi
temp_f=`echo "scale=2; $temp_c*1.8 + 32" | bc`
echo ${PW_TEMP_THRESHOLDS[${SENSOR}]} | cut -d',' -f 1 | read temp_warn
echo ${PW_TEMP_THRESHOLDS[${SENSOR}]} | cut -d',' -f 2 | read temp_crit
echo ${PW_TEMP_THRESHOLDS[${SENSOR}]} | cut -d',' -f 3 | read temp_crit_sys
echo "HERE: ${SENSOR} - ${temp_f} - ${temp_warn} - ${temnp_crit}"
if [ "${last_temp[${SENSOR}]}" != "" ]; then
temp_diff=`expr ${last_temp[${SENSOR}]%.*} - ${temp_f%.*}`
[ $temp_diff -lt 0 ] && temp_diff=$(($temp_diff * -1))
else
last_temp[${SENSOR}]=0
temp_diff=0
fi
if [ $(bc -l <<< "$temp_f >= $temp_warn") -eq 1 ] && [ $(bc -l <<< "$temp_f < $temp_crit") -eq 1 ]; then
touch ${PW_TMPFOLDER}/temp.warn
if [ $temp_diff -gt 1 ] || [ "${last_temp[${SENSOR}]}" = "1" ]; then
SENDNOTICE "${SENSOR} TEMP WARNING" "($datetime) WARNING TEMP: ${temp_f}'F - (Difference of ${temp_diff}')" 1
echo "($datetime) - ${temp_f}'F - alert sent" >> ${logtemp}
last_temp[${SENSOR}]=$temp_f
echo "($datetime) - ${temp_f}'F - WARNING TEMP - (Difference of ${temp_diff}')" >> ${logtemp}
fi
elif [ $(bc -l <<< "$temp_f >= $temp_crit") -eq 1 ] && [ $(bc -l <<< "$temp_f < $temp_crit_sys") -eq 1 ]; then
touch ${PW_TMPFOLDER}/temp.crit
if [ $temp_diff -gt 1 ] || [ "${last_temp[${SENSOR}]}" = "1" ]; then
SENDNOTICE "${SENSOR} TEMP CRITICAL" "($datetime) CRITICAL TEMP: ${temp_f}'F - (Difference of ${temp_diff}')
Shutting down servers!!" 1
echo "($datetime) - ${temp_f}'F - CRITICAL - (Difference of ${temp_diff}'F)" >> ${logtemp}
echo "($datetime) - ${temp_f}'F - alert sent" >> ${logtemp}
echo -e "($datetime) - ${SENSOR} - ${temp_f}'F - CRITICAL TEMP - (Difference of ${temp_diff}')" >> ${PW_LOGFILE}
last_temp[${SENSOR}]=$temp_f
SHUTDOWN_MAIN ${SENSOR} &
echo -e "($datetime) - ${temp_f}'F - Shutting down MAIN servers" >> ${logtemp}
fi
elif [ $(bc -l <<< "$temp_f >= $temp_crit_sys") -eq 1 ]; then
touch ${PW_TMPFOLDER}/temp.critsys
if [ $temp_diff -gt 1 ] || [ "${last_temp[${SENSOR}]}" = "1" ]; then
SENDNOTICE "${SENSOR} TEMP CRITICAL" "($datetime) SYSTEM CRITICAL TEMP: ${temp_f}'F - (Difference of ${temp_diff}')
Shutting down system servers!!" 1
echo "($datetime) - ${temp_f}'F - SYSTEM CRITICAL - (Difference of ${temp_diff})" >> ${logtemp}
echo "($datetime) - ${temp_f}'F - alert sent" >> ${logtemp}
echo -e "($datetime) - ${temp_f}'F - ${SENSOR} - CRITICAL TEMP SYSTEM - (Difference of ${temp_diff}')" >> ${PW_LOGFILE}
last_temp[${SENSOR}]=$temp_f
SHUTDOWN_SYS ${SENSOR} TEMP &
SHUTDOWN_CRIT ${SENSOR} &
echo -e "($datetime) - ${temp_f}'F - Shutting down SYS servers" >> ${logtemp}
fi
else
[ ! -f ${PW_TMPFOLDER}/temp.* ] && [ $(expr `date +%s` - $(stat -c %Y ${PW_TMPFOLDER}/.${SENSOR}.sentdaily)) -gt 600 ] && [ $(date +%H)$(date +%M) -ge 0700 ] && [ $(date +%H)$(date +%M) -lt 0705 ] && touch ${PW_TMPFOLDER}/.${SENSOR}.sentdaily && SENDNOTICE "Daily Temp Readings" "$(DAILYTEMP)" &
if [ ${last_temp[${SENSOR}]} -gt 1 ]; then
SENDNOTICE "${SENSOR} BACK TO NORMAL" "($datetime) NORMAL TEMP: ${temp_f}'F
Previous Temp: ${last_temp[${SENSOR}]}'F"
echo -e "($datetime) - ${temp_f}'F - Back to NORMAL TEMP - Previous Temp: ${last_temp[${SENSOR}]}'F" >> ${logtemp}
echo "($datetime) - ${temp_f}'F - alert sent" >> ${logtemp}
echo -e "($datetime) - ${temp_f}'F - ${SENSOR} - Back to NORMAL TEMP" >> ${PW_LOGFILE}
[ "$(compgen -G "${PW_FOLDER}/shutdown.*")" != "" ] && rm -f ${PW_FOLDER}/shutdown.*
elif [ ${last_temp[${SENSOR}]} -eq 0 ]; then
SENDNOTICE "${SENSOR} TEMP NORMAL" "($datetime) Service Startup
NORMAL TEMP: ${temp_f}'F"
echo -e "($datetime) - ${temp_f}'F - Service Startup - NORMAL TEMP" >> ${logtemp}
echo -e "($datetime) - ${temp_f}'F - ${SENSOR} - Service Startup" >> ${PW_LOGFILE}
[ "$(compgen -G "${PW_FOLDER}/shutdown.*")" != "" ] && rm -f ${PW_FOLDER}/shutdown.*
elif [ $relog -eq 1 ]; then
echo "($datetime) - ${temp_f}'F - Normal Temp" >> ${logtemp}
fi
rm -f ${PW_TMPFOLDER}/temp.*
last_temp[${SENSOR}]=1
fi
[ -f ${PW_TMPFOLDER}/${SENSOR}.insert ] && lastinsert=$(expr `date +%s` - $(stat -c %Y ${PW_TMPFOLDER}/${SENSOR}.insert)) || lastinsert=100000
if [ "$(compgen -G "${PW_TMPFOLDER}/temp.*")" != "" ] || [ "$(compgen -G "${PW_TMPFOLDER}/power.ac.*")" != "" ] || [ "$(compgen -G "${PW_TMPFOLDER}/power.dc.*")" != "" ]; then
wait=60
else
wait=300
fi
if [ ${lastinsert} -ge ${wait} ]; then
if [ "${temp_h}" != "" ]; then
QRY="USE servermonitor; INSERT INTO sensor_data (\`sensorid\`, \`date\`, \`temp\`, \`hum\`) VALUES ('${PW_SENSOR_ID[${SENSOR}]}','${datetime}','${temp_f}','${temp_h}')"
else
QRY="USE servermonitor; INSERT INTO sensor_data (\`sensorid\`, \`date\`, \`temp\`) VALUES ('${PW_SENSOR_ID[${SENSOR}]}','${datetime}','${temp_f}')"
fi
${mysql_conn} -e "${QRY}"
touch ${PW_TMPFOLDER}/${SENSOR}.insert
fi
else
if [ ! -f ${PW_TMPFOLDER}/${SENSOR}-error.reading ] || [ $(expr `date +%s` - $(stat -c %Y ${PW_TMPFOLDER}/${SENSOR}-error.reading)) -ge 1800 ]; then
SENDNOTICE "${SENSOR} Sensor ERROR" "($datetime) ERROR reading sensor data" 1
echo "($datetime) - ${SENSOR} Sensor - ERROR reading sensor data" >> ${logtemp}
touch ${PW_TMPFOLDER}/${SENSOR}-error.reading
fi
fi
else #sensor down
if [ ! -f ${PW_TMPFOLDER}/${SENSOR}.down ] || [ $(expr `date +%s` - $(stat -c %Y ${PW_TMPFOLDER}/${SENSOR}.down)) -ge 1800 ]; then
SENDNOTICE "${SENSOR} ERROR" "${SENSOR} Sensor is down" 1
echo "($datetime) - ${SENSOR} - ${SENSOR} Sensor is down" >> ${logtemp}
touch ${PW_TMPFOLDER}/${SENSOR}.down
fi
fi
done