@@ -12,6 +12,7 @@ INDEXER_PASSWORD="admin"
12
12
INDEXER_HOSTNAME=" localhost"
13
13
14
14
POLICY_NAME=" rollover_policy"
15
+ LOG_FILE=" /tmp/wazuh-indexer/ism-init.log"
15
16
16
17
INDEXER_URL=" https://${INDEXER_HOSTNAME} :9200"
17
18
@@ -84,15 +85,17 @@ function generate_rollover_template() {
84
85
# ########################################################################
85
86
function load_templates() {
86
87
# Note: the wazuh-template.json could also be loaded here.
88
+ echo " Will create index templates to configure the alias"
87
89
for alias in " ${aliases[@]} " ; do
88
90
generate_rollover_template " ${alias} " |
89
91
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 \
91
94
-H ' Content-Type: application/json' -d @-; then
92
- echo " Error uploading ${alias} template"
95
+ echo " ERROR: ' ${alias} ' template creation failed "
93
96
return 1
94
97
else
95
- echo " ${alias} template uploaded "
98
+ echo " SUCC: ' ${alias} ' template created or updated "
96
99
fi
97
100
done
98
101
}
@@ -106,29 +109,36 @@ function load_templates() {
106
109
# None.
107
110
# ########################################################################
108
111
function upload_rollover_policy() {
112
+ echo " Will create the '${POLICY_NAME} ' policy"
109
113
policy_exists=$(
110
114
curl -s -k ${C_AUTH} \
111
115
-X GET " ${INDEXER_URL} /_plugins/_ism/policies/${POLICY_NAME} " \
112
- -o /dev/null \
116
+ -o " ${LOG_FILE} " --create-dirs \
113
117
-w " %{http_code}"
114
118
)
115
119
116
120
# Check if the ${POLICY_NAME} ISM policy was loaded (404 error if not found)
117
121
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"
124
133
else
125
- echo " ${POLICY_NAME} policy uploaded"
134
+ echo " ERROR: '${POLICY_NAME} ' policy not created => ${policy_uploaded} "
135
+ return 1
126
136
fi
127
137
else
128
138
if [[ " ${policy_exists} " == " 200" ]]; then
129
- echo " ${POLICY_NAME} policy already exists"
139
+ echo " INFO: policy ' ${POLICY_NAME} ' already exists. Skipping policy creation "
130
140
else
131
- echo " Error checking if ${POLICY_NAME} exists"
141
+ echo " ERROR: could not check if the policy ' ${POLICY_NAME} ' exists => ${policy_exists} "
132
142
return 1
133
143
fi
134
144
fi
@@ -155,9 +165,9 @@ function generate_write_index_alias() {
155
165
cat << -EOF
156
166
{
157
167
"aliases": {
158
- "$1 ": {
159
- "is_write_index": true
160
- }
168
+ "$1 ": {
169
+ "is_write_index": true
170
+ }
161
171
}
162
172
}
163
173
EOF
@@ -169,14 +179,14 @@ function generate_write_index_alias() {
169
179
# 1. The alias. String.
170
180
# ########################################################################
171
181
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" \
174
184
-H ' Content-Type: application/json' \
175
185
-d " $( generate_write_index_alias " ${1} " ) " ; then
176
- echo " Error creating ${1} write index"
186
+ echo " ERROR: creating ' ${1} ' write index"
177
187
exit 1
178
188
else
179
- echo " ${1} write index created"
189
+ echo " SUCC: ' ${1} ' write index created"
180
190
fi
181
191
}
182
192
@@ -186,13 +196,16 @@ function create_write_index() {
186
196
# 1. List of aliases to initialize.
187
197
# ########################################################################
188
198
function create_indices() {
199
+ echo " Will create initial indices for the aliases"
189
200
for alias in " ${aliases[@]} " ; do
190
201
# Check if there are any write indices for the current alias
191
202
write_index_exists=$( check_for_write_index " ${alias} " )
192
203
193
204
# Create the write index if it does not exist
194
205
if [[ -z $write_index_exists ]]; then
195
206
create_write_index " ${alias} "
207
+ else
208
+ echo " INFO: '${alias} ' write index already exists. Skipping write index creation"
196
209
fi
197
210
done
198
211
}
@@ -320,9 +333,9 @@ function main() {
320
333
# Upload the rollover policy
321
334
# Create the initial write indices
322
335
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. "
324
337
else
325
- echo " Indexer ISM initialization failed"
338
+ echo " ERROR: Indexer ISM initialization failed. Check ${LOG_FILE} for more information. "
326
339
exit 1
327
340
fi
328
341
}
0 commit comments