#!/bin/bash
JQ="/appl/eap/sas/httpd/cgi-bin/jq-1.5";
##API call
# Step 1: Acquire the JSON data
json_data=$(cat)
#DATE=$(date +%Y%m%d%H%M%S)
cmd=""
result=""
flashcat_server_url="https://e...content-available-to-author-only...g.hk"
#alslog variables
als_log_url="https://e...content-available-to-author-only...g.hk/alsLog"
location_cd=""
workstation_id=$(hostname -f)
user_id=$USER
script_name=${0##*/}
content="The autohealing script executed."
project_code="EAPAUTO-ANSIBLE-ALS"
# Step 2: Extract values from the JSON data
target_ident=$(echo ${json_data} | ${JQ} -r '.target_ident')
tags=$(echo ${json_data} | ${JQ} -r '.tags')
is_recovered=$(echo ${json_data} | ${JQ} -r '.is_recovered')
hash=$(echo ${json_data} | ${JQ} -r '.hash')
echo "hash: ${hash}" > debug.txt
if [[ "${is_recovered}" == "false" ]]; then
# Step 3: Extract required tags and store them in variables
autoheal_category=""
autoheal_user=""
event_dc=""
active_dc=$(curl -sk https://${flashcat_server_url}//ha/dc/active)
for tag in $(echo "${tags}" | ${JQ} -r .[]); do
if [[ ${tag} == "autoheal_category="* ]]; then
autoheal_category=${tag#"autoheal_category="}
elif [[ ${tag} == "autoheal_user="* ]]; then
autoheal_user=${tag#"autoheal_user="}
elif [[ ${tag} == "environment="* ]]; then
environment=${tag#"environment="}
elif [[ ${tag} == "DC="* ]]; then
event_dc=${tag#"DC="}
echo "event_dc: ${event_dc}" >> debug.txt
fi
done
# Flashcat URL
if [[ "${environment}" == "dev" ]]; then
flashcat_server_url="https://e...content-available-to-author-only...g.hk"
als_log_url="https://e...content-available-to-author-only...g.hk/alsLog"
location_cd="TST"
elif [[ "${environment}" == "sit" || "${environment}" == "nonprd" ]]; then
flashcat_server_url="https://e...content-available-to-author-only...g.hk"
als_log_url="https://e...content-available-to-author-only...g.hk/alsLog"
location_cd="TST"
elif [[ "${environment}" == "prd" ]]; then
flashcat_server_url="https://e...content-available-to-author-only...g.hk"
als_log_url="https://e...content-available-to-author-only...g.hk:56797/alsLog"
location_cd="PRD"
fi
echo "flashcat_server_url: ${flashcat_server_url}" >> debug.txt
echo "als_log_url: ${als_log_url}" >> debug.txt
echo "location_cd: ${location_cd}" >> debug.txt
# Step 4: Execute the command with the extracted values
if [[ "${active_dc}" == "${event_dc}" ]]; then
if [[ -z "$target_ident" ]]; then
result+="Invalid target_ident "
elif [[ -z "$autoheal_category" ]]; then
result+="Invalid autoheal_category "
elif [[ -z "$autoheal_user" ]]; then
result+="Invalid autoheal_user"
else
cmd="/appl/eap/cfs/cmdb/ansible-playbook/run/operation/auto-healing/auto-healing.sh ${target_ident} ${autoheal_category} ${autoheal_user}"
#write log to als
response
=$
(curl
--write
-out
'%{http_code}' -s ${als_log_url
} -X POST
-kH
'Content-Type: application/json' -d
"{ \"logType\":\"INFO\", \"locationCd\":\"$location_cd\", \"workstationId\":\"$workstation_id\", \"projectCode\":\"$project_code\", \"correlationId\":\"$hash\", \"description\":\"$script_name\", \"content\":\"$content\" }"); if [[ $response != *'200'* ]]; then
echo "Failed to write ALS Log: $response";
fi
fi
else
result+="This is the event from passive site: ${event_dc}"
fi
echo "Content-type: application/json; charset=utf-8"
echo ""
echo "["
echo "{ \"result\" : \"$result\"}";
echo "]"
fi
#result=$(eval $cmd | grep stdout | egrep -w "is running|already running|ok|ansible_default")
#if [[ -z $result ]]; then
# json="Fail"
#else
# json="Pass"
#fi