#!/bin/bash
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#

#set -x

export http_proxy=http://PITC-Zscaler-Americas-Cincinnati3PR.proxy.corporate.ge.com:80
export HTTPS_PROXY=http://PITC-Zscaler-Americas-Cincinnati3PR.proxy.corporate.ge.com:80
export https_proxy=http://PITC-Zscaler-Americas-Cincinnati3PR.proxy.corporate.ge.com:80
export HTTP_PROXY=http://PITC-Zscaler-Americas-Cincinnati3PR.proxy.corporate.ge.com:80

# Global Variables
TOKEN=""
SERVICE_API_URL=""
TOKEN_URL=""
RESPONSE=""
API_RESP=""
CURL="/usr/bin/curl"
FIND="/bin/find"
GREP="/bin/grep"

# Exit codes
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3

USAGE="Usage: $0 -t Access_Token_URL -s Service_API_URL"

if [ "$#" == "0" ]; then
  echo "$USAGE"
  exit $STATE_UNKNOWN
fi

while (( $# )); do

case "$1" in
        --help)
        	echo "$USAGE"
        	exit $STATE_UNKNOWN
        ;;
        -h)
        	echo "$USAGE"
        	exit $STATE_UNKNOWN
        ;;
        -t)
		if [ ! -z "$2" ]; then 
	 	 TOKEN_URL=""
	 	 TOKEN_URL="$2"
		else 
	 	 echo "$USAGE"
	 	 exit $STATE_UNKNOWN
		fi
        ;;
        -s)
		if [ ! -z "$2" ]; then
	 	 SERVICE_API_URL=""
	 	 SERVICE_API_URL="$2"
		else
	 	 echo "$USAGE"
	 	 exit $STATE_UNKNOWN
		fi
        ;;
        *)
        	echo "$USAGE"
		exit $STATE_UNKNOWN
	;;
esac
shift
shift
done

get_token(){
RESPONSE=""
RESPONSE=`$CURL -s -X POST "$TOKEN_URL"  -H 'Authorization: Basic ZGlnaXRhbF9wb3J0YWwtYWNzX2RldjpSMVhFRU84UUQ3cUJiTTFa' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Postman-Token: 997e21e7-0dae-4ab0-a365-38f1d8fce1b0' -H 'cache-control: no-cache' -d 'grant_type=client_credentials&client_id=digital_portal-acs_dev&undefined='` 2>&1 >/dev/null
}

get_token
if [ ! -z "$RESPONSE" ]; then
	IFS=',' read -ra my_array <<< "$RESPONSE"
	RESP="${my_array[0]}"
	TOKEN=`echo ${RESP:16} | sed 's/"//g'`
fi

#echo
#echo "TOKEN: $TOKEN"
#echo "---------------------------------"
#echo "API URL: $SERVICE_API_URL"
#echo "---------------------------------"

check_service(){
API_RESP=""
API_RESP=`$CURL -s -X GET "$SERVICE_API_URL" -H "Authorization: Bearer $TOKEN" -H 'Postman-Token: 55972109-27bb-44aa-957c-741bf96aa68d' -H 'cache-control: no-cache'` 2>&1 >/dev/null
}

check_service

#echo "API RESPONSE: $API_RESP"
FILTER_RESP=`echo "$API_RESP" | sed 's/[{}"]//g'`
#echo "FILTER RESPONSE: $FILTER_RESP"

OK(){
echo "OK: $FILTER_RESP"
#exit $STATE_OK
}

WARNING(){
echo "WARNING: $FILTER_RESP"
exit $STATE_WARNING
}

CRITICAL(){
echo "CRITICAL: $FILTER_RESP"
#exit $STATE_CRITICAL
}

UNKNOWN(){
echo "UNKNOWN: $FILTER_RESP"
exit $STATE_UNKNOWN
}

if [[ "$FILTER_RESP" =~ .*Health:* ]]
then
 OK
else
 CRITICAL
fi
