Last active
February 23, 2017 09:17
-
-
Save thanosa75/d0eb2a30cf280948c0155fcc17b0d4be to your computer and use it in GitHub Desktop.
Script that checks a monitor host (via MAC address) to see if it is alive. If not, it will SSH (assume keys have been setup) to another host and shut it down.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# A quick-and-dirty UPS shutdown monitor by Thanos | |
# | |
# Can perform shutdown of a "precious" machine that is on a UPS | |
# based on network reachability of a "inexpensive" machine that is | |
# not on a UPS. In my case, the "inexpensive" is a RPI that is always on. | |
# | |
# Cron entry for this script (uses "pi" user): | |
# */2 * * * * /home/pi/check-power.sh > /home/pi/powercheck.log 2>&1 | |
# MAC address of system without UPS support | |
MAC="XX:YY:ZZ:BB:CC:DD" | |
# Host (and presumably user) to SSH to | |
HOST="[email protected]" | |
# CMD to execute for shutdown | |
# QNAP uses "halt", usually Linux would be "shutdown -h now" | |
CMD="/sbin/halt" | |
# LOG file to keep shutdown commands | |
LOSSLOG=/home/pi/powerloss.log | |
# find the IP address from the MAC we got | |
IP=$(cat /proc/net/arp | grep $MAC | awk '{print$1}') | |
if [ "$IP" != "" ] ; then | |
echo -n "`date "+%d/%m/%y %H:%M:%S" ` monitor found at $IP " | |
ping -c 5 -i 12 -w 70 $IP &> /dev/null | |
RET=$? | |
if [ $RET -gt 0 ]; then | |
echo -n "down (`date "+%H:%M:%S"`)..." | |
echo "`date "+%d/%m/%y %H:%M:%S" ` Will now issue $CMD to $HOST, $IP was unreachable" >> $LOSSLOG | |
sync;sync # make sure we got the log entry written (about to lose power also :D) | |
ssh $HOST $CMD | |
break | |
else | |
echo "up!" | |
fi | |
else | |
echo -n "`date "+%d/%m/%y %H:%M:%S" ` monitor MAC not found on network" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment