From c77ebcf07d373f2d4189da59de28c82d21b27c82 Mon Sep 17 00:00:00 2001 From: David Schroeder Date: Mon, 14 Aug 2023 22:57:21 -0500 Subject: [PATCH] Update powerwall.sh --- powerwall.sh | 224 ++++++++++++++++++++++++++------------------------- 1 file changed, 116 insertions(+), 108 deletions(-) diff --git a/powerwall.sh b/powerwall.sh index e5d5061f..3654ee2e 100755 --- a/powerwall.sh +++ b/powerwall.sh @@ -162,11 +162,13 @@ CHECKTEMP_SERVICE(){ rm -f ${PW_TMPFOLDER}/${SENSOR}.down fi - + + SENSORS_CHECK=(${SENSOR}) 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 + SENSORS_CHECK=(${SENSOR}) elif [ "${PW_SENSOR_TYPE[${SENSOR}]}" == "system" ]; then temp_c=$(vcgencmd measure_temp) @@ -177,124 +179,130 @@ CHECKTEMP_SERVICE(){ temp_c=$(ssh root@${PW_REMOTE_SENSORS[${SENSOR}]} vcgencmd measure_temp) temp_c=${temp_c%\'*}; temp_c=${temp_c#*=} temp_h='' + + # elif [ "${PW_SENSOR_TYPE[${SENSOR}]}" == "esxi" ]; then + fi + for SENSOR in ${SENSORS_CHECK[@]}; do - 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 + 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` + 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 ${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} - ${temp_crit}" + echo "HERE: ${SENSOR} - ${temp_f} - ${temp_warn} - ${temp_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 - if [ ! -f ${PW_TMPFOLDER}/temp.* ] && [ $(date +%H)$(date +%M) -ge 0700 ] && [ $(date +%H)$(date +%M) -lt 0705 ]; then - if [ ! -f ${PW_TMPFOLDER}/.${SENSOR}.sentdaily ] || [ $(expr `date +%s` - $(stat -c %Y ${PW_TMPFOLDER}/.${SENSOR}.sentdaily)) -gt 600 ]; then - touch ${PW_TMPFOLDER}/.${SENSOR}.sentdaily - SENDNOTICE "Daily Temp Readings" "$(DAILYTEMP)" & - fi - fi - - 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}')" + if [ "${last_temp[${SENSOR}]}" != "" ]; then + temp_diff=`expr ${last_temp[${SENSOR}]%.*} - ${temp_f%.*}` + [ $temp_diff -lt 0 ] && temp_diff=$(($temp_diff * -1)) else - QRY="USE servermonitor; INSERT INTO sensor_data (\`sensorid\`, \`date\`, \`temp\`) VALUES ('${PW_SENSOR_ID[${SENSOR}]}','${datetime}','${temp_f}')" + last_temp[${SENSOR}]=0 + temp_diff=0 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 + + 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 + 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 + if [ ! -f ${PW_TMPFOLDER}/temp.* ] && [ $(date +%H)$(date +%M) -ge 0700 ] && [ $(date +%H)$(date +%M) -lt 0705 ]; then + if [ ! -f ${PW_TMPFOLDER}/.${SENSOR}.sentdaily ] || [ $(expr `date +%s` - $(stat -c %Y ${PW_TMPFOLDER}/.${SENSOR}.sentdaily)) -gt 600 ]; then + touch ${PW_TMPFOLDER}/.${SENSOR}.sentdaily + SENDNOTICE "Daily Temp Readings" "$(DAILYTEMP)" & + fi + fi + + 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 + + done else #sensor down if [ ! -f ${PW_TMPFOLDER}/${SENSOR}.down ] || [ $(expr `date +%s` - $(stat -c %Y ${PW_TMPFOLDER}/${SENSOR}.down)) -ge 1800 ]; then