Advertisement
naturalnetworks

BIGIP_LTM_Reboot_Script

Sep 3rd, 2011
6,735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.68 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -u
  4. set -e
  5.  
  6. HOSTNAME="$(bigpipe system hostname show | awk '{print $6}')"
  7.  
  8. MAIL_BIN=`which mail`
  9. EMAIL_SUBJECT="$HOSTNAME REBOOT"
  10.  
  11. TIME=`date "+%Y%m%d-%H:%M:%S"`
  12.  
  13. UPTIME_SECONDS="$(cat /proc/uptime | awk '{printf "%.0f\n", $1}')"
  14. MAX_UPTIME="259200"
  15.  
  16. SHUTDOWN_BIN=$(which shutdown)
  17.  
  18. RESULT_FILE=/home/admin/reboot-cron-job-result
  19.  
  20. if [ -e $RESULT_FILE ] ; then
  21.     echo "Found an old $RESULT_FILE, removing..."
  22.     rm -f $RESULT_FILE
  23. fi
  24.  
  25. echo "Starting pre-reboot checks on $HOSTNAME as of $TIME..."
  26. echo "Starting pre-reboot checks on $HOSTNAME as of $TIME..." >> $RESULT_FILE
  27.  
  28. # Check whether current unit is in active or standby mode
  29. if [[ $(bigpipe failover show) == *active* ]]; then
  30.     echo "Unit is active..."
  31.     echo "Unit is active..." >> $RESULT_FILE
  32.     FAILOVER_STATE="ACTIVE"
  33. else
  34.     echo "I'm standby..."
  35.     echo "I'm standby..." >> $RESULT_FILE
  36.     FAILOVER_STATE="STANDBY"
  37. # exit 0
  38. fi
  39.  
  40. # Check if peer is alive (ping)
  41. if ping -c 1 -w 5 peer &>/dev/null ; then
  42.     echo "Peer is up..."
  43.     echo "Peer is up..." >> $RESULT_FILE
  44.     PEER_STATE="UP"
  45. else
  46.     echo "Peer is down..."
  47.     echo "Peer is down..." >> $RESULT_FILE
  48.     PEER_STATE="DOWN"
  49. # exit 0
  50. fi
  51.  
  52. # Check uptime
  53. if [ "$UPTIME_SECONDS" -le "$MAX_UPTIME" ] ; then
  54.     echo "Uptime of $UPTIME_SECONDS seconds is less than or equal to the maximum of $MAX_UPTIME seconds"
  55.     echo "Uptime of $UPTIME_SECONDS seconds is less than or equal to the maximum of $MAX_UPTIME seconds" >> $RESULT_FILE
  56.     UPTIME_CHECK="GOOD"
  57. else
  58.     echo "Uptime of $UPTIME_SECONDS seconds is greater than the maxium of $MAX_UPTIME seconds"
  59.     echo "Uptime of $UPTIME_SECONDS seconds is greater than the maxium of $MAX_UPTIME seconds" >> $RESULT_FILE
  60.     UPTIME_CHECK="BAD"
  61. fi
  62.  
  63. # Checking Config Sync Status
  64. if [[ $(bigpipe config sync show) == *Synchronized* ]] ; then
  65.     echo "Active/Standby Configuration is synchronized"
  66.     echo "Active/Standby Configuration is synchronized" >> $RESULT_FILE
  67.     CONFIG_SYNC_STATE="SYNCHRONIZED"
  68. else
  69.     echo "Active/Standby Configuration is NOT synchronised"
  70.     echo "Active/Standby Configuration is NOT synchronised" >> $RESULT_FILE
  71.     CONFIG_SYNC_STATE="UNSYNCHRONIZED"
  72. fi
  73.  
  74.  
  75. MESSAGE="$TIME ## $HOSTNAME is $FAILOVER_STATE unit, peer is $PEER_STATE, config sync is $CONFIG_SYNC_STATE ##"
  76.  
  77. if [[ $FAILOVER_STATE == 'ACTIVE' && $PEER_STATE == 'UP' && $UPTIME_CHECK == 'BAD' ]] ; then
  78.  
  79.     # Run config sync if the configurations are not synchronized, test again after, exit if still unsynch'd
  80.     if [ $CONFIG_SYNC_STATE == 'UNSYNCHRONIZED' ] ; then
  81.         echo "Synchronizing the configuration..."
  82.         echo "Synchronizing the configuration..." >> $RESULT_FILE
  83.         bigpipe config sync all
  84.  
  85.         if [[ ! $(bigpipe config sync show) == *Synchronized* ]] ; then
  86.             echo "Configuration still not synchronized, bailing out..."
  87.             echo "Configuration still not synchronized, bailing out..." >> $RESULT_FILE
  88.             exit 0
  89.         fi
  90.         echo "Configuration sync successful..."
  91.         echo "Configuration sync successful..." >> $RESULT_FILE
  92.     fi
  93.  
  94.     echo "$MESSAGE rebooting!! "
  95.     echo "$MESSAGE rebooting!!" >> $RESULT_FILE
  96.     logger -t BIGIP-ADMIN-SCRIPT -p local0.notice "$MESSAGE rebooting!!"
  97.     $MAIL_BIN -s "$EMAIL_SUBJECT" "$EMAIL_TO" < $RESULT_FILE
  98.  
  99.     # reboot
  100.     # shutdown -r now
  101.     $SHUTDOWN_BIN -r now
  102.  
  103.     echo "I would have rebooted but this was a test"
  104.     echo "I would have rebooted but this was a test" >> $RESULT_FILE
  105.     logger -t BIGIP-ADMIN-SCRIPT -p local0.notice "I would have rebooted but this was a test"
  106.  
  107. else
  108.  
  109.     echo "$MESSAGE nothing to do... "
  110.     echo "$MESSAGE nothing to do..." >> $RESULT_FILE
  111.     logger -t BIGIP-ADMIN-SCRIPT -p local0.notice "$MESSAGE doing nothing"
  112.     $MAIL_BIN -s "$EMAIL_SUBJECT" "$EMAIL_TO" < $RESULT_FILE
  113.  
  114. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement