This repository has been archived by the owner on Apr 9, 2021. It is now read-only.
forked from sfrek/es_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indices.sh
executable file
·225 lines (204 loc) · 6.3 KB
/
indices.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/bash
declare -a DATA_NODES
declare -a BIND_NODES
declare HOST
get_host(){
HOST=$(netstat -ltpn | awk '/9200/ {print $4}' | awk -F':' '{print $1}')
[ -z ${HOST} ] && HOST=localhost
}
data_nodes(){
DATA_NODES=( $(curl -s -XGET "http://${HOST}:9200/_cat/nodes?h=r,n" | awk '/^d/ {print $2}' | xargs -i echo -n {}" ") )
}
bind_nodes(){
BIND_NODES=( $( curl -s -XGET 192.168.5.13:9200/_nodes | \
jq -r '.nodes | .[] | .http | .publish_address' | \
grep -v 127.0.0.1 | sed -r 's:inet\[\/(.*)\]:\1:' ) )
}
master_node(){
curl -s -XGET "http://${HOST}:9200/_cat/nodes?h=m,h" | awk '/^\*/ {print $2}'
}
random_data_node(){
echo ${DATA_NODES[$(( ( RANDOM % 5 ) ))]}
}
optimize(){
[ -z "${DATA_NODES}" ] && data_nodes
NODES=$( curl -s -XGET 192.168.5.13:9200/_nodes | \
jq -r '.nodes | .[] | .http | .publish_address' | \
grep -v 127.0.0.1 | sed -r 's:inet\[\/(.*)\]:\1:' )
for NODE in ${NODES}
do
echo -en "\t\e[01;46moptimizin in ${NODE}\e[00m\t"
curl -s -XPOST "http://${NODE}/_optimize"
echo -e "\e[00m"
done
}
open_index(){
local INDEX=${1}
NODES=$( curl -s -XGET 192.168.5.13:9200/_nodes | \
jq -r '.nodes | .[] | .http | .publish_address' | \
grep -v 127.0.0.1 | sed -r 's:inet\[\/(.*)\]:\1:' )
for NODE in ${NODES}
do
echo -en "\t\e[01;46m${NODE}\e[00m\t"
curl -s -XPOST "http://${NODE}/${INDEX}/_open"
echo -e "\e[00m"
done
}
close_index(){
local INDEX=${1}
NODES=$( curl -s -XGET 192.168.5.13:9200/_nodes | \
jq -r '.nodes | .[] | .http | .publish_address' | \
grep -v 127.0.0.1 | sed -r 's:inet\[\/(.*)\]:\1:' )
for NODE in ${NODES}
do
echo -en "\t\e[01;46mclosing in ${NODE}\e[00m\t"
curl -s -XPOST -o /dev/null -w %{http_code} "http://${NODE}/${INDEX}/_close"
echo -e "\e[00m"
done
}
flush_index(){
local INDEX=${1}
NODES=$( curl -s -XGET 192.168.5.13:9200/_nodes | \
jq -r '.nodes | .[] | .http | .publish_address' | \
grep -v 127.0.0.1 | sed -r 's:inet\[\/(.*)\]:\1:' )
for NODE in ${NODES}
do
echo -en "\t\e[01;46mflushing in ${NODE}\e[00m\t"
curl -s -XPOST -o /dev/null -w %{http_code} "http://${NODE}/${INDEX}/_flush"
echo -e "\e[00m"
done
}
delete_index(){
local INDEX=${1}
NODES=$( curl -s -XGET 192.168.5.13:9200/_nodes | \
jq -r '.nodes | .[] | .http | .publish_address' | \
grep -v 127.0.0.1 | sed -r 's:inet\[\/(.*)\]:\1:' )
for NODE in ${NODES}
do
echo -en "\t\e[01;46mdeleteing ${NODE}\e[00m\t"
# Get index state ( open/close .. ) and remove all spaces
STATE=$(curl -s -XGET "http://${NODE}/_cat/indices/${INDEX}?h=s" | sed -e 's/ //g')
if [[ "${STATE}" == "close" ]]
then
echo -en "\e[01;34mdeleting... \e[00m"
curl -s -XDELETE -o /dev/null -w %{http_code} "http://${NODE}/${INDEX}"
else
echo -en "\e[01;37m ${STATE}\t\e[01;41mIndex is Not Closed\e[00m"
fi
echo -e "\e[00m"
# sleep $(( (RANDOM % 3) + 1 ))
done
}
relocate(){
[ -z "${DATA_NODES}" ] && data_nodes
[ ! -d relocate ] && mkdir -v relocate
local LEN=${#DATA_NODES[@]}
while read INDEX SHARD
do
NODE=${DATA_NODES[$(( RANDOM % LEN ))]}
echo -e "\e[01;33mReallocating in \e[01;35m$NODE \e[01;37m[\e[01;36m ${INDEX} \e[01;37m]\e[00m"
curl -s -o relocate/${INDEX}.${SHARD}.${NODE}.json -XPOST "http://${HOST}:9200/_cluster/reroute" -d '{
"commands" : [ {
"allocate" : {
"index" : "'${INDEX}'",
"shard" : '${SHARD}',
"node" : "'${NODE}'",
"allow_primary" : true
}
}
]
}'
# echo -e "\e[00m"
done < <( curl -s -XGET http://${HOST}:9200/_cat/shards | grep -i unassigned | awk '{print $1,$2}' | sort )
# Note!
# bash redirections:
# <
# <<
# <<<
# <( )
}
ACTION=${1}
get_host
case ${ACTION} in
list|LIST)
PATTERN=${2}
echo -e "\e[01;33mindices: \e[01;35m${ACTION}\e[00m"
curl -s -XGET http://${HOST}:9200/_cat/indices?h=i,s,dc,ss
# "\e[01;37m[\e[01;36m ${INDICES} \e[01;37m]\e[00m"
;;
delete|DELETE)
ARGS=(${@})
INDICES=${ARGS[@]:1}
[[ -z "${INDICES}" ]] && echo -e "\e[01;41mMissing Index\e[00m" && exit 1
echo -e "\e[01;33mindices: \e[01;35m${ACTION} \e[01;37m[\e[01;36m ${INDICES} \e[01;37m]\e[00m"
if [ ! -z "${INDICES}" ]
then
for INDEX in ${INDICES[@]}
do
flush_index ${INDEX}
close_index ${INDEX}
delete_index ${INDEX}
done
fi
optimize
;;
optimize|OPTIMIZE)
echo -e "\e[01;33mindices: \e[01;35m${ACTION}\e[00m"
optimize
;;
open|OPEN)
ARGS=(${@})
INDICES=${ARGS[@]:1}
[[ -z "${INDICES}" ]] && echo -e "\e[01;41mMissing Index\e[00m" && exit 1
echo -e "\e[01;33mindices: \e[01;35m${ACTION} \e[01;37m[\e[01;36m ${INDICES} \e[01;37m]\e[00m"
if [ ! -z "${INDICES}" ]
then
for INDEX in ${INDICES[@]}
do
open_index ${INDEX}
done
fi
;;
# INDEX=${2}
# [[ -z ${INDEX} ]] && echo -e "\e[01;41mMissing Index\e[00m" && exit 1
# echo -e "\e[01;33mindices: \e[01;35m${ACTION} \e[01;37m[\e[01;36m ${INDEX} \e[01;37m]\e[00m"
# open_index ${INDEX}
# ;;
close|CLOSE)
ARGS=(${@})
INDICES=${ARGS[@]:1}
[[ -z "${INDICES}" ]] && echo -e "\e[01;41mMissing Index\e[00m" && exit 1
echo -e "\e[01;33mindices: \e[01;35m${ACTION} \e[01;37m[\e[01;36m ${INDICES} \e[01;37m]\e[00m"
if [ ! -z "${INDICES}" ]
then
for INDEX in ${INDICES[@]}
do
close_index ${INDEX}
done
fi
;;
# INDEX=${2}
# [[ -z ${INDEX} ]] && echo -e "\e[01;41mMissing Index\e[00m" && exit 1
# echo -e "\e[01;33mindices: \e[01;35m${ACTION} \e[01;37m[\e[01;36m ${INDEX} \e[01;37m]\e[00m"
# close_index ${INDEX}
# ;;
relocate|RELOCATE)
echo -e "\e[01;33mindices: \e[01;35m${ACTION}\e[00m"
relocate
;;
check)
echo -e "\e[01;33mindices: \e[01;35m${ACTION}\e[00m"
echo -e "\t\e[01;43mdata_nodes\e[00m"
curl -s -XGET http://${HOST}:9200/_cat/nodes | sed '1d' | sort
echo -e "\t\e[01;46mdata_nodes\e[00m"
data_nodes
echo ${DATA_NODES[@]}
echo -e "\t\e[01;45mmaster_node\e[00m"
master_node
echo -e "\t\e[01;44mrandom_data_node\e[00m"
random_data_node
;;
*)
echo "in progressss...."
;;
esac