From 623e3b1e292fc47931d0f953fd62daeba19f0ce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lex=20Ruiz?= Date: Wed, 27 Dec 2023 16:09:42 +0100 Subject: [PATCH] Fix yellow cluster state (#95) * Add template and settings to disable replicas on ISM plugin internal indices * Fix documentation Replaces exit 1 statements with return 1 * Fix uncommented comment line --- distribution/src/bin/indexer-ism-init.sh | 76 ++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 4 deletions(-) diff --git a/distribution/src/bin/indexer-ism-init.sh b/distribution/src/bin/indexer-ism-init.sh index 6051ab95788ae..3e9e4a3f278f3 100644 --- a/distribution/src/bin/indexer-ism-init.sh +++ b/distribution/src/bin/indexer-ism-init.sh @@ -82,6 +82,48 @@ function generate_rollover_template() { EOF } +######################################################################### +# Creates an index template to disable replicas on ISM configurastion indices. +# 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 persistent cluster's settings to disable replicas for ISM history. +# Returns: +# The setting 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. ######################################################################### @@ -95,7 +137,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 @@ -103,6 +145,32 @@ function load_templates() { echo " ERROR: ${ALERTS_TEMPLATE} 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/ism_history_indices" \ + -o "${LOG_FILE}" --create-dirs \ + -H 'Content-Type: application/json' -d @-; then + echo " ERROR: 'ism_history_indices' template creation failed" + return 1 + else + 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 + echo "Will create index templates to configure the alias" for alias in "${aliases[@]}"; do generate_rollover_template "${alias}" | @@ -202,7 +270,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 @@ -264,7 +332,7 @@ function show_help() { echo -e " -v, --verbose" echo -e " Set verbose mode. Prints more information." echo -e "" - exit 1 + return 1 } ######################################################################### @@ -363,7 +431,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 }