#!/bin/bash
set -u
set -e
HOSTNAME="$(bigpipe system hostname show | awk \'{print $6}\')"
MAIL_BIN=`which mail`
EMAIL_TO="user@domain.tld"
EMAIL_SUBJECT="$HOSTNAME REBOOT"
TIME=`date "+%Y%m%d-%H:%M:%S"`
UPTIME_SECONDS="$(cat /proc/uptime | awk \'{printf "%.0f\\n", $1}\')"
MAX_UPTIME="259200"
SHUTDOWN_BIN=$(which shutdown)
RESULT_FILE=/home/admin/reboot-cron-job-result
if [ -e $RESULT_FILE ] ; then
echo "Found an old $RESULT_FILE, removing..."
rm -f $RESULT_FILE
fi
echo "Starting pre-reboot checks on $HOSTNAME as of $TIME..."
echo "Starting pre-reboot checks on $HOSTNAME as of $TIME..." >> $RESULT_FILE
# Check whether current unit is in active or standby mode
if [[ $(bigpipe failover show) == *active* ]]; then
echo "Unit is active..."
echo "Unit is active..." >> $RESULT_FILE
FAILOVER_STATE="ACTIVE"
else
echo "I\'m standby..."
echo "I\'m standby..." >> $RESULT_FILE
FAILOVER_STATE="STANDBY"
# exit 0
fi
# Check if peer is alive (ping)
if ping -c 1 -w 5 peer &>/dev/null ; then
echo "Peer is up..."
echo "Peer is up..." >> $RESULT_FILE
PEER_STATE="UP"
else
echo "Peer is down..."
echo "Peer is down..." >> $RESULT_FILE
PEER_STATE="DOWN"
# exit 0
fi
# Check uptime
if [ "$UPTIME_SECONDS" -le "$MAX_UPTIME" ] ; then
echo "Uptime of $UPTIME_SECONDS seconds is less than or equal to the maximum of $MAX_UPTIME seconds"
echo "Uptime of $UPTIME_SECONDS seconds is less than or equal to the maximum of $MAX_UPTIME seconds" >> $RESULT_FILE
UPTIME_CHECK="GOOD"
else
echo "Uptime of $UPTIME_SECONDS seconds is greater than the maxium of $MAX_UPTIME seconds"
echo "Uptime of $UPTIME_SECONDS seconds is greater than the maxium of $MAX_UPTIME seconds" >> $RESULT_FILE
UPTIME_CHECK="BAD"
fi
# Checking Config Sync Status
if [[ $(bigpipe config sync show) == *Synchronized* ]] ; then
echo "Active/Standby Configuration is synchronized"
echo "Active/Standby Configuration is synchronized" >> $RESULT_FILE
CONFIG_SYNC_STATE="SYNCHRONIZED"
else
echo "Active/Standby Configuration is NOT synchronised"
echo "Active/Standby Configuration is NOT synchronised" >> $RESULT_FILE
CONFIG_SYNC_STATE="UNSYNCHRONIZED"
fi
MESSAGE="$TIME ## $HOSTNAME is $FAILOVER_STATE unit, peer is $PEER_STATE, config sync is $CONFIG_SYNC_STATE ##"
if [[ $FAILOVER_STATE == \'ACTIVE\' && $PEER_STATE == \'UP\' && $UPTIME_CHECK == \'BAD\' ]] ; then
# Run config sync if the configurations are not synchronized, test again after, exit if still unsynch\'d
if [ $CONFIG_SYNC_STATE == \'UNSYNCHRONIZED\' ] ; then
echo "Synchronizing the configuration..."
echo "Synchronizing the configuration..." >> $RESULT_FILE
bigpipe config sync all
if [[ ! $(bigpipe config sync show) == *Synchronized* ]] ; then
echo "Configuration still not synchronized, bailing out..."
echo "Configuration still not synchronized, bailing out..." >> $RESULT_FILE
exit 0
fi
echo "Configuration sync successful..."
echo "Configuration sync successful..." >> $RESULT_FILE
fi
echo "$MESSAGE rebooting!! "
echo "$MESSAGE rebooting!!" >> $RESULT_FILE
logger -t BIGIP-ADMIN-SCRIPT -p local0.notice "$MESSAGE rebooting!!"
$MAIL_BIN -s "$EMAIL_SUBJECT" "$EMAIL_TO" < $RESULT_FILE
# reboot
# shutdown -r now
$SHUTDOWN_BIN -r now
echo "I would have rebooted but this was a test"
echo "I would have rebooted but this was a test" >> $RESULT_FILE
logger -t BIGIP-ADMIN-SCRIPT -p local0.notice "I would have rebooted but this was a test"
else
echo "$MESSAGE nothing to do... "
echo "$MESSAGE nothing to do..." >> $RESULT_FILE
logger -t BIGIP-ADMIN-SCRIPT -p local0.notice "$MESSAGE doing nothing"
$MAIL_BIN -s "$EMAIL_SUBJECT" "$EMAIL_TO" < $RESULT_FILE
fi