diff --git a/.gitignore b/.gitignore index 3794cbe..840f692 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ Setup/Applications/* .vscode/* zz_demoflow* business_transactions/uploaded/* +api_actions/uploaded/*.json diff --git a/api_actions/action-suppression-payload-template.json b/api_actions/action-suppression-payload-template.json new file mode 100755 index 0000000..dee8105 --- /dev/null +++ b/api_actions/action-suppression-payload-template.json @@ -0,0 +1,9 @@ +{ + "name": "_action_suppression_name", + "disableAgentReporting": true, + "startTime": "_action_suppression_start", + "endTime": "_action_suppression_end", + "affects": { + "affectedInfoType": "APPLICATION" + } +} \ No newline at end of file diff --git a/api_actions/actions/tiers_action.json b/api_actions/actions/tiers_action.json new file mode 100644 index 0000000..691559f --- /dev/null +++ b/api_actions/actions/tiers_action.json @@ -0,0 +1,17 @@ +{ + "name": "CMA-suppression-tiers-nodes-test", + "disableAgentReporting": true, + "startTime": "2020-09-06T18:33:14+0000", + "endTime": "2020-09-06T19:33:14+0000", + "affects": { + "affectedInfoType": "TIERS_NODES", + "affectedEntities": { + "tierNodeType": "NODE", + "nodeType": "ALL_NODES", + "affectedNodes": { + "affectedNodeScope": "NODES_OF_SPECIFIC_TIERS", + "specificTiers": ["Web"] + } + } + } +} diff --git a/api_actions/application-action-suppression.sh b/api_actions/application-action-suppression.sh new file mode 100755 index 0000000..fbee9a6 --- /dev/null +++ b/api_actions/application-action-suppression.sh @@ -0,0 +1,68 @@ +#!/bin/bash + +# 1. INPUT PARAMETERS +_controller_url=${1} # hostname + /controller +_user_credentials=${2} # ${username}:${password} + +_action_suppression_start=${3} +_action_suppression_duration=${4} + +_application_name=${5} + +# 2. FUNCTIONS +function func_check_http_response(){ + local http_message_body="$1" + local string_success_response_contains="$2" + if [[ "$http_message_body" =~ "$string_success_response_contains" ]]; then # contains + echo "Success..." + else + echo "${dt} ERROR "{$http_message_body}"" >> error.log + echo "ERROR $http_message_body" + exit 1 + fi +} + +# 3. PREPARE REQUEST +dt=$(date '+%Y-%m-%d_%H-%M-%S') + +_payload_path="./api_actions/uploaded/action-suppression-payload-${dt}.json" +_template_path="./api_actions/action-suppression-payload-template.json" + +_header="Content-Type: application/json; charset=utf8" + +_action_suppression_name="CMA-suppression-${dt}" + + +echo | date -d "today" >/dev/null 2>&1 +if [ $? -eq 0 ]; then + # GNU + _action_suppression_end=$(date -d "${_action_suppression_start} +${_action_suppression_duration} minutes" "+%FT%T+0000") +else + # Mac + _action_suppression_end=$(date -j -v +${_action_suppression_duration}M -f "%Y-%M-%dT%H:%M:%S+0000" "${_action_suppression_start}" "+%FT%T+0000") +fi + +# populate the payload template +sed -e "s/_action_suppression_name/${_action_suppression_name}/g" -e "s/_action_suppression_start/${_action_suppression_start}/g" -e "s/_action_suppression_end/${_action_suppression_end}/g" "${_template_path}" > "${_payload_path}" + +# application id +allApplications=$(curl -s --user ${_user_credentials} ${_controller_url}/rest/applications?output=JSON) + +applicationObject=$(jq --arg appName "$_application_name" '.[] | select(.name == $appName)' <<<$allApplications) + +if [ "$applicationObject" = "" ]; then + func_check_http_status 404 "Application '"$_application_name"' not found. Aborting..." +fi + +application_id=$(jq '.id' <<< $applicationObject) +_resource_url="alerting/rest/v1/applications/${application_id}/action-suppressions" + +# 4. SEND A CREATE REQUEST +echo "Uploading application supression action" +response=$(curl -s -X POST --user $_user_credentials $_controller_url/$_resource_url -H "${_header}" --data "@${_payload_path}" ) + +# 5. CHECK RESULT +expected_response='"id":' # returns id on success +func_check_http_response "\{$response}" $expected_response + +#echo "response is: $response" diff --git a/api_actions/upload-files-action-suppression.sh b/api_actions/upload-files-action-suppression.sh new file mode 100755 index 0000000..73d902b --- /dev/null +++ b/api_actions/upload-files-action-suppression.sh @@ -0,0 +1,59 @@ +#!/bin/bash + +# 1. INPUT PARAMETERS +_controller_url=${1} # hostname + /controller +_user_credentials=${2} # ${username}:${password} + +_application_name=${3} + +# 2. FUNCTIONS +function func_check_http_response(){ + local http_message_body="$1" + local string_success_response_contains="$2" + if [[ "$http_message_body" =~ "$string_success_response_contains" ]]; then # contains + echo "Success..." + else + echo "${dt} ERROR "{$http_message_body}"" >> error.log + echo "ERROR $http_message_body" + #exit 1 + fi +} + +# 3. PREPARE REQUEST +dt=$(date '+%Y-%m-%d_%H-%M-%S') + +_header="Content-Type: application/json; charset=utf8" + + +# application id +allApplications=$(curl -s --user ${_user_credentials} ${_controller_url}/rest/applications?output=JSON) + +applicationObject=$(jq --arg appName "$_application_name" '.[] | select(.name == $appName)' <<<$allApplications) + +if [ "$applicationObject" = "" ]; then + func_check_http_status 404 "Application '"$_application_name"' not found. Aborting..." +fi + +_application_id=$(jq '.id' <<< $applicationObject) +_resource_url="alerting/rest/v1/applications/${_application_id}/action-suppressions" + +_action_suppression_files="./api_actions/actions/*.json" + + +for f in $_action_suppression_files; do + echo "Processing $f action suppression file" + + _payload_path=$f + + # 4. SEND A CREATE REQUEST + response=$(curl -s -X POST --user $_user_credentials $_controller_url/$_resource_url -H "${_header}" --data "@${_payload_path}" ) + + # 5. CHECK RESULT + expected_response='"id":' # returns id on success + func_check_http_response "\{$response}" $expected_response + + # echo "response is: $response" + +done + + diff --git a/config.json b/config.json index 73c9ff2..73cb93f 100644 --- a/config.json +++ b/config.json @@ -30,5 +30,13 @@ "configure_bt": false, "bt_only": false } + ], + "action_suppression": [ + { + "suppress_action": false, + "suppress_start": "", + "suppress_duration": "", + "suppress_upload_files": false + } ] } \ No newline at end of file diff --git a/dashboards/CustomDashboard_noDB_noSIM_vanilla.json b/dashboards/CustomDashboard_noDB_noSIM_vanilla.json index 5492c7e..5643d03 100644 --- a/dashboards/CustomDashboard_noDB_noSIM_vanilla.json +++ b/dashboards/CustomDashboard_noDB_noSIM_vanilla.json @@ -357,7 +357,7 @@ "width": 372, "minHeight": 0, "minWidth": 0, - "x": 133, + "x": 105, "y": 0, "label": null, "description": null, diff --git a/dashboards/CustomDashboard_noDB_vanilla.json b/dashboards/CustomDashboard_noDB_vanilla.json index ebf742d..1253885 100644 --- a/dashboards/CustomDashboard_noDB_vanilla.json +++ b/dashboards/CustomDashboard_noDB_vanilla.json @@ -357,7 +357,7 @@ "width": 372, "minHeight": 0, "minWidth": 0, - "x": 133, + "x": 105, "y": 0, "label": null, "description": null, diff --git a/dashboards/CustomDashboard_noSIM_vanilla.json b/dashboards/CustomDashboard_noSIM_vanilla.json index b47a81b..1214458 100644 --- a/dashboards/CustomDashboard_noSIM_vanilla.json +++ b/dashboards/CustomDashboard_noSIM_vanilla.json @@ -357,7 +357,7 @@ "width": 372, "minHeight": 0, "minWidth": 0, - "x": 133, + "x": 105, "y": 0, "label": null, "description": null, diff --git a/dashboards/CustomDashboard_vanilla.json b/dashboards/CustomDashboard_vanilla.json index 5b308b8..8ade351 100644 --- a/dashboards/CustomDashboard_vanilla.json +++ b/dashboards/CustomDashboard_vanilla.json @@ -357,7 +357,7 @@ "width": 372, "minHeight": 0, "minWidth": 0, - "x": 133, + "x": 105, "y": 0, "label": null, "description": null, diff --git a/start.sh b/start.sh index 0d9c1e4..3cd3aef 100755 --- a/start.sh +++ b/start.sh @@ -53,8 +53,14 @@ _arg_use_branding=true _arg_logo_name= _arg_background_name= +_arg_suppress_action=false +_arg_suppress_start= +_arg_suppress_duration= +_arg_suppress_upload_files=false + _arg_debug=false +_arg_controller_port_explicitly_set=false _arg_use_encoded_credentials_explicitly_set=false _arg_overwrite_health_rules_explicitly_set=false _arg_use_https_explicitly_set=false @@ -65,6 +71,8 @@ _arg_include_sim_explicitly_set=false _arg_configure_bt_explicitly_set=false _arg_use_branding_explicitly_set=false _arg_bt_only_explicitly_set=false +_arg_suppress_action_explicitly_set=false +_arg_suppress_upload_files_explicitly_set=false print_help() { @@ -102,6 +110,12 @@ print_help() printf '\t%s\n' "--overwrite-health-rules, --no-overwrite-health-rules: overwrite health rules (${_arg_overwrite_health_rules} by default)" printf '\t%s\n' "--bt-only, --no-bt-only: Configure business transactions only (${_arg_bt_only} by default)" + printf '%s\n' "Action suppression options:" + printf '\t%s\n' "--suppress-action, --no-suppress-action: use application action suppression (${_arg_suppress_action} by default)" + printf '\t%s\n' "--suppress-start: application suppression start date in \"yyyy-MM-ddThh:mm:ss+0000\" format (GMT), mandatory if suppress-action set to true (current datetime by default)" + printf '\t%s\n' "--suppress-duration: application suppression duration in minutes, mandatory if suppress-action is set to true (one hour by default)" + printf '\t%s\n' "--suppress-upload-files, --no-suppress-upload-files: upload action suppression files from a folder (${_arg_suppress_upload_files} by default)" + printf '%s\n' "Help options:" printf '\t%s\n' "-h, --help: Prints help" printf '\t%s\n' "--debug, --no-debug: Run in debug mode (${_arg_debug} by default)" @@ -141,6 +155,7 @@ parse_commandline() _arg_controller_host="${_key##-c}" ;; -P|--controller-port) + _arg_controller_port_explicitly_set=true test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 _arg_controller_port="$2" shift @@ -282,6 +297,32 @@ parse_commandline() --background-name=*) _arg_background_name="${_key##--background-name=}" ;; + --no-suppress-action|--suppress-action) + _arg_suppress_action=true + _arg_suppress_action_explicitly_set=true + test "${1:0:5}" = "--no-" && _arg_suppress_action=false + ;; + --suppress-start) + test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 + _arg_suppress_start="$2" + shift + ;; + --suppress-start=*) + _arg_suppress_start="${_key##--suppress-start=}" + ;; + --suppress-duration) + test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1 + _arg_suppress_duration="$2" + shift + ;; + --suppress-duration=*) + _arg_suppress_duration="${_key##--suppress-duration=}" + ;; + --no-suppress-upload-files|--suppress-upload-files) + _arg_suppress_upload_files=true + _arg_suppress_upload_files_explicitly_set=true + test "${1:0:5}" = "--no-" && _arg_suppress_upload_files=false + ;; -h|--help) print_help exit 0 @@ -393,7 +434,7 @@ fi if ([ -z "${_arg_controller_host// }" ] && [ ! -z "${CMA_CONTROLLER_HOST// }" ]); then _arg_controller_host=${CMA_CONTROLLER_HOST} fi -if ([ -z "${_arg_controller_port// }" ] && [ ! -z "${CMA_CONTROLLER_PORT// }" ]); then +if ([ $_arg_controller_port_explicitly_set = false ] && [ ! -z "${CMA_CONTROLLER_PORT// }" ]); then _arg_controller_port=${CMA_CONTROLLER_PORT} fi if ([ $_arg_use_https_explicitly_set = false ] && [ ! -z "${CMA_USE_HTTPS// }" ]); then @@ -453,6 +494,21 @@ if ([ $_arg_bt_only_explicitly_set = false ] && [ ! -z "${CMA_BT_ONLY// }" ]); t _arg_bt_only=${CMA_BT_ONLY} fi +# action suppression +if ([ $_arg_suppress_action_explicitly_set = false ] && [ ! -z "${CMA_SUPPRESS_ACTION// }" ]); then + _arg_suppress_action=${CMA_SUPPRESS_ACTION} +fi +if ([ -z "${_arg_suppress_start// }" ] && [ ! -z "${CMA_SUPPRESS_START// }" ]); then + _arg_suppress_start=${CMA_SUPPRESS_START} +fi +if ([ -z "${_arg_suppress_duration// }" ] && [ ! -z "${CMA_SUPPRESS_DURATION// }" ]); then + _arg_suppress_duration=${CMA_SUPPRESS_DURATION} +fi +if ([ $_arg_suppress_upload_files_explicitly_set = false ] && [ ! -z "${CMA_SUPPRESS_UPLOAD_FILES// }" ]); then + _arg_suppress_upload_files=${CMA_SUPPRESS_UPLOAD_FILES} +fi + + # 1.3 If value not set replace with configuration file values conf_file="config.json" @@ -470,7 +526,7 @@ if [[ -z "${_arg_controller_host// }" ]]; then _arg_controller_host=$(jq -r '.controller_details[].host' <${conf_file}) fi -if [[ -z "${_arg_controller_port// }" ]]; then +if ([ $_arg_controller_port_explicitly_set = false ] && [ -z "${CMA_CONTROLLER_PORT}" ]); then _arg_controller_port=$(jq -r '.controller_details[].port' <${conf_file}) fi @@ -531,6 +587,19 @@ if ([[ $_arg_bt_only_explicitly_set = false ]] && [ -z "${CMA_BT_ONLY// }" ]); t _arg_bt_only=$(jq -r '.configuration[].bt_only' <${conf_file}) fi +if ([[ $_arg_suppress_action_explicitly_set = false ]] && [ -z "${CMA_SUPPRESS_ACTION// }" ]); then + _arg_suppress_action=$(jq -r '.action_suppression[].suppress_action' <${conf_file}) +fi +if [[ -z "${_arg_suppress_start// }" ]]; then + _arg_suppress_start=$(jq -r '.action_suppression[].suppress_start' <${conf_file}) +fi +if [[ -z "${_arg_suppress_duration// }" ]]; then + _arg_suppress_duration=$(jq -r '.action_suppression[].suppress_duration' <${conf_file}) +fi +if ([[ $_arg_suppress_upload_files_explicitly_set = false ]] && [ -z "${CMA_SUPPRESS_UPLOAD_FILES// }" ]); then + _arg_suppress_upload_files=$(jq -r '.action_suppression[].suppress_upload_files' <${conf_file}) +fi + ### 2 VALIDATE ### # 2.1 Check if values are in expected ranges @@ -575,7 +644,7 @@ if [ $_arg_debug = true ]; then fi -### 3 PREPARE PARAMETERS AND EXECUTE SCRIPT ### +### 3 PREPARE PARAMETERS ### # 3.1 Prepare user credentials @@ -625,8 +694,34 @@ if [ $_arg_debug = true ]; then echo "Configure BTs: $_arg_configure_bt" fi -# 3.4 Execute ConfigMyApp script +# 3.5 Prepare action supression +if [ $_arg_suppress_action = true ]; then + if [ -z "${_arg_suppress_start// }" ]; then + # set to current datetime if empty + # UTC / GMT + _arg_suppress_start=$(date -u +%FT%T+0000) + fi + if [ -z "${_arg_suppress_duration// }" ]; then + # set to one hour if empty + _arg_suppress_duration=60 + fi +fi + +### 4 ACTION SUPRESSION ### +if [ $_arg_suppress_action = true ]; then + ./api_actions/application-action-suppression.sh "$_arg_controller_url" "$_arg_user_credentials" "$_arg_suppress_start" "$_arg_suppress_duration" "$_arg_application_name" + +fi + +if [ $_arg_suppress_upload_files = true ]; then + ./api_actions/upload-files-action-suppression.sh "$_arg_controller_url" "$_arg_user_credentials" "$_arg_application_name" + exit 0 # only upload files +fi + + +### 5 EXECUTE CMA SCRIPT ### ./configMyApp.sh "$_arg_controller_url" "$_arg_user_credentials" "$_arg_proxy_details" "$_arg_application_name" "$_arg_include_database" "$_arg_database_name" "$_arg_include_sim" "$_arg_configure_bt" "$_arg_overwrite_health_rules" "$_arg_bt_only" "$_arg_use_branding" "$_arg_logo_name" "$_arg_background_name" + # <-- needed, do not delete # ] <-- needed, do not delete \ No newline at end of file