From daf244f7cb68fd83e5bf1d483974e07a3a258a6d Mon Sep 17 00:00:00 2001 From: David Schroeder Date: Sun, 22 Oct 2023 20:42:48 -0500 Subject: [PATCH] Update powerwall.sh --- powerwall.sh | 287 ++++++++++++++++++++++++++------------------------- 1 file changed, 144 insertions(+), 143 deletions(-) diff --git a/powerwall.sh b/powerwall.sh index b48ac0e9..a0c5f4cc 100755 --- a/powerwall.sh +++ b/powerwall.sh @@ -220,159 +220,160 @@ CHECKTEMP_SERVICE(){ else SENSORS_CHECK=(${SENSORa}) fi + if [ "${SENSORS_CHECK}" != "" ]; then + for SENSOR in "${SENSORS_CHECK[@]}"; do - for SENSOR in "${SENSORS_CHECK[@]}"; do - - if [ "${PW_SENSOR_TYPE[${SENSORa}]}" == "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[${SENSORa}]}" == "system" ]; then - temp_c=$(vcgencmd measure_temp) - temp_c=${temp_c%\'*}; temp_c=${temp_c#*=} - temp_h='' - - elif [ "${PW_SENSOR_TYPE[${SENSORa}]}" == "remotesystem" ]; then - 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[${SENSORa}]}" == "esxi" ]; then - temp_c=$(echo ${SENSOR} | cut -d'p' -f2 | awk '/ / {print $2}' | sed -e 's/C//g'); temp_c=${temp_c/ /} - temp_warn=$(echo ${SENSOR} | cut -d'p' -f2 | awk '/ / {print $7}' | sed -e 's/C//g'); - temp_crit=$(echo ${SENSOR} | cut -d'p' -f2 | awk '/ / {print $4}' | sed -e 's/C//g'); - temp_crit_sys=$(echo ${SENSOR} | cut -d'p' -f2 | awk '/ / {print $4}' | sed -e 's/C//g'); - - temp_warn=`echo $(bc <<< "scale=2; (${temp_warn/ /}*1.8+32)/1") | awk '{print int($1+0.5)}'` - temp_crit=`echo $(bc <<< "scale=2; (${temp_crit/ /}*1.8+32)/1") | awk '{print int($1+0.5)}'` - temp_crit_sys=`echo $(bc <<< "scale=2; (${temp_crit_sys/ /}*1.8+32)/1") | awk '{print int($1+0.5)}'` - - temp_h='' - vSENSOR=$(echo ${SENSOR} | cut -d'T' -f1 | xargs) - SENSOR="${SENSORa}-${vSENSOR// /_}" - - 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 + if [ "${PW_SENSOR_TYPE[${SENSORa}]}" == "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[${SENSORa}]}" == "system" ]; then + temp_c=$(vcgencmd measure_temp) + temp_c=${temp_c%\'*}; temp_c=${temp_c#*=} + temp_h='' + + elif [ "${PW_SENSOR_TYPE[${SENSORa}]}" == "remotesystem" ]; then + temp_c=$(ssh root@${PW_REMOTE_SENSORS[${SENSOR}]} vcgencmd measure_temp) + temp_c=${temp_c%\'*}; temp_c=${temp_c#*=} + temp_h='' - fi - temp_f=`echo "scale=2; $temp_c*1.8 + 32" | bc` - # echo "HERE: ${SENSOR} - ${temp_f} - ${temp_warn} - ${temp_crit} - ${temp_crit_sys}" - - if [ "${PW_SENSOR_TYPE[${SENSORa}]}" != "esxi" ]; then - 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 - fi - - 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 + elif [ "${PW_SENSOR_TYPE[${SENSORa}]}" == "esxi" ]; then + temp_c=$(echo ${SENSOR} | cut -d'p' -f2 | awk '/ / {print $2}' | sed -e 's/C//g'); temp_c=${temp_c/ /} + temp_warn=$(echo ${SENSOR} | cut -d'p' -f2 | awk '/ / {print $7}' | sed -e 's/C//g'); + temp_crit=$(echo ${SENSOR} | cut -d'p' -f2 | awk '/ / {print $4}' | sed -e 's/C//g'); + temp_crit_sys=$(echo ${SENSOR} | cut -d'p' -f2 | awk '/ / {print $4}' | sed -e 's/C//g'); + + temp_warn=`echo $(bc <<< "scale=2; (${temp_warn/ /}*1.8+32)/1") | awk '{print int($1+0.5)}'` + temp_crit=`echo $(bc <<< "scale=2; (${temp_crit/ /}*1.8+32)/1") | awk '{print int($1+0.5)}'` + temp_crit_sys=`echo $(bc <<< "scale=2; (${temp_crit_sys/ /}*1.8+32)/1") | awk '{print int($1+0.5)}'` + + temp_h='' + vSENSOR=$(echo ${SENSOR} | cut -d'T' -f1 | xargs) + SENSOR="${SENSORa}-${vSENSOR// /_}" + 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 - last_temp[${SENSOR}]=$temp_f - echo "($datetime) - ${temp_f}'F - WARNING TEMP - (Difference of ${temp_diff}')" >> ${logtemp} - - - - 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 - 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 -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}')" 1 - echo "($datetime) - ${temp_f}'F - SYSTEM CRITICAL - (Difference of ${temp_diff})" >> ${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 - Sending system shutdown signals, if applicable. Check main Powerwall Logs" >> ${logtemp} - fi - else - - 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 -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 + temp_f=`echo "scale=2; $temp_c*1.8 + 32" | bc` + # echo "HERE: ${SENSOR} - ${temp_f} - ${temp_warn} - ${temp_crit} - ${temp_crit_sys}" + if [ "${PW_SENSOR_TYPE[${SENSORa}]}" != "esxi" ]; then + 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 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 [ "${PW_SENSOR_TYPE[${SENSORa}]}" == "esxi" ]; then - entityid=($(${mysql_conn} -e "SELECT id FROM servermonitor.entities WHERE BINARY name='${vSENSOR// /_}' AND sensorid='${PW_SENSOR_ID[${SENSORa}]}'")) - if [ "${entity[1]}" == "" ]; then - ${mysql_conn} -e "USE servermonitor; INSERT INTO entities (\`sensorid\`, \`name\`) VALUES ('${PW_SENSOR_ID[${SENSORa}]}','${vSENSOR// /_}')" - entityid=($(${mysql_conn} -e "SELECT id FROM servermonitor.entities WHERE BINARY name='${vSENSOR// /_}' AND sensorid='${PW_SENSOR_ID[${SENSORa}]}'")) - fi - entityid=${entityid[1]} - - QRY="USE servermonitor; INSERT INTO sensor_data (\`sensorid\`, \`entity\`, \`date\`, \`temp\`) VALUES ('${PW_SENSOR_ID[${SENSORa}]}','${entityid}','${datetime}','${temp_f}')" - elif [ "${temp_h}" != "" ]; then - QRY="USE servermonitor; INSERT INTO sensor_data (\`sensorid\`, \`date\`, \`temp\`, \`hum\`) VALUES ('${PW_SENSOR_ID[${SENSORa}]}','${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[${SENSORa}]}','${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 + 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 -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 - - done + 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}')" 1 + echo "($datetime) - ${temp_f}'F - SYSTEM CRITICAL - (Difference of ${temp_diff})" >> ${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 - Sending system shutdown signals, if applicable. Check main Powerwall Logs" >> ${logtemp} + fi + else + + 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 -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 [ "${PW_SENSOR_TYPE[${SENSORa}]}" == "esxi" ]; then + entityid=($(${mysql_conn} -e "SELECT id FROM servermonitor.entities WHERE BINARY name='${vSENSOR// /_}' AND sensorid='${PW_SENSOR_ID[${SENSORa}]}'")) + if [ "${entity[1]}" == "" ]; then + ${mysql_conn} -e "USE servermonitor; INSERT INTO entities (\`sensorid\`, \`name\`) VALUES ('${PW_SENSOR_ID[${SENSORa}]}','${vSENSOR// /_}')" + entityid=($(${mysql_conn} -e "SELECT id FROM servermonitor.entities WHERE BINARY name='${vSENSOR// /_}' AND sensorid='${PW_SENSOR_ID[${SENSORa}]}'")) + fi + entityid=${entityid[1]} + + QRY="USE servermonitor; INSERT INTO sensor_data (\`sensorid\`, \`entity\`, \`date\`, \`temp\`) VALUES ('${PW_SENSOR_ID[${SENSORa}]}','${entityid}','${datetime}','${temp_f}')" + elif [ "${temp_h}" != "" ]; then + QRY="USE servermonitor; INSERT INTO sensor_data (\`sensorid\`, \`date\`, \`temp\`, \`hum\`) VALUES ('${PW_SENSOR_ID[${SENSORa}]}','${datetime}','${temp_f}','${temp_h}')" + else + QRY="USE servermonitor; INSERT INTO sensor_data (\`sensorid\`, \`date\`, \`temp\`) VALUES ('${PW_SENSOR_ID[${SENSORa}]}','${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 + fi elif [ "${PW_HOST_ADDRESSES[${SENSORa}]}" == "" ]; then #sensor down if [ ! -f ${PW_TMPFOLDER}/${SENSORa}.down ] || [ $(expr `date +%s` - $(stat -c %Y ${PW_TMPFOLDER}/${SENSORa}.down)) -ge 1800 ]; then