#!/bin/bash
#                                                                                       #
# SCRIPT: blukrypt_health_check.sh                                                             #
# DATE:   22-May-2017                                                                  #
# REV:    1.0                                                                           #
# DEVELOPED BY: Parablu Systems                                                         #
# DEVELOPER: Vaibhav K
# PURPOSE: Checks Blukrypt Health Status                    #
# PLATFORM: Ubuntu

PARABLU_COMPONENT_SCRIPT_PATH=./common_health_check.sh
source $PARABLU_COMPONENT_SCRIPT_PATH


function blukrypt_component_check ()
{
	echo "CHECKING BLUKRYPT COMPONENTS......"
	tomcat_job_check
	tomcat_pcb_check
	memcache_check
	apache_check
#	activemq_check
	if [ "$SERVER_TYPE" != "0" ]; then
		mongo_check
	fi
}


function blukrypt_json_object ()
{
	hostname=`hostname`
	current_ip=$(ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
            if grep -q unknown-245 /var/lib/dhcp/dhclient.eth0.leases; then
                current_ip=$(curl ipinfo.io/ip 2>/dev/null)
            fi
	date=`date`

	bkJsonObject="{
    	\"hostName\" : \"$hostname\",
    	\"ipAddress\" : \"$current_ip\",
    	\"updatedTime\" : \"$date\",
    	\"processes\" : [
			$tomcatJobJsonObject,$tomcatPcbJsonObject,$memcacheJsonObject,$apacheJsonObject,$activemqJsonObject
		        ],
    	\"physicalMemory\": [
			      $jsonObjectMemRoot,$jsonObjectMemParablu,$jsonObjectMemMdatabase
		 	    ],
	\"memory\" : [
		     $jsonObjectRamMem,$jsonObjectRamSwap
	  	  ]
}"

#curl -i -X POST -H "Content-Type:application/json" -d "$bkJsonObject" http://localhost:1119/watchdog/components
}

function monitor_blukrypt ()
{
        blukrypt_component_check
        echo "********************************************************************************************************************" >>watchdog.log
        echo "">>watchdog.log
        echo "********************************************************************************************************************" >>watchdog.log
        echo -e "DISK\t\t | AVAILABLE_MEMORY\t | USED_MEMORY\t | USED_PERCENTAGE\t" >>watchdog.log
        space_check
        ram_check
#        jsonObjectMemRoot=$(memory_root)
        jsonObjectMemParablu=$(memory_parablu)
#	jsonObjectMemMdatabase=$(memory_mdatabase)
        echo "********************************************************************************************************************" >>watchdog.log
        echo "">>watchdog.log
        echo "********************************************************************************************************************" >>watchdog.log
        echo -e "MEMORY_TYPE\t | TOTAL_MEMORY\t | USED_MEMORY\t | FREE_MEMORY\t | BUFFER\t | CACHE\t" >>watchdog.log
        jsonObjectRamMem=$(ram_mem)
        jsonObjectRamSwap=$(ram_swap)
#        blukrypt_json_object
}

