#!/bin/bash
PARABLU_COMPONENT_SCRIPT_PATH=../config/parablu_component_config.sh
PARABLU_CONFIG_SCRIPT_PATH=../config/parablu_config.sh
source $PARABLU_COMPONENT_SCRIPT_PATH
source $PARABLU_CONFIG_SCRIPT_PATH

export PATH=$PATH:/sbin


for ping_ip in ${OTHER_WATCHDOG_IP[@]}
                do

                        get_host=$(ssh $SSH_USERNAME@$ping_ip "hostname")
			ssh -p $SSH_PORT $SSH_USERNAME@$ping_ip test  -d "/var/lib/tomcat-pcb1/webapps/BluKryptBuilder"

			if [ $? -eq 0 ]; then

	                        call_bkapi=`curl -k /dev/null --max-time 60 --silent --get --write-out '%{http_code}\n'  https://$ping_ip/BluKryptBuilder/ping/`
        	                echo "$call_bkapi" | grep 200
                	        if [ $? -eq 0 ]; then

                        	        echo "PG Machine $get_host is UP" >> pg_ping.log
                                	echo "" >> pg_ping.log
	                        else
        	                        echo "PG Machine $get_host is DOWN" >> pg_ping.log
                	                ssh $SSH_USERNAME@$ping_ip "service apache2 restart"
                        	        #echo "Apache Service restarted" >> pg_ping.log
                                	echo "" >> pg_ping.log
	                        fi
			fi

                done

echo "`cat pg_ping.log`" | mail -s "PG Check of [$CUSTOMER_NAME] at `date`" $WATCHDOG_MAIL_ADDRESS -aFrom:$WATCHDOG_FROM_MAIL_ADDRESS
rm -rf pg_ping.log
