Files
powerwall/powerwall.sh
2022-09-03 14:46:16 -05:00

140 lines
4.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# powerwall - CLI commands to control VM guest power
action="$1"
FOLDER='/opt/idssys/powerwall'
source $FOLDER/defaults.inc
source $FOLDER/powerwall.conf
source /opt/idssys/defaults/colors.inc
source /opt/idssys/defaults/default.inc
shopt -s lastpipe
#ssh root@10.5.10.35 '/vmfs/volumes/NFS_ESXi-Vault/esxi-shutdown.sh > /dev/null 2>&1' & > /dev/null 2>&1
CHECKTEMP(){
mqtt_message=`${mqtt_conn} -t tele/${1}/SENSOR -C 1`
echo $mqtt_message | cut -d':' -f 6 | cut -d',' -f 1 | read temp_c
if [ "$temp_c" != "null" ] && [ "$temp_c" != "" ]; then
temp_f=`echo "scale=2; $temp_c*1.8 + 32" | bc`
#echo "$temp_c -> $temp_f"
echo ${TEMP_THRESHOLDS[${1}]} | cut -d',' -f 1 | read temp_warn
echo ${TEMP_THRESHOLDS[${1}]} | cut -d',' -f 2 | read temp_crit
if [ $(bc -l <<< "$temp_f >= $temp_warn") -eq 1 ] && [ $(bc -l <<< "$temp_f < $temp_crit") -eq 1 ]; then
echo "WARNING: $temp_f°F"
elif [ $(bc -l <<< "$temp_f >= $temp_crit") -eq 1 ]; then
echo "CRITICAL: $temp_f°F"
else
echo "normal: $temp_f°F"
fi
fi
}
CHECKTEMP_SERVICE(){
last_temp=1
mqtt_message=`${mqtt_conn} -t tele/${1}/SENSOR -C 1`
while true
do
${mqtt_conn} -t tele/${1}/SENSOR | while read -r mqtt_message
do
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
echo $mqtt_message | cut -d'T' -f 2 | cut -d"\"" -f 3 | read temp_d
echo $mqtt_message | cut -d'T' -f 3 | cut -d"\"" -f 1 | read temp_t
if [ "$temp_c" != "null" ] && [ "$temp_c" != "" ]; then
temp_f=`echo "scale=2; $temp_c*1.8 + 32" | bc`
#echo "$temp_c -> $temp_f"
echo ${TEMP_THRESHOLDS[${1}]} | cut -d',' -f 1 | read temp_warn
echo ${TEMP_THRESHOLDS[${1}]} | cut -d',' -f 2 | read temp_crit
temp_diff=$((${last_temp%.*} - ${temp_f%.*}))
[ $temp_diff -lt 0 ] && temp_diff=$(($temp_diff * -1))
if [ $(bc -l <<< "$temp_f >= $temp_warn") -eq 1 ] && [ $(bc -l <<< "$temp_f < $temp_crit") -eq 1 ]; then
if [ $temp_diff -gt 1 ] || [ "$last_temp" == "0" ]; then
echo "($temp_diff) WARNING TEMP: $temp_f°F" | mail -s "${1} TEMP WARNING" $email_alert
last_temp=$temp_f
fi
echo "($temp_d @ $temp_t) WARNING: $temp_f°F"
elif [ $(bc -l <<< "$temp_f >= $temp_crit") -eq 1 ]; then
if [ $temp_diff -gt 1 ] || [ "$last_temp" == "0" ]; then
echo "CRITICAL TEMP: $temp_f°F" | mail -s "${1} TEMP CRITICAL" $email_alert
last_temp=$temp_f
fi
echo "($temp_d @ $temp_t) CRITICAL: $temp_f°F"
else
#last_temp=1
# echo "NORMAL TEMP: $temp_f°F" | mail -s "${1} TEMP NORMAL" $email_alert
echo "($temp_d @ $temp_t) normal: $temp_f°F"
fi
QRY="USE servermonitor; INSERT INTO sensor_data (\`sensorid\`, \`date\`, \`temp\`, \`hum\`) VALUES ('${SENSOR_ID[$1]}','${temp_d} ${temp_t}','${temp_f}','${temp_h}')"
${mysql_conn} -e "${QRY}"
fi
done
sleep 10
done # &
}
UPDATE(){
echo -en "${idsCL[LightCyan]}Checking for updates...${idsCL[Default]}"
echo ""
if curl -s --head --request GET https://git.schroedercity.com | grep "HTTP/2 200" > /dev/null; then
cd /opt/idssys/defaults
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
fi
cd /opt/idssys/powerwall
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
fi
echo -en "\e[1A";
echo -e "\e[0K\r ${idsCL[Green]}Updates Completed${idsCL[Default]}"
fi
}
if [ ${action-x} ]; then
case $action in
update) UPDATE;;
checktemp) CHECKTEMP ${2};;
checktemp_service)
if [ "${2}" == "stop" ]; then
service_pid=`systemctl show --property MainPID --value ${SERVICE_NAME[${3}]}`
/bin/kill -9 $service_pid
/bin/systemctl stop $srvcname
#/usr/bin/killall bash
else
UPDATE
CHECKTEMP_SERVICE ${2}
fi
;;
esac
fi
exit 0