From 97a443f9b10a4a434ddb01fafce5dbdf40ba5d07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lex=20Ruiz?= Date: Wed, 27 Dec 2023 14:48:58 +0100 Subject: [PATCH 1/3] Add template and settings to disable replicas on ISM plugin internal indices --- distribution/src/bin/indexer-ism-init.sh | 86 ++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 7 deletions(-) diff --git a/distribution/src/bin/indexer-ism-init.sh b/distribution/src/bin/indexer-ism-init.sh index 4217979624bc7..623138d6a726c 100644 --- a/distribution/src/bin/indexer-ism-init.sh +++ b/distribution/src/bin/indexer-ism-init.sh @@ -80,6 +80,52 @@ function generate_rollover_template() { EOF } +######################################################################### +# Creates an index template with order 3 to set the rollover alias. +# Arguments: +# - The alias name, a string. Also used as index pattern. +# Returns: +# The index template as a JSON string. +######################################################################### +function generate_ism_config_template() { + cat <<-EOF + { + "order": 1, + "index_patterns": [ + ".opendistro-ism-managed-index-history-*", + ".opendistro-ism-config", + ".opendistro-job-scheduler-lock" + ], + "settings": { + "number_of_replicas": 0 + } + } + EOF +} + +######################################################################### +# Creates an index template with order 3 to set the rollover alias. +# Arguments: +# - The alias name, a string. Also used as index pattern. +# Returns: +# The index template as a JSON string. +######################################################################### +function generate_ism_config() { + cat <<-EOF + { + "persistent": { + "plugins": { + "index_state_management": { + "history": { + "number_of_replicas": "0" + } + } + } + } + } + EOF +} + ######################################################################### # Loads the index templates for the rollover policy to the indexer. ######################################################################### @@ -89,18 +135,44 @@ function load_templates() { echo "Will create 'wazuh' index template" if [ -f $wazuh_template_path ]; then cat $wazuh_template_path | + if ! curl -s -k ${C_AUTH} \ + -X PUT "${INDEXER_URL}/_template/wazuh" \ + -o "${LOG_FILE}" --create-dirs \ + -H 'Content-Type: application/json' -d @-; then + echo " ERROR: 'wazuh' template creation failed" + exit 1 + else + echo " SUCC: 'wazuh' template created or updated" + fi + else + echo " ERROR: $wazuh_template_path not found" + fi + + Load template for ISM configuration indices + echo "Will create 'ism_history_indices' index template" + generate_ism_config_template | if ! curl -s -k ${C_AUTH} \ - -X PUT "${INDEXER_URL}/_template/wazuh" \ + -X PUT "${INDEXER_URL}/_template/ism_history_indices" \ -o "${LOG_FILE}" --create-dirs \ -H 'Content-Type: application/json' -d @-; then - echo " ERROR: 'wazuh' template creation failed" - exit 1 + echo " ERROR: 'ism_history_indices' template creation failed" + return 1 else - echo " SUCC: 'wazuh' template created or updated" + echo " SUCC: 'ism_history_indices' template created or updated" + fi + + # Make settings persistent + echo "Will disable replicas for 'plugins.index_state_management.history' indices" + generate_ism_config | + if ! curl -s -k ${C_AUTH} \ + -X PUT "${INDEXER_URL}/_cluster/settings" \ + -o "${LOG_FILE}" --create-dirs \ + -H 'Content-Type: application/json' -d @-; then + echo " ERROR: cluster's settings update failed" + return 1 + else + echo " SUCC: cluster's settings saved" fi - else - echo " ERROR: $wazuh_template_path not found" - fi echo "Will create index templates to configure the alias" for alias in "${aliases[@]}"; do From 14a1cb8ab3354d220ff1a60c248c82e464e1f552 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lex=20Ruiz?= Date: Wed, 27 Dec 2023 14:54:19 +0100 Subject: [PATCH 2/3] Fix documentation Replaces exit 1 statements with return 1 --- distribution/src/bin/indexer-ism-init.sh | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/distribution/src/bin/indexer-ism-init.sh b/distribution/src/bin/indexer-ism-init.sh index 623138d6a726c..88253b1050d41 100644 --- a/distribution/src/bin/indexer-ism-init.sh +++ b/distribution/src/bin/indexer-ism-init.sh @@ -81,9 +81,7 @@ function generate_rollover_template() { } ######################################################################### -# Creates an index template with order 3 to set the rollover alias. -# Arguments: -# - The alias name, a string. Also used as index pattern. +# Creates an index template to disable replicas on ISM configurastion indices. # Returns: # The index template as a JSON string. ######################################################################### @@ -104,11 +102,9 @@ function generate_ism_config_template() { } ######################################################################### -# Creates an index template with order 3 to set the rollover alias. -# Arguments: -# - The alias name, a string. Also used as index pattern. +# Creates persistent cluster's settings to disable replicas for ISM history. # Returns: -# The index template as a JSON string. +# The setting as a JSON string. ######################################################################### function generate_ism_config() { cat <<-EOF @@ -140,7 +136,7 @@ function load_templates() { -o "${LOG_FILE}" --create-dirs \ -H 'Content-Type: application/json' -d @-; then echo " ERROR: 'wazuh' template creation failed" - exit 1 + return 1 else echo " SUCC: 'wazuh' template created or updated" fi @@ -273,7 +269,7 @@ function create_write_index() { -H 'Content-Type: application/json' \ -d "$(generate_write_index_alias "${1}")"; then echo " ERROR: creating '${1}' write index" - exit 1 + return 1 else echo " SUCC: '${1}' write index created" fi @@ -335,7 +331,7 @@ function show_help() { echo -e " -v, --verbose" echo -e " Set verbose mode. Prints more information." echo -e "" - exit 1 + return 1 } ######################################################################### @@ -425,7 +421,7 @@ function main() { echo "SUCC: Indexer ISM initialization finished successfully." else echo "ERROR: Indexer ISM initialization failed. Check ${LOG_FILE} for more information." - exit 1 + return 1 fi } From 6058368079bc5c1781395407af5edcffdd05c020 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lex=20Ruiz?= Date: Wed, 27 Dec 2023 14:56:48 +0100 Subject: [PATCH 3/3] Fix uncommented comment line --- distribution/src/bin/indexer-ism-init.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/distribution/src/bin/indexer-ism-init.sh b/distribution/src/bin/indexer-ism-init.sh index 88253b1050d41..b30531cb0a713 100644 --- a/distribution/src/bin/indexer-ism-init.sh +++ b/distribution/src/bin/indexer-ism-init.sh @@ -144,7 +144,7 @@ function load_templates() { echo " ERROR: $wazuh_template_path not found" fi - Load template for ISM configuration indices + # Load template for ISM configuration indices echo "Will create 'ism_history_indices' index template" generate_ism_config_template | if ! curl -s -k ${C_AUTH} \