-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanup_elastic_indexes.sh
142 lines (124 loc) · 4.63 KB
/
cleanup_elastic_indexes.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/bash
########## Configurationsection ##########
# In case of an error, who should receive the email
MAILTO="PROVIDE EMAILADDRESSE"
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# Turn on debug informations
DEBUG=0
DIR_INDEX="/tmp"
LOCK_DIR="/var/run"
FILE_TMP="${DIR_INDEX}/manage_elastic_tmp"
FILE_EXE="${DIR_INDEX}/managelastic"
FILE_INDEX="${DIR_INDEX}/indexes"
ALL_INDEX="${DIR_INDEX}/all_indexes"
ERROR_FILE="/tmp/elasticerror"
# Put Here your Elasticserver
ELASTICSERVER="PUT ELASTICSERVER HERE"
ELASTICPORT="PUT ELASTICSERVERPORT HERE"
#Put here all your indices which need to be ignored by this script, seperated by |
IGNORE_INDICES="\.[a-zA-Z]"
# How many days an index should remain open before closing it
CLOSE_AFTER_DAYS=$(($(date -d "4 days ago" +%s) * 1000))
# How man days an index should remain close before it will get deleted
DELETE_AFTER_DAYS=$(($(date -d "7 days ago" +%s) * 1000))
########### End of Configuration ###########
> ${FILE_EXE}
> ${FILE_TMP}
> ${ERROR_FILE}
# this script will clean up all left overs
function log_error () {
echo $1 >> ${ERROR_FILE}
}
function debug () {
if [[ ${DEBUG} -gt 0 ]]
then
echo $1
fi
}
function mail_error {
mail -s "script $0 was executed with errors on host $(hostname)" ${MAILTO} < ${ERROR_FILE}
}
for PROGRAM in bc jq curl awk
do
debug "Checking existing of Program: ${PROGRAM}";
if ( ! $(which ${PROGRAM} > /dev/null 2>&1) )
then
echo "Please install ${PROGRAM}";
log_error "Please install ${PROGRAM}";
BREAK=1
else
debug "Program ${PROGRAM} seems to be installed"
fi
done
if [[ ${BREAK} -gt 0 ]]
then
echo "Aborting execution, because of failed dependecies"
exit ${BREAK}
fi
if [[ ! -e ${LOCK_DIR}/cleanup_elastic.lock ]]
then
touch ${LOCK_DIR}/cleanup_elastic.lock
curl -s http://${ELASTICSERVER}:${ELASTICPORT}/_cat/indices?v > ${FILE_INDEX}
curl -s http://${ELASTICSERVER}:${ELASTICPORT}/_all > ${ALL_INDEX}
for i in $( awk '/open/{print $3}' ${FILE_INDEX} | grep -v -E "${IGNORE_INDICES}")
do
INDEX_DATE=$(jq -c "( .\"${i}\".settings.index.creation_date | tonumber )" ${ALL_INDEX})
CORRECT_DATE=$(echo ${INDEX_DATE} / 1000 | bc)
if [[ ${INDEX_DATE} -lt ${CLOSE_AFTER_DAYS} ]]
then
debug "Will need to close ${i} because it was created $(date -d @${CORRECT_DATE})"
HTTP_RESP=$(curl -w "%{http_code}" -o /dev/null -s -XPOST http://${ELASTICSERVER}:${ELASTICPORT}/${i}/_close)
if [[ ${HTTP_RESP} -gt 200 ]] && [[ ${HTTP_RESP} -ne 404 ]]
then
log_error "Closing index ${i} failed with Errorcode: ${HTTP_RESP}"
fi
else
debug "Will remain open ${i} because it was created $(date -d @${CORRECT_DATE})"
fi
done
# we will need to get all closed indexes creationdates, because they are not included within the /_all query
for i in $( awk '/close/{print $2}' ${FILE_INDEX} | grep -v -E "${IGNORE_INDICES}")
do
debug "#### $i #####"
# Added code to run on multiple hosts
TEMP_DATE=$(curl -s http://${ELASTICSERVER}:${ELASTICPORT}/${i} -w "%{http_code}" -o /tmp/remove_tmp_index)
if [[ ${TEMP_DATE} -eq 200 ]]
then
INDEX_DATE=$(jq -c "( .[].settings.index.creation_date | tonumber )" /tmp/remove_tmp_index)
debug "INDEXDATE : ${INDEX_DATE}"
# Added validation for empty INDEXDATE, to be able to run scripts on multiple hosts
if [[ -n ${INDEX_DATE} ]] && ( echo ${INDEX_DATE} | grep -o -E [[:digit:]] > /dev/null )
then
CORRECT_DATE=$(echo ${INDEX_DATE} / 1000 | bc)
if [[ ${INDEX_DATE} -lt ${DELETE_AFTER_DAYS} ]]
then
debug "Will need to delete ${i} because it was created $(date -d @${CORRECT_DATE})"
HTTP_RESP=$(curl -s -w "%{http_code}" -o /dev/null -XDELETE http://${ELASTICSERVER}:${ELASTICPORT}/${i})
HTTP_TEMP_RESP=$(curl -w "%{http_code}" -o /dev/null -s -XDELETE http://${ELASTICSERVER}:${ELASTICPORT}/_template/${i})
if [[ ${HTTP_RESP} -gt 200 ]] && [[ ${HTTP_RESP} -ne 404 ]]
then
log_error "Deleting index ${i} failed with Errorcode: ${HTTP_RESP}"
fi
if [[ ${HTTP_TEMP_RESP} -gt 200 ]] && [[ ${HTTP_TEMP_RESP} -ne 404 ]]
then
log_error "Deleting Template ${i} failed with Errorcode: ${HTTP_TEMP_RESP}"
fi
else
debug "${i} remain on Store because it was created $(date -d @${CORRECT_DATE})"
fi
else
debug "INDEX ${i} has already been deleted"
fi
else
debug "INDEX ${i} has already been deleted"
fi
done
rm ${LOCK_DIR}/cleanup_elastic.lock
rm /tmp/remove_tmp_index
else
log_error "Lockfile exists. Please remove ${LOCK_DIR}/cleanup_elastic.lock and check your Mails"
fi
if [[ -s ${ERROR_FILE} ]]
then
mail_error
fi