#!/bin/bash

###
# This script is designed by Sean Koniarz to ensure a graceful shutdown and restart of Security Analytics Concentrator Services.
# Version: 1.0
# Release Date: 8/29/2014
# The script is given as is and is not certified by RSA.
# Notes: Additional functionality such as shutting down aggregation will be added to the script.
###

# Variables
LOG=<log location>/restartConcentrator.log
service=restart_concentrator
DATE="`date +"%m-%d-%Y %r"`"
# User information for NwConsole
USER="<user>"
PASSWORD="<password>"


# Grab Status of concentrator aggregation
STATUS="`NwConsole -c login localhost:50005 $USER $PASSWORD  -c concentrator/devices ls depth=3 |grep -i status --color |awk '{ print $5 }'`"


### For Testing
#echo $STATUS
#exit 0
###

# Check for log file
if [ ! -f "$LOG" ]
then
        touch $LOG
fi

# Prompt for input for error checking
echo -n "Did you turn off aggregation in the web UI? (y or n)  "

read answer

## For Testing
#echo $answer
##

if [[ "$answer" == "y" || "$answer" == "Y" ]]; then
        if [ "$STATUS" == "consuming" ];then
                echo "Concentrator still consuming."
                echo "Exiting."

                # Create Log
                echo $DATE "Concentrator failed to restart due to aggregation on." >> $LOG
                exit 0
        else
                echo "Restarting Concentrator, stand by."
                restart nwconcentrator
                echo "Concentrator has restarted please wait for database to initialize"

                # Create Log
                echo $DATE "Concentrator was restarted." >> $LOG
                exit 0
        fi
elif [[ "$answer" == "n" || "$answer" == "N" ]]; then
        echo "Please turn of aggregation before restarting the service.  This can be done by following the Security Analytics Administration Guide."

        # Create Log
        echo $DATE "Concentrator failed to restart due to aggregation on." >> $LOG
        exit 0
else
        echo "No Answer Given, exiting."

        # Create Log
        echo $DATE "Concentrator failed to restart due to no or incorrect input." >> $LOG
        exit 0
fi
