Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexRuiz7 committed Nov 8, 2023
1 parent e73816c commit 88f417a
Showing 1 changed file with 41 additions and 18 deletions.
59 changes: 41 additions & 18 deletions scripts/indexer-ism-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,15 @@ function generate_rollover_template() {
function load_templates() {
# Note: the wazuh-template.json could also be loaded here.
for alias in "${aliases[@]}"; do
echo "TEMPLATES AND POLICIES - Uploading ${alias} template"
generate_rollover_template "${alias}" | curl -s -k ${C_AUTH} \
-X PUT "${INDEXER_URL}/_template/${alias}-rollover" -o /dev/null \
-H 'Content-Type: application/json' -d @-
generate_rollover_template "${alias}" |
if ! curl -s -k ${C_AUTH} \
-X PUT "${INDEXER_URL}/_template/${alias}-rollover" -o /dev/null \
-H 'Content-Type: application/json' -d @-; then
echo "Error uploading ${alias} template"
return 1
else
echo "${alias} template uploaded"
fi
done
}

Expand All @@ -110,15 +115,21 @@ function upload_rollover_policy() {

# Check if the ${POLICY_NAME} ISM policy was loaded (404 error if not found)
if [[ "${policy_exists}" == "404" ]]; then
echo "TEMPLATES AND POLICIES - Uploading ${POLICY_NAME} ISM policy"
curl -s -k ${C_AUTH} -o /dev/null \
if ! curl -s -k ${C_AUTH} -o /dev/null \
-X PUT "${INDEXER_URL}/_plugins/_ism/policies/${POLICY_NAME}" \
-H 'Content-Type: application/json' -d "$(generate_rollover_policy)"
-H 'Content-Type: application/json' \
-d "$(generate_rollover_policy)"; then
echo "Error uploading ${POLICY_NAME} policy"
return 1
else
echo "${POLICY_NAME} policy uploaded"
fi
else
if [[ "${policy_exists}" == "200" ]]; then
echo "TEMPLATES AND POLICIES - ${POLICY_NAME} policy already exists"
echo "${POLICY_NAME} policy already exists"
else
echo "TEMPLATES AND POLICIES - Error uploading ${POLICY_NAME} policy"
echo "Error checking if ${POLICY_NAME} exists"
return 1
fi
fi
}
Expand Down Expand Up @@ -158,9 +169,15 @@ function generate_write_index_alias() {
# 1. The alias. String.
#########################################################################
function create_write_index() {
curl -s -k ${C_AUTH} -o /dev/null \
if ! curl -s -k ${C_AUTH} -o /dev/null \
-X PUT "$INDEXER_URL/%3C${1}-4.x-%7Bnow%2Fd%7D-000001%3E?pretty" \
-H 'Content-Type: application/json' -d "$(generate_write_index_alias "${1}")"
-H 'Content-Type: application/json' \
-d "$(generate_write_index_alias "${1}")"; then
echo "Error creating ${1} write index"
exit 1
else
echo "${1} write index created"
fi
}

#########################################################################
Expand All @@ -169,7 +186,6 @@ function create_write_index() {
# 1. List of aliases to initialize.
#########################################################################
function create_indices() {
echo "TEMPLATES AND POLICIES - Creating write indices"
for alias in "${aliases[@]}"; do
# Check if there are any write indices for the current alias
write_index_exists=$(check_for_write_index "${alias}")
Expand All @@ -181,7 +197,6 @@ function create_indices() {
done
}


#########################################################################
# Shows usage help.
#########################################################################
Expand Down Expand Up @@ -215,6 +230,9 @@ function show_help() {
echo -e " -s, --min-shard-size <shard-size>"
echo -e " Set the minimum shard size in GB. By default 25."
echo -e ""
echo -e " -v, --verbose"
echo -e " Set verbose mode. Prints more information."
echo -e ""
exit 1
}

Expand Down Expand Up @@ -287,6 +305,10 @@ function main() {
shift 2
fi
;;
"-v" | "--verbose")
set -x
shift
;;
*)
echo "Unknow option: ${1}"
show_help
Expand All @@ -295,13 +317,14 @@ function main() {
done

# Load the Wazuh Indexer templates
load_templates

# Upload the rollover policy
upload_rollover_policy

# Create the initial write indices
create_indices "${aliases[@]}"
if load_templates && upload_rollover_policy && create_indices "${aliases[@]}"; then
echo "Indexer ISM initialization finished successfully"
else
echo "Indexer ISM initialization failed"
exit 1
fi
}

main "$@"

0 comments on commit 88f417a

Please sign in to comment.