批量解除es索引只读状态脚本

背景:elasticsearch磁盘超过80%后,会出现连接都正常,写接口也不报错,只是数据没有写入,当然也就无法查询到。

经过查找原因,原来磁盘超过80%后,es默认会变成只读模式,扩容后,也不会自动还原。

判断是否索引只读命令

curl -u admin:xxxxxxxxx -k https://xx.xx.xx.xx:9200/index-name/_settings?pretty 

如下图,read_only_allow_delete 为true即为索引只读。

 

可通过以下命令还原

 
curl -XPUT -H "Content-Type: application/json" -u admin:xxxxxxxxx -k https://xx.xx.xx.xx:9200/index-name/_settings -d '{"index.blocks.read_only_allow_delete": null}'

 

脚本实现,检测索引是否存在只读,如果只读就恢复

 
#!/bin/sh
ES_ENDPOINT="172.13.98.214:9200"
ES_USER="admin"
ES_PASSWORD="xxxxxxxxxx"
PROTOCOL="https" #https or http
AA='"read_only_allow_delete": "true"'

if [[ ${PROTOCOL} = "https" ]];then
PARA="-u "${ES_USER}:${ES_PASSWORD}" -k"
else
PARA=" "
fi

curl -s ${PARA} ${PROTOCOL}://${ES_ENDPOINT}/_cat/indices| grep -v searchguard > ./index-list

for i in $(cat index-list | awk '{print $3}');
do
curl -s ${PARA} ${PROTOCOL}://${ES_ENDPOINT}/$i/_settings?pretty > linshi
AA=`cat linshi | grep read_only_allow_delete`
if [ -z "$AA" ]
then
echo " no read-only "
else
BB=`echo $AA |awk -F':' '{print $2}'`
if [ $BB = '"true"' ]
then
echo $i
curl -s -XPUT -H "Content-Type: application/json" ${PARA} ${PROTOCOL}://${ES_ENDPOINT}/$i/_settings -d '{"index.blocks.read_only_allow_delete": null}'
else
echo " "
fi
fi

done

rm -rf ./linshi

自定义es地址和用户密码等信息后直接执行即可。

 

 

posted @ 2023-05-30 16:16  ZANAN  阅读(370)  评论(0编辑  收藏  举报