Files
powerwall/powerwall.sh
2022-09-10 11:01:19 -05:00

163 lines
5.9 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=0
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
datetime="${temp_d} ${temp_t}"
datetime=$(TZ=UTC0 date -d "${datetime}" +%s)
datetime=`date -d @${datetime} +'%Y-%m-%d %H:%M:%S'`
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" = "1" ]; then
echo "($datetime) WARNING TEMP: ${temp_f}°F - (Difference of ${temp_diff}°)" | mail -s "${1} TEMP WARNING" $email_alert
echo "alert sent"
last_temp=$temp_f
fi
echo "($datetime) WARNING: ${temp_f}°F - (Difference of ${temp_diff}°)"
elif [ $(bc -l <<< "$temp_f >= $temp_crit") -eq 1 ]; then
if [ $temp_diff -gt 1 ] || [ "$last_temp" = "1" ]; then
echo -e "($datetime) CRITICAL TEMP: ${temp_f}°F - (Difference of ${temp_diff}°)\nShutting down servers!!" | mail -s "${1} TEMP CRITICAL" $email_alert
echo "alert sent"
last_temp=$temp_f
for ESXIHOST in "${ESXIHOSTS[@]}"; do
echo "Shutting Down: ${ESXIHOST}"
/usr/bin/ssh root@${ESXIHOST} "cp /vmfs/volumes/iSCSI2-Datastore2\ \(R5\)\(1-4\)/\!SCRIPTS/esxidown/async.sh /vmfs/volumes/iSCSI2-Datastore2\ \(R5\)\(1-4\)/\!SCRIPTS/esxidown/async-${ESXIHOST}.sh"
/usr/bin/ssh root@${ESXIHOST} "cp /vmfs/volumes/iSCSI2-Datastore2\ \(R5\)\(1-4\)/\!SCRIPTS/esxidown/esxidown.sh /vmfs/volumes/iSCSI2-Datastore2\ \(R5\)\(1-4\)/\!SCRIPTS/esxidown/esxidown-${ESXIHOST}.sh"
/usr/bin/ssh root@${ESXIHOST} "sed -i 's/\$SCRIPTPATH\/esxidown.sh/\$SCRIPTPATH\/esxidown-${ESXIHOST}.sh/g' /vmfs/volumes/iSCSI2-Datastore2\ \(R5\)\(1-4\)/\!SCRIPTS/esxidown/async-${ESXIHOST}.sh"
/usr/bin/ssh root@${ESXIHOST} "/vmfs/volumes/iSCSI2-Datastore2\ \(R5\)\(1-4\)/\!SCRIPTS/esxidown/async-${ESXIHOST}.sh"
done
fi
echo "($datetime) CRITICAL: ${temp_f}°F - (Difference of ${temp_diff}°)"
else
if [ $last_temp -gt 1 ]; then
echo -e "($datetime) NORMAL TEMP: ${temp_f}°F\nPrevious Temp: ${last_temp}°F" | mail -s "${1} BACK TO NORMAL" $email_alert
echo "alert sent"
elif [ $last_temp -eq 0 ]; then
echo "NORMAL TEMP: ${temp_f}°F" | mail -s "${1} TEMP NORMAL" $email_alert;
fi
last_temp=1
echo "($datetime) normal: ${temp_f}°F"
fi
QRY="USE servermonitor; INSERT INTO sensor_data (\`sensorid\`, \`date\`, \`temp\`, \`hum\`) VALUES ('${SENSOR_ID[$1]}','${datetime}','${temp_f}','${temp_h}')"
${mysql_conn} -e "${QRY}"
else
echo "($datetime) ERROR reading temperature" | mail -s "${1} ERROR" $email_alert
echo "alert sent"
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