Skip to content

Commit

Permalink
Fix bug with -i option (#51)
Browse files Browse the repository at this point in the history
* Fix bug with -i option

* Improve error handling
  • Loading branch information
AlexRuiz7 committed Apr 24, 2024
1 parent b72ca7f commit a12ff9d
Showing 1 changed file with 54 additions and 18 deletions.
72 changes: 54 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 @@ -209,9 +224,15 @@ function show_help() {
echo -e " -p, --indexer-password <password>"
echo -e " Specifies the Wazuh indexer admin user password."
echo -e ""
echo -e " -P, --priority <priority>"
echo -e " Specifies the policy's priority."
echo -e ""
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 @@ -252,6 +273,7 @@ function main() {
show_help
else
INDEXER_HOSTNAME="${2}"
INDEXER_URL="https://${INDEXER_HOSTNAME}:9200"
shift 2
fi
;;
Expand All @@ -274,6 +296,19 @@ function main() {
shift 2
fi
;;
"-P" | "--priority")
if [ -z "${2}" ]; then
echo "Error on arguments. Probably missing <priority> after -P|--priority"
show_help
else
ISM_PRIORITY="${2}"
shift 2
fi
;;
"-v" | "--verbose")
set -x
shift
;;
*)
echo "Unknow option: ${1}"
show_help
Expand All @@ -282,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 a12ff9d

Please sign in to comment.