diff --git a/recipes/duplicate_healthrule.sh b/recipes/duplicate_healthrule.sh new file mode 100755 index 0000000..5c9399a --- /dev/null +++ b/recipes/duplicate_healthrule.sh @@ -0,0 +1,218 @@ +#!/usr/bin/env bash + +################################################################################ +# Duplicate health rules in an application with a suffix +# +# NB; +# HR String (-H) is used in either an exact or contains match, based on (-X) value +# Argument (-Z) is a string value +# +################################################################################ + +usage() { + echo " + Usage: + $0 [params] + + Params; + -E [string] The ACT environment name (Mandatory) + -A [number] Application ID you want to run for + -H [string] String for health rule matching + -X [boolean] Exact match or contains (default is exact, true) + -Z [pattern] Suffix string + + Examples; + ./duplicate_healthrule.sh -E prod -A 123 -H "CPU Utilisation" -Z "COPY" + ./duplicate_healthrule.sh -E prod -A 123 -H "^.* Utilisation" -X false -Z "COPY" + " +} + +################################################################################ +# Setup Variables, etc +################################################################################ + +# Read input params +while getopts ":E:A:H:X:Z:" opt "$@"; do + case "${opt}" in + E) + ENVIRONMENT="${OPTARG}" + ;; + A) + APPID="${OPTARG}" + ;; + H) + HRSTRING="${OPTARG}" + ;; + X) + HREXACT="${OPTARG}" + ;; + Z) + SUFFIX="${OPTARG}" + ;; + :) + usage + exit 1 + ;; + *) + echo "Invalid flag ${OPTARG}, exiting..." + exit 1 + ;; + esac +done + +################################################################################ +# Start of functions +################################################################################ + +# Checks +runChecks() { + # If we didnt get an ENVIRONMENT, we cant continue + if [[ -z ${ENVIRONMENT} ]]; then + echo "No -E param passed." + usage + exit 1 + fi + # If we didnt get an APPID, we cant continue + if [[ -z ${APPID} ]]; then + echo "No -A param passed." + usage + exit 1 + fi + # If we didnt get an HRSTRING, we cant continue + if [[ -z ${HRSTRING} ]]; then + echo "No -H param passed." + usage + exit 1 + fi + # If we didnt get an HRSTRING, we cant continue + if [[ -z ${HREXACT} ]]; then + echo "No -X param passed, defaulting to true" + HREXACT=true + fi + # If we didnt get a SUFFIX, we cant continue + if [[ -z ${SUFFIX} ]]; then + echo "No -Z param passed." + usage + exit 1 + fi +} + +# Clear down node related info +resetVars() { + HRID="" + HRNAME="" + HROUTPUT="" + NEWHRNAME="" + HROUTPUTUPDATED="" + NEWHR="" + HRPOSTRESPONSE="" + HRPOSTRESPONSECHECK="" +} + +################################################################################ +# GetData functions +################################################################################ + +# Get health rule data +getHRLIST() { + # Perform a Login to get tokens + echo "----------------------------------------------------------------------------" + echo "Performing login to controller in environment: ${ENVIRONMENT}" + LOGINRESPONSE=$(../act.sh -E ${ENVIRONMENT} controller call -X GET /controller/auth?action=login) + echo " ${LOGINRESPONSE}" + echo "----------------------------------------------------------------------------" + echo "Getting matching HRs" + # Set our URI for getting HRs + HEATHRULEURI="/controller/alerting/rest/v1/applications/${APPID}/health-rules" + # Get list of HR IDs matching HRSTRING + if [[ ${HREXACT} == "true" ]]; then + echo " -H '${HRSTRING}', getting exact matching HR" + HRLIST=$(../act.sh -E ${ENVIRONMENT} controller call -X GET ${HEATHRULEURI} | jq -r --arg PATTERN "${HRSTRING}" '.[] | select(.name == $PATTERN) | "\(.id),\(.name)"') + else + echo " -H '${HRSTRING}', getting regex matching HRs" + HRLIST=$(../act.sh -E ${ENVIRONMENT} controller call -X GET ${HEATHRULEURI} | jq -r --arg PATTERN "${HRSTRING}" '.[] | select(.name | match($PATTERN)) | "\(.id),\(.name)"') + fi + echo "${HRLIST}" + echo "----------------------------------------------------------------------------" +} + +# Duplucate each health rule +duplicateHRData() { + echo "Starting HR duplications" + + # Reset our vars and carry on + resetVars + + OLDIFS=$IFS + IFS=$'\r\n' + + # Iterate through HR list + for HRITEM in $HRLIST; do + HRID=$(echo ${HRITEM} | cut -d "," -f1) + HRNAME=$(echo ${HRITEM} | cut -d "," -f2) + NEWHRNAME="${HRNAME} ${SUFFIX}" + echo "Duplicating ${HRID}, '${HRNAME}' to '${NEWHRNAME}'" + echo "----------------------------------------------------------------------------" + + # GET our HR json + HROUTPUT=$(../act.sh -E ${ENVIRONMENT} controller call -X GET ${HEATHRULEURI}/${HRID}) + # echo "HROUTPUT: ${HROUTPUT}..." + + # Update the name in our HR JSON + HROUTPUTUPDATED=$(echo "${HROUTPUT}" | sed -r -e "s|\"name\":\"${HRNAME}\",|\"name\":\"${NEWHRNAME}\",|") + # echo "HROUTPUTUPDATED: ${HROUTPUTUPDATED}..." + + # De-duple shortNames + NEWHR=$(echo "$HROUTPUTUPDATED" | jq ' + # Deduplicate Critical Criteria + .evalCriterias.criticalCriteria.conditions |= ( + # Group all conditions by the value of their shortName + group_by(.shortName) | + # Keep only the first element (the representative) of each group + map(.[0]) + ) | + + # Deduplicate Warning Criteria + .evalCriterias.warningCriteria.conditions |= ( + # Group all conditions by the value of their shortName + group_by(.shortName) | + # Keep only the first element (the representative) of each group + map(.[0]) + ) + ') + # echo "NEWHR: ${NEWHR}" + + # Import our renamed and de-duped HR JSON + HRPOSTRESPONSE=$(../act.sh -E ${ENVIRONMENT} controller call -X POST -d "${NEWHR}" ${HEATHRULEURI}) + # echo "HRPOSTRESPONSE: ${HRPOSTRESPONSE}..." + HRPOSTRESPONSECHECK=$(echo ${HRPOSTRESPONSE} | grep -E "\"name\":\"${NEWHRNAME}\",") + if [[ ${HRPOSTRESPONSECHECK} ]]; then + echo "" + echo "Health Rule Duplication - Success" + else + echo "" + echo "Health Rule Duplication - Failed" + fi + echo "----------------------------------------------------------------------------" + done + + IFS=$OLDIFS + + echo "----------------------------------------------------------------------------" + echo "Completed HR updates" + echo "----------------------------------------------------------------------------" +} + + +################################################################################ +################################################################################ +# MAIN CODE +################################################################################ +################################################################################ + +# Lets get ready +runChecks + +# Lets go +getHRLIST +duplicateHRData diff --git a/recipes/list_healthrules.sh b/recipes/list_healthrules.sh new file mode 100755 index 0000000..f547ba8 --- /dev/null +++ b/recipes/list_healthrules.sh @@ -0,0 +1,202 @@ +#!/usr/bin/env bash + +################################################################################ +# Duplicate health rules in an application with a suffix +# +# NB; +# HR String (-H) is used in either an exact or contains match, based on (-X) value +# Argument (-Z) is a string value +# +################################################################################ + +usage() { + echo " + Usage: + $0 [params] + + Params; + -E [string] The ACT environment name (Mandatory) + -A [number] Application ID you want to run for + -H [string] String for health rule matching + -X [boolean] Exact match or contains (default is exact, true) + + Examples; + ./list_healthrules.sh -E prod -A 123 -H "CPU Utilisation" + ./list_healthrules.sh -E prod -A 123 -H "^.* Utilisation" -X false + " +} + +################################################################################ +# Setup Variables, etc +################################################################################ + +# Read input params +while getopts ":E:A:H:X:" opt "$@"; do + case "${opt}" in + E) + ENVIRONMENT="${OPTARG}" + ;; + A) + APPID="${OPTARG}" + ;; + H) + HRSTRING="${OPTARG}" + ;; + X) + HREXACT="${OPTARG}" + ;; + :) + usage + exit 1 + ;; + *) + echo "Invalid flag ${OPTARG}, exiting..." + exit 1 + ;; + esac +done + +################################################################################ +# Start of functions +################################################################################ + +# Checks +runChecks() { + # If we didnt get an ENVIRONMENT, we cant continue + if [[ -z ${ENVIRONMENT} ]]; then + echo "No -E param passed." + usage + exit 1 + fi + # If we didnt get an APPID, we cant continue + if [[ -z ${APPID} ]]; then + echo "No -A param passed." + usage + exit 1 + fi + # If we didnt get an HRSTRING, we cant continue + if [[ -z ${HRSTRING} ]]; then + echo "No -H param passed." + usage + exit 1 + fi + # If we didnt get an HREXACT, we cant continue + if [[ -z ${HREXACT} ]]; then + echo "No -X param passed, defaulting to true" + HREXACT=true + fi +} + +# Clear down node related info +resetVars() { + HRID="" + HRNAME="" + HROUTPUT="" +} + +################################################################################ +# GetData functions +################################################################################ + +# Get health rule data +getHRLIST() { + # Perform a Login to get tokens + echo "----------------------------------------------------------------------------" + echo "Performing login to controller in environment: ${ENVIRONMENT}" + LOGINRESPONSE=$(../act.sh -E ${ENVIRONMENT} controller call -X GET /controller/auth?action=login) + echo " ${LOGINRESPONSE}" + echo "----------------------------------------------------------------------------" + echo "Getting matching HRs" + # Set our URI for getting HRs + HEATHRULEURI="/controller/alerting/rest/v1/applications/${APPID}/health-rules" + # Get list of HR IDs matching HRSTRING + if [[ ${HREXACT} == "true" ]]; then + echo " -H '${HRSTRING}', getting exact matching HR" + HRLIST=$(../act.sh -E ${ENVIRONMENT} controller call -X GET ${HEATHRULEURI} | jq -r --arg PATTERN "${HRSTRING}" '.[] | select(.name == $PATTERN) | "\(.id),\(.name)"') + else + echo " -H '${HRSTRING}', getting regex matching HRs" + HRLIST=$(../act.sh -E ${ENVIRONMENT} controller call -X GET ${HEATHRULEURI} | jq -r --arg PATTERN "${HRSTRING}" '.[] | select(.name | match($PATTERN)) | "\(.id),\(.name)"') + fi + echo "${HRLIST}" + echo "----------------------------------------------------------------------------" +} + + +# Duplucate each health rule +outputHRData() { + echo "Starting HR output" + + # Reset our vars and carry on + resetVars + + OLDIFS=$IFS + IFS=$'\r\n' + + # Iterate through HR list + for HRITEM in $HRLIST; do + HRID=$(echo ${HRITEM} | cut -d "," -f1) + HRNAME=$(echo ${HRITEM} | cut -d "," -f2) + echo "Listing ${HRID}, '${HRNAME}'..." + + # GET our HR json + HROUTPUT=$(../act.sh -E ${ENVIRONMENT} controller call -X GET ${HEATHRULEURI}/${HRID}) + # echo "HROUTPUT: ${HROUTPUT}..." + + # List HROUTPUT + echo "$HROUTPUT" | jq ' + . as $parent | + + # Define a reusable filter to extract and format condition data from an array + def extract_conditions: + # 1. Group the conditions by their shortName + group_by(.shortName) | + # 2. Map over the groups and select the first element of each group + map(.[0]) | + # 3. Apply the final formatting (using the single-string format from previous request) + map( + "\(.shortName) - \(.evalDetail.metricEvalDetail.metricEvalDetailType) - \(.evalDetail.metricPath) -- \(.triggerEnabled) - \(.minimumTriggers)" + ); + # Construct the final output object + { + "id-name": ("\(.id) - \(.name)"), + # "id": $parent.id, + # "name": $parent.name, + "criteria": { + "critical": { + "combination": "\($parent.evalCriterias.criticalCriteria.conditionAggregationType) - \($parent.evalCriterias.criticalCriteria.conditionExpression)", + "conditions": ( + $parent.evalCriterias.criticalCriteria.conditions | extract_conditions + ) + }, + "warning": { + "combination": "\($parent.evalCriterias.criticalCriteria.conditionAggregationType) - \($parent.evalCriterias.criticalCriteria.conditionExpression)", + "conditions": ( + $parent.evalCriterias.warningCriteria.conditions | extract_conditions + ) + } + } + } + ' + echo "----------------------------------------------------------------------------" + done + + IFS=$OLDIFS + + echo "----------------------------------------------------------------------------" + echo "Completed HR updates" + echo "----------------------------------------------------------------------------" +} + + +################################################################################ +################################################################################ +# MAIN CODE +################################################################################ +################################################################################ + +# Lets get ready +runChecks + +# Lets go +getHRLIST +outputHRData diff --git a/recipes/update_healthrule_bulk.sh b/recipes/update_healthrule_bulk.sh index 4e5fd24..7d5b1bf 100755 --- a/recipes/update_healthrule_bulk.sh +++ b/recipes/update_healthrule_bulk.sh @@ -21,8 +21,9 @@ usage() { -E [string] The ACT environment name (Mandatory) -A [number] Application ID you want to run for -H [string] String for health rule matching - -X [pattern] Pattern to match in health rule output - -Y [pattern] Value to change pattern to + -X [boolean] Exact match or contains (default is exact, true) + -Y [pattern] Pattern to match in health rule output + -Z [pattern] Value to change pattern to Examples; ./update_healthrule_bulk.sh -E prod -A 123 -H "CPU Utilisation" -X "\"evaluateToTrueOnNoData\":false" -Y "\"evaluateToTrueOnNoData\":true" @@ -35,7 +36,7 @@ usage() { ################################################################################ # Read input params -while getopts ":E:A:H:X:Y:" opt "$@"; do +while getopts ":E:A:H:X:Y:Z:" opt "$@"; do case "${opt}" in E) ENVIRONMENT="${OPTARG}" @@ -47,9 +48,12 @@ while getopts ":E:A:H:X:Y:" opt "$@"; do HRSTRING="${OPTARG}" ;; X) - PATTERNIN="${OPTARG}" + HREXACT="${OPTARG}" ;; Y) + PATTERNIN="${OPTARG}" + ;; + Z) PATTERNOUT="${OPTARG}" ;; :) @@ -88,9 +92,14 @@ runChecks() { usage exit 1 fi + # If we didnt get an HREXACT, we cant continue + if [[ -z ${HREXACT} ]]; then + echo "No -X param passed, defaulting to true" + HREXACT=true + fi # If we didnt get any PATTERNs, we cant continue if [[ -z ${PATTERNIN} || -z ${PATTERNOUT} ]]; then - echo "No -X or -Y param passed." + echo "No -Y or -Z param passed." usage exit 1 fi @@ -98,7 +107,6 @@ runChecks() { # Clear down node related info resetVars() { - HEATHRULEURI="" HROUTPUT="" HROUTPUTCHECK="" HROUTPUTUPDATED="" @@ -117,18 +125,17 @@ getHRLIST() { echo "Performing login to controller in environment: ${ENVIRONMENT}" LOGINRESPONSE=$(../act.sh -E ${ENVIRONMENT} controller call -X GET /controller/auth?action=login) echo " ${LOGINRESPONSE}" - echo "----------------------------------------------------------------------------" echo "Getting list of health rule IDs;" - # If we dont have a HR pattern to match - if [[ -z ${HRSTRING} ]]; then - # Get ALL HRs !!! - echo " No -H param, getting ALL HR" - HRLIST=$(../act.sh -E ${ENVIRONMENT} healthrule list -a ${APPID} | jq -r '.[] | .id') + # Set our URI for getting HRs + HEATHRULEURI="/controller/alerting/rest/v1/applications/${APPID}/health-rules" + # Get list of HR IDs matching HRSTRING + if [[ ${HREXACT} == "true" ]]; then + echo " -H '${HRSTRING}', getting exact matching HR" + HRLIST=$(../act.sh -E ${ENVIRONMENT} controller call -X GET ${HEATHRULEURI} | jq -r --arg PATTERN "${HRSTRING}" '.[] | select(.name == $PATTERN) | "\(.id),\(.name)"') else - echo " -H '${HRSTRING}', getting mathcing HRs" - # Get list if HR IDs matching HRSTRING - HRLIST=$(../act.sh -E ${ENVIRONMENT} healthrule list -a ${APPID} | jq -r --arg PATTERN "${HRSTRING}" '.[] | select(.name | contains($PATTERN)) | .id') + echo " -H '${HRSTRING}', getting regex matching HRs" + HRLIST=$(../act.sh -E ${ENVIRONMENT} controller call -X GET ${HEATHRULEURI} | jq -r --arg PATTERN "${HRSTRING}" '.[] | select(.name | match($PATTERN)) | "\(.id),\(.name)"') fi echo "${HRLIST}" echo "----------------------------------------------------------------------------" @@ -141,25 +148,33 @@ updateHRData() { # Reset our vars and carry on resetVars + OLDIFS=$IFS + IFS=$'\r\n' + # Iterate through HR list - for HRID in $HRLIST; do + for HRITEM in $HRLIST; do + HRID=$(echo ${HRITEM} | cut -d "," -f1) echo "Updating ${HRID}..." - # Set our URI for getting/updating current HR - HEATHRULEURI="/controller/alerting/rest/v1/applications/${APPID}/health-rules/${HRID}" + echo "Changing '${PATTERNIN}' to '${PATTERNOUT}'..." # GET our HR json - HROUTPUT=$(../act.sh -E ${ENVIRONMENT} controller call -X GET ${HEATHRULEURI}) + HROUTPUT=$(../act.sh -E ${ENVIRONMENT} controller call -X GET ${HEATHRULEURI}/${HRID}) + # echo "HROUTPUT: ${HROUTPUT}" # Do we see the PATTERNIN in our HR? - HROUTPUTCHECK=$(echo ${HROUTPUT} | grep "${PATTERNIN}") + HROUTPUTCHECK=$(echo ${HROUTPUT} | grep -E "${PATTERNIN}") + # echo "HROUTPUTCHECK: ${HROUTPUTCHECK}" if [[ -z ${HROUTPUTCHECK} ]]; then echo " No pattern match found in health rule, skipping any changes..." continue else # Update the HR json - HROUTPUTUPDATED=$(echo "${HROUTPUT}" | sed "s|${PATTERNIN}|${PATTERNOUT}|g") + HROUTPUTUPDATED=$(echo "${HROUTPUT}" | sed -r "s|${PATTERNIN}|${PATTERNOUT}|g") + # echo "HROUTPUTUPDATED: ${HROUTPUTUPDATED}" # PUT our HR json back - HRPUTRESPONSE=$(../act.sh -E ${ENVIRONMENT} controller call -X PUT -d "${HROUTPUTUPDATED}" ${HEATHRULEURI}) - HRPUTRESPONSECHECK=$(echo ${HRPUTRESPONSE} | grep "\"id\":${HRID},") + HRPUTRESPONSE=$(../act.sh -E ${ENVIRONMENT} controller call -X PUT -d "${HROUTPUTUPDATED}" ${HEATHRULEURI}/${HRID}) + # echo "HRPUTRESPONSE: ${HRPUTRESPONSE}" + HRPUTRESPONSECHECK=$(echo ${HRPUTRESPONSE} | grep -E "\"id\":${HRID},") + # echo "HRPUTRESPONSECHECK: ${HRPUTRESPONSECHECK}" if [[ ${HRPUTRESPONSECHECK} ]]; then echo " Updated" else @@ -168,6 +183,8 @@ updateHRData() { fi done + IFS=$OLDIFS + echo "----------------------------------------------------------------------------" echo "Completed HR updates" echo "----------------------------------------------------------------------------" diff --git a/recipes/update_healthrule_conditions.sh b/recipes/update_healthrule_conditions.sh new file mode 100755 index 0000000..ece998e --- /dev/null +++ b/recipes/update_healthrule_conditions.sh @@ -0,0 +1,243 @@ +#!/usr/bin/env bash + +################################################################################ +# Duplicate health rules in an application with a suffix +# +# NB; +# HR String (-H) is used in either an exact or contains match, based on (-X) value +# Argument (-Z) is a string value +# +################################################################################ + +usage() { + echo " + Usage: + $0 [params] + + Params; + -E [string] The ACT environment name (Mandatory) + -A [number] Application ID you want to run for + -H [string] String for health rule matching + -X [boolean] Exact match or contains (default is exact, true) + -Y [string] String to use for JQ mapping update to critical conditions (use single quotes) + i.e. remove shortName C & D - 'map(select(.shortName != "C") | select(.shortName != "D"))' + -Z [string] String to use for JQ mapping update to warning conditions (use single quotes) + If left blank will use -Y value + + Examples; + ./update_healthrule_conditions.sh -E prod -A 123 -H "CPU Utilisation" -Y 'map(select(.shortName != "C") | select(.shortName != "D"))' + ./update_healthrule_conditions.sh -E prod -A 123 -H "^.* Utilisation$" -X false -Y 'map(select(.shortName != "C") | select(.shortName != "D"))' + " +} + +################################################################################ +# Setup Variables, etc +################################################################################ + +# Read input params +while getopts ":E:A:H:X:Y:Z:" opt "$@"; do + case "${opt}" in + E) + ENVIRONMENT="${OPTARG}" + ;; + A) + APPID="${OPTARG}" + ;; + H) + HRSTRING="${OPTARG}" + ;; + X) + HREXACT="${OPTARG}" + ;; + Y) + JQSTRINGCRIT="${OPTARG}" + ;; + Z) + JQSTRINGWARN="${OPTARG}" + ;; + :) + usage + exit 1 + ;; + *) + echo "Invalid flag ${OPTARG}, exiting..." + exit 1 + ;; + esac +done + +################################################################################ +# Start of functions +################################################################################ + +# Checks +runChecks() { + # If we didnt get an ENVIRONMENT, we cant continue + if [[ -z ${ENVIRONMENT} ]]; then + echo "No -E param passed." + usage + exit 1 + fi + # If we didnt get an APPID, we cant continue + if [[ -z ${APPID} ]]; then + echo "No -A param passed." + usage + exit 1 + fi + # If we didnt get an HRSTRING, we cant continue + if [[ -z ${HRSTRING} ]]; then + echo "No -H param passed." + usage + exit 1 + fi + # If we didnt get an HRSTRING, we cant continue + if [[ -z ${HREXACT} ]]; then + echo "No -X param passed, defaulting to true" + HREXACT=true + fi + # If we didnt get an JQSTRINGCRIT, we cant continue + if [[ -z ${JQSTRINGCRIT} ]]; then + echo "No -Y param passed." + usage + exit 1 + elif [[ -z ${JQSTRINGWARN} ]]; then + echo "No -Z param passed, using -Y value." + JQSTRINGWARN=${JQSTRINGCRIT} + fi +} + +# Clear down node related info +resetVars() { + HRID="" + HRNAME="" + HROUTPUT="" + NEWHR="" + HRPUTRESPONSE="" + HRPUTRESPONSECHECK="" +} + +################################################################################ +# GetData functions +################################################################################ + +# Get health rule data +getHRLIST() { + # Perform a Login to get tokens + echo "----------------------------------------------------------------------------" + echo "Performing login to controller in environment: ${ENVIRONMENT}" + LOGINRESPONSE=$(../act.sh -E ${ENVIRONMENT} controller call -X GET /controller/auth?action=login) + echo " ${LOGINRESPONSE}" + + echo "----------------------------------------------------------------------------" + echo "Getting matching HRs" + # Set our URI for getting HRs + HEATHRULEURI="/controller/alerting/rest/v1/applications/${APPID}/health-rules" + # Get list of HR IDs matching HRSTRING + if [[ ${HREXACT} == "true" ]]; then + echo " -H '${HRSTRING}', getting exact matching HR" + HRLIST=$(../act.sh -E ${ENVIRONMENT} controller call -X GET ${HEATHRULEURI} | jq -r --arg PATTERN "${HRSTRING}" '.[] | select(.name == $PATTERN) | "\(.id),\(.name)"') + else + echo " -H '${HRSTRING}', getting regex matching HRs" + HRLIST=$(../act.sh -E ${ENVIRONMENT} controller call -X GET ${HEATHRULEURI} | jq -r --arg PATTERN "${HRSTRING}" '.[] | select(.name | match($PATTERN)) | "\(.id),\(.name)"') + fi + echo "${HRLIST}" + echo "----------------------------------------------------------------------------" +} + +# Duplucate each health rule +updateHRData() { + echo "Starting HR duplications" + + # Reset our vars and carry on + resetVars + + OLDIFS=$IFS + IFS=$'\r\n' + + # Iterate through HR list + for HRITEM in $HRLIST; do + HRID=$(echo ${HRITEM} | cut -d "," -f1) + HRNAME=$(echo ${HRITEM} | cut -d "," -f2) + echo "Updating ${HRID}, '${HRNAME}'..." + echo "----------------------------------------------------------------------------" + + # GET our HR json + HROUTPUT=$(../act.sh -E ${ENVIRONMENT} controller call -X GET ${HEATHRULEURI}/${HRID}) + # echo "HROUTPUT: ${HROUTPUT}..." + echo "----------------------------------------------------------------------------" + + # Update our JQSTRINGs with de-dupe logic on shortName + JQSTRINGCRIT="${JQSTRINGCRIT} | group_by(.shortName) | map(.[0])" + JQSTRINGWARN="${JQSTRINGWARN} | group_by(.shortName) | map(.[0])" + + # Update HROUTPUT + NEWHR=$(echo "$HROUTPUT" | jq ' + # 1. Update Critical Criteria + .evalCriterias.criticalCriteria as $critical | + ( + $critical.conditions | + '$JQSTRINGCRIT' + # map(select(.evalDetail.metricPath != "Errors per Minute") | select(.shortName != "E")) + # map(select(.evalDetail.metricPath != "Average Response Time (ms)") | select(.shortName != "B")) + + ) as $new_critical_conditions | + ( + # Update the conditions array + .evalCriterias.criticalCriteria.conditions = $new_critical_conditions | + # Update the conditionExpression string: join the shortNames + .evalCriterias.criticalCriteria.conditionExpression = ($new_critical_conditions | map(.shortName) | join(" AND ")) + ) + + # 2. Update Warning Criteria + | .evalCriterias.warningCriteria as $warning | + ( + $warning.conditions | + '$JQSTRINGWARN' + # map(select(.evalDetail.metricPath != "Errors per Minute") | select(.shortName != "E")) + # map(select(.evalDetail.metricPath != "Average Response Time (ms)") | select(.shortName != "B")) + + ) as $new_warning_conditions | + ( + # Update the conditions array + .evalCriterias.warningCriteria.conditions = $new_warning_conditions | + # Update the conditionExpression string: join the shortNames + .evalCriterias.warningCriteria.conditionExpression = ($new_warning_conditions | map(.shortName) | join(" AND ")) + ) + ') + # echo "NEWHR: ${NEWHR}" + + # Update our HR JSON + HRPUTRESPONSE=$(../act.sh -E ${ENVIRONMENT} controller call -X PUT -d "${NEWHR}" ${HEATHRULEURI}/${HRID}) + # echo "HRPUTRESPONSE: ${HRPUTRESPONSE}..." + + HRPUTRESPONSECHECK=$(echo ${HRPUTRESPONSE} | grep -E "\"name\":\"${HRNAME}\",") + if [[ ${HRPUTRESPONSECHECK} ]]; then + echo "" + echo "Health Rule Update - Success" + else + echo "" + echo "Health Rule Update - Failed" + fi + echo "----------------------------------------------------------------------------" + done + + IFS=$OLDIFS + + echo "----------------------------------------------------------------------------" + echo "Completed HR updates" + echo "----------------------------------------------------------------------------" +} + + +################################################################################ +################################################################################ +# MAIN CODE +################################################################################ +################################################################################ + +# Lets get ready +runChecks + +# Lets go +getHRLIST +updateHRData