====== reboot apc ups ====== touch /tmp/powerfail apcupsd --killpower ====== esxishutdown ====== * source https://raw.githubusercontent.com/sixdimensionalarray/esxidown/master/esxidown.sh #!/bin/sh # ESXi 5.1 host automated shutdown script # these are the VM IDs to shutdown in the order specified # use the SSH shell, run "vim-cmd vmsvc/getallvms" to get ID numbers # specify IDs separated by a space SERVERIDS=$(vim-cmd vmsvc/getallvms | sed -e '1d' -e 's/ \[.*$//' | awk '$1 ~ /^[0-9]+$/ {print $1}') # New variable to allow script testing, assuming the vim commands all work to issue shutdowns # can be "0" or "1" TEST=0 # script waits WAIT_TRYS times, WAIT_TIME seconds each time # number of times to wait for a VM to shutdown cleanly before forcing power off. WAIT_TRYS=20 # how long to wait in seconds each time for a VM to shutdown. WAIT_TIME=10 # ------ DON'T CHANGE BELOW THIS LINE ------ validate_shutdown() { vim-cmd vmsvc/power.getstate $SRVID | grep -i "off" > /dev/null 2<&1 STATUS=$? if [ $STATUS -ne 0 ]; then if [ $TRY -lt $WAIT_TRYS ]; then # if the vm is not off, wait for it to shut down TRY=$((TRY + 1)) echo "Waiting for guest VM ID $SRVID to shutdown (attempt #$TRY)..." sleep $WAIT_TIME validate_shutdown else # force power off and wait a little (you could use vmsvc/power.suspend here instead) echo "Unable to gracefully shutdown guest VM ID $SRVID... forcing power off." if [ $TEST -eq 0 ]; then vim-cmd vmsvc/power.off $SRVID fi sleep $WAIT_TIME fi fi } # enter maintenance mode immediately echo "Entering maintenance mode..." if [ $TEST -eq 0 ]; then esxcli system maintenanceMode set -e true -t 0 & fi # read each line as a server ID and shutdown/poweroff for SRVID in $SERVERIDS do TRY=0 vim-cmd vmsvc/power.getstate $SRVID | grep -i "off\|Suspended" > /dev/null 2<&1 STATUS=$? if [ $STATUS -ne 0 ]; then echo "Attempting shutdown of guest VM ID $SRVID..." if [ $TEST -eq 0 ]; then vim-cmd vmsvc/power.shutdown $SRVID fi validate_shutdown else echo "Guest VM ID $SRVID already off..." fi done # guest vm shutdown complete echo "Guest VM shutdown complete..." # shutdown the ESXi host echo "Shutting down ESXi host after 10 seconds..." if [ $TEST -eq 0 ]; then esxcli system shutdown poweroff -d 10 -r "Automated ESXi host shutdown - esxidown.sh" fi # exit maintenance mode immediately before server has a chance to shutdown/power off # NOTE: it is possible for this to fail, leaving the server in maintenance mode on reboot! echo "Exiting maintenance mode..." if [ $TEST -eq 0 ]; then esxcli system maintenanceMode set -e false -t 0 fi # exit the session exit ====== apc esxishutdown.sh ====== #!/bin/sh #variables esxi1=10.11.12.109 conn_check=0 logfile=/root/apcshutdown-onbattery.log TIME=`date +%b-%d-%y` DATE=`date` #check if the server is up, and pass over the output to the variable ssh -o ConnectTimeout=5 -i /root/.ssh/id_rsa root@$esxi1 'exit' conn_check=$(echo $?) #log the output to the file echo "Powerout - `date`: Return code for ssh = $conn_check" >> $logfile logger "Powerout - `date`: Retern code for ssh = $conn_check" #check if the server is up and try to shutdown if [ $conn_check = 0 ] then echo "Powerout - `date`: I can see the ESXI is up and I will poweroff the server" >> $logfile logger "Powerout - `date`: I can see the ESXI is up and I will poweroff the server" echo "Powerout - `date`: executing the /sbin/poweroff/sbin/shutdown.sh /sbin/poweroff" >> $logfile logger "Powerout -`date`: esxishutdown.sh executing the /sbin/poweroff/sbin/shutdown.sh /sbin/poweroff" #reset the license ssh -i /root/.ssh/id_rsa root@$esxi1 'cp /etc/vmware/.#license.cfg /etc/vmware/license.cfg' #shutdown the esxi ssh -y -i /root/.ssh/id_rsa root@$esxi1 '/sbin/shutdown.sh && /sbin/poweroff' & echo "Powerout - `date`: Sleeping for 6minutes till ESXi powers off" >> $logfile logger "Powerout - `date`: Sleeping for 6minutes till ESXi powers off" sleep 6m else echo "Powerout - `date`: I was no table to connect to the server, no need to shutdown" >> $logfile logger "Powerout - `date`: I was no table to connect to the server, no need to shutdown" fi #shutdown the UPS now and log the actions echo "Powerout - `date`: shutdown the UPS" >> $logfile logger "Powerout - `date`: shutdown the UPS" /etc/apcupsd/apccontrol killpower #read the status of the battery and send it via email #/etc/scripts/script-batt-check-down.sh bcharge=$(apcaccess | grep BCHARGE) bcharge1=$(apcaccess) #### rm /tmp/msg-batt-alert-down.txt echo "To: burimali@yahoo.com" >> /tmp/msg-batt-alert-down.txt echo "From: bur.nagios.net@gmail.com" >> /tmp/msg-batt-alert-down.txt echo "Subject: CoreRT2 - alert - down - $bcharge $DATE" >> /tmp/msg-batt-alert-down.txt echo "" >> /tmp/msg-batt-alert-down.txt echo "$bcharge1" >> /tmp/msg-batt-alert-down.txt echo "" >> /tmp/msg-batt-alert-down.txt echo "EOF" >> /tmp/msg-batt-alert-down.txt ssmtp burimali@yahoo.com < /tmp/msg-batt-alert-down.txt #write logs fo the shutdown action echo "Powerout - `date`: esxishutdown.sh is executing esxishutdown-pi.sh" >> $logfile logger "Powerout - `date`: esxishutdown.sh is executing esxishutdown-pi.sh" #shutdown raspberry PI it will have 1 minute to poweroff /etc/scripts/esxishutdown-pi.sh &