Skip to content

Commit 845f66c

Browse files
committed
Improve ISM init script (#57)
* Improve ISM init script * Change log file path
1 parent 4650ed8 commit 845f66c

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

scripts/indexer-ism-init.sh

+35-22
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ INDEXER_PASSWORD="admin"
1212
INDEXER_HOSTNAME="localhost"
1313

1414
POLICY_NAME="rollover_policy"
15+
LOG_FILE="/tmp/wazuh-indexer/ism-init.log"
1516

1617
INDEXER_URL="https://${INDEXER_HOSTNAME}:9200"
1718

@@ -84,15 +85,17 @@ function generate_rollover_template() {
8485
#########################################################################
8586
function load_templates() {
8687
# Note: the wazuh-template.json could also be loaded here.
88+
echo "Will create index templates to configure the alias"
8789
for alias in "${aliases[@]}"; do
8890
generate_rollover_template "${alias}" |
8991
if ! curl -s -k ${C_AUTH} \
90-
-X PUT "${INDEXER_URL}/_template/${alias}-rollover" -o /dev/null \
92+
-X PUT "${INDEXER_URL}/_template/${alias}-rollover" \
93+
-o "${LOG_FILE}" --create-dirs \
9194
-H 'Content-Type: application/json' -d @-; then
92-
echo "Error uploading ${alias} template"
95+
echo " ERROR: '${alias}' template creation failed"
9396
return 1
9497
else
95-
echo "${alias} template uploaded"
98+
echo " SUCC: '${alias}' template created or updated"
9699
fi
97100
done
98101
}
@@ -106,29 +109,36 @@ function load_templates() {
106109
# None.
107110
#########################################################################
108111
function upload_rollover_policy() {
112+
echo "Will create the '${POLICY_NAME}' policy"
109113
policy_exists=$(
110114
curl -s -k ${C_AUTH} \
111115
-X GET "${INDEXER_URL}/_plugins/_ism/policies/${POLICY_NAME}" \
112-
-o /dev/null \
116+
-o "${LOG_FILE}" --create-dirs \
113117
-w "%{http_code}"
114118
)
115119

116120
# Check if the ${POLICY_NAME} ISM policy was loaded (404 error if not found)
117121
if [[ "${policy_exists}" == "404" ]]; then
118-
if ! curl -s -k ${C_AUTH} -o /dev/null \
119-
-X PUT "${INDEXER_URL}/_plugins/_ism/policies/${POLICY_NAME}" \
120-
-H 'Content-Type: application/json' \
121-
-d "$(generate_rollover_policy)"; then
122-
echo "Error uploading ${POLICY_NAME} policy"
123-
return 1
122+
policy_uploaded=$(
123+
curl -s -k ${C_AUTH} \
124+
-X PUT "${INDEXER_URL}/_plugins/_ism/policies/${POLICY_NAME}" \
125+
-o "${LOG_FILE}" --create-dirs \
126+
-H 'Content-Type: application/json' \
127+
-d "$(generate_rollover_policy)" \
128+
-w "%{http_code}"
129+
)
130+
131+
if [[ "${policy_uploaded}" == "201" ]]; then
132+
echo " SUCC: '${POLICY_NAME}' policy created"
124133
else
125-
echo "${POLICY_NAME} policy uploaded"
134+
echo " ERROR: '${POLICY_NAME}' policy not created => ${policy_uploaded}"
135+
return 1
126136
fi
127137
else
128138
if [[ "${policy_exists}" == "200" ]]; then
129-
echo "${POLICY_NAME} policy already exists"
139+
echo " INFO: policy '${POLICY_NAME}' already exists. Skipping policy creation"
130140
else
131-
echo "Error checking if ${POLICY_NAME} exists"
141+
echo " ERROR: could not check if the policy '${POLICY_NAME}' exists => ${policy_exists}"
132142
return 1
133143
fi
134144
fi
@@ -155,9 +165,9 @@ function generate_write_index_alias() {
155165
cat <<-EOF
156166
{
157167
"aliases": {
158-
"$1": {
159-
"is_write_index": true
160-
}
168+
"$1": {
169+
"is_write_index": true
170+
}
161171
}
162172
}
163173
EOF
@@ -169,14 +179,14 @@ function generate_write_index_alias() {
169179
# 1. The alias. String.
170180
#########################################################################
171181
function create_write_index() {
172-
if ! curl -s -k ${C_AUTH} -o /dev/null \
173-
-X PUT "$INDEXER_URL/%3C${1}-4.x-%7Bnow%2Fd%7D-000001%3E?pretty" \
182+
if ! curl -s -k ${C_AUTH} -o "${LOG_FILE}" --create-dirs \
183+
-X PUT "$INDEXER_URL/%3C${1}-4.x-%7Bnow%2Fd%7D-000001%3E" \
174184
-H 'Content-Type: application/json' \
175185
-d "$(generate_write_index_alias "${1}")"; then
176-
echo "Error creating ${1} write index"
186+
echo " ERROR: creating '${1}' write index"
177187
exit 1
178188
else
179-
echo "${1} write index created"
189+
echo " SUCC: '${1}' write index created"
180190
fi
181191
}
182192

@@ -186,13 +196,16 @@ function create_write_index() {
186196
# 1. List of aliases to initialize.
187197
#########################################################################
188198
function create_indices() {
199+
echo "Will create initial indices for the aliases"
189200
for alias in "${aliases[@]}"; do
190201
# Check if there are any write indices for the current alias
191202
write_index_exists=$(check_for_write_index "${alias}")
192203

193204
# Create the write index if it does not exist
194205
if [[ -z $write_index_exists ]]; then
195206
create_write_index "${alias}"
207+
else
208+
echo " INFO: '${alias}' write index already exists. Skipping write index creation"
196209
fi
197210
done
198211
}
@@ -320,9 +333,9 @@ function main() {
320333
# Upload the rollover policy
321334
# Create the initial write indices
322335
if load_templates && upload_rollover_policy && create_indices "${aliases[@]}"; then
323-
echo "Indexer ISM initialization finished successfully"
336+
echo "SUCC: Indexer ISM initialization finished successfully."
324337
else
325-
echo "Indexer ISM initialization failed"
338+
echo "ERROR: Indexer ISM initialization failed. Check ${LOG_FILE} for more information."
326339
exit 1
327340
fi
328341
}

0 commit comments

Comments
 (0)