fork download
  1. #!/bin/bash
  2. JQ="/appl/eap/sas/httpd/cgi-bin/jq-1.5";
  3. ##API call
  4.  
  5. # Step 1: Acquire the JSON data
  6. json_data=$(cat)
  7. #DATE=$(date +%Y%m%d%H%M%S)
  8. cmd=""
  9. result=""
  10. flashcat_server_url="https://e...content-available-to-author-only...g.hk"
  11. #alslog variables
  12. als_log_url="https://e...content-available-to-author-only...g.hk/alsLog"
  13. location_cd=""
  14. workstation_id=$(hostname -f)
  15. user_id=$USER
  16. script_name=${0##*/}
  17. content="The autohealing script executed."
  18. project_code="EAPAUTO-ANSIBLE-ALS"
  19.  
  20. # Step 2: Extract values from the JSON data
  21. target_ident=$(echo ${json_data} | ${JQ} -r '.target_ident')
  22. tags=$(echo ${json_data} | ${JQ} -r '.tags')
  23. is_recovered=$(echo ${json_data} | ${JQ} -r '.is_recovered')
  24. hash=$(echo ${json_data} | ${JQ} -r '.hash')
  25. echo "hash: ${hash}" > debug.txt
  26.  
  27. if [[ "${is_recovered}" == "false" ]]; then
  28. # Step 3: Extract required tags and store them in variables
  29. autoheal_category=""
  30. autoheal_user=""
  31. event_dc=""
  32. active_dc=$(curl -sk https://${flashcat_server_url}//ha/dc/active)
  33.  
  34. for tag in $(echo "${tags}" | ${JQ} -r .[]); do
  35. if [[ ${tag} == "autoheal_category="* ]]; then
  36. autoheal_category=${tag#"autoheal_category="}
  37. elif [[ ${tag} == "autoheal_user="* ]]; then
  38. autoheal_user=${tag#"autoheal_user="}
  39. elif [[ ${tag} == "environment="* ]]; then
  40. environment=${tag#"environment="}
  41. elif [[ ${tag} == "DC="* ]]; then
  42. event_dc=${tag#"DC="}
  43. echo "event_dc: ${event_dc}" >> debug.txt
  44. fi
  45. done
  46.  
  47. # Flashcat URL
  48. if [[ "${environment}" == "dev" ]]; then
  49. flashcat_server_url="https://e...content-available-to-author-only...g.hk"
  50. als_log_url="https://e...content-available-to-author-only...g.hk/alsLog"
  51. location_cd="TST"
  52. elif [[ "${environment}" == "sit" || "${environment}" == "nonprd" ]]; then
  53. flashcat_server_url="https://e...content-available-to-author-only...g.hk"
  54. als_log_url="https://e...content-available-to-author-only...g.hk/alsLog"
  55. location_cd="TST"
  56. elif [[ "${environment}" == "prd" ]]; then
  57. flashcat_server_url="https://e...content-available-to-author-only...g.hk"
  58. als_log_url="https://e...content-available-to-author-only...g.hk:56797/alsLog"
  59. location_cd="PRD"
  60. fi
  61. echo "flashcat_server_url: ${flashcat_server_url}" >> debug.txt
  62. echo "als_log_url: ${als_log_url}" >> debug.txt
  63. echo "location_cd: ${location_cd}" >> debug.txt
  64.  
  65. # Step 4: Execute the command with the extracted values
  66. if [[ "${active_dc}" == "${event_dc}" ]]; then
  67. if [[ -z "$target_ident" ]]; then
  68. result+="Invalid target_ident "
  69. elif [[ -z "$autoheal_category" ]]; then
  70. result+="Invalid autoheal_category "
  71. elif [[ -z "$autoheal_user" ]]; then
  72. result+="Invalid autoheal_user"
  73. else
  74. cmd="/appl/eap/cfs/cmdb/ansible-playbook/run/operation/auto-healing/auto-healing.sh ${target_ident} ${autoheal_category} ${autoheal_user}"
  75. result=$(eval $cmd)
  76.  
  77. #write log to als
  78. 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\" }");
  79. if [[ $response != *'200'* ]]; then
  80. echo "Failed to write ALS Log: $response";
  81. fi
  82. fi
  83. else
  84. result+="This is the event from passive site: ${event_dc}"
  85. fi
  86.  
  87. echo "Content-type: application/json; charset=utf-8"
  88. echo ""
  89. echo "["
  90. echo "{ \"result\" : \"$result\"}";
  91. echo "]"
  92. fi
  93. #result=$(eval $cmd | grep stdout | egrep -w "is running|already running|ok|ansible_default")
  94.  
  95. #if [[ -z $result ]]; then
  96. # json="Fail"
  97. #else
  98. # json="Pass"
  99. #fi
  100.  
Success #stdin #stdout #stderr 0.02s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
hostname: Temporary failure in name resolution
prog.pl: line 21: /appl/eap/sas/httpd/cgi-bin/jq-1.5: No such file or directory
prog.pl: line 22: /appl/eap/sas/httpd/cgi-bin/jq-1.5: No such file or directory
prog.pl: line 23: /appl/eap/sas/httpd/cgi-bin/jq-1.5: No such file or directory
prog.pl: line 24: /appl/eap/sas/httpd/cgi-bin/jq-1.5: No such file or directory
prog.pl: line 25: debug.txt: Permission denied