对elasticsearch过期索引的定时删除

本脚对我方es中10个按日增长的索引进行定期删除,多数索引只保留7天,部分索引保留30天

每个close_delete方法中只需要替换for i in echo $apm_error672_datetime和apm_overDateIndex="apm-6.7.2-error-$indexOriDate"两处即可直接使用

#/bin/bash
#author wxd
#apm-6.7.2-error-
#apm-6.7.2-metric-
#apm-6.7.2-transaction-
#apm-6.7.2-span-
#apm-7.2.0-error-
#apm-7.2.0-metric-
#apm-7.2.0-onboarding-
#apm-7.2.0-span-
#apm-7.2.0-transaction-
#nginx-log-

PARAMETER=$1
ES_URL="http://xxx.xxx.xxx.xxx:9200" #填写你的es对外http连接地址
ES_USER="username"  #username代表你的你的es用户名
ES_PASSWORD="password" #password代表你的es用户密码
#num=`curl -s -u username:password http://xxx.xxx.xxx:9200/_cat/indices | awk '{print $3}' | grep "apm-7.2.0-span-" | sort -n |head -1 | sed -r 's/apm-7.2.0-span-(.*)/\1/'`
#yesterday=$(date -d '-1days' +'%Y.%m.%d')  
#today=$(date '+%Y.%m.%d')  

before7day=$(date -d '-7days' +'%Y%m%d')  
before30day=$(date -d '-30days' +'%Y%m%d')  

usage() {
    echo -e "\033[46;31mUsage\033[0m: Please use \e[0;35m$0 foo \e[0m"    
    exit 1;
}

#1 apm-6.7.2-error-
apm_error672_datetime=`curl -s -u $ES_USER:$ES_PASSWORD $ES_URL/_cat/indices | awk '{print $3}' | grep "apm-6.7.2-error-" | sort -n | sed -r 's/apm-6.7.2-error-(.*)/\1/'`
close_delete_apm_error672_index(){
    echo "start match apm_error672_overDateIndex"
    for i in `echo $apm_error672_datetime`;do
		indexOriDate=`echo $i | head -n 1`
		indexFormatDate=`echo $i | head -n 1 | sed 's/\.//g'`
		apm_overDateIndex="apm-6.7.2-error-$indexOriDate"
		if [[ $before7day -ge $indexFormatDate ]]; then    
			echo -e "\n$(date '+%Y-%m-%d %H:%M:%S')   es_index match successful,当前索引:\e[0;35m$apm_overDateIndex\e[0m"
			curl -XPOST -u $ES_USER:$ES_PASSWORD "$ES_URL/$apm_overDateIndex/_close"
			echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 关闭完成"
			if [[ ` echo $? ` == 0 ]];then
				curl -XDELETE -u $ES_USER:$ES_PASSWORD "$ES_URL/$apm_overDateIndex" 	
				echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 删除完成"
			else
				echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 关闭失败,无法进行删除"
			fi		
		else
			echo -e "\n$(date '+%Y-%m-%d %H:%M:%S')   es_index match fail,当前索引:\e[0;35m$apm_overDateIndex\e[0m"
			echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 没有过期,不需要关闭,over.."
		fi	
    done	
}
#2 apm-6.7.2-metric-
apm_metric672_datetime=`curl -s -u $ES_USER:$ES_PASSWORD $ES_URL/_cat/indices | awk '{print $3}' | grep "apm-6.7.2-metric-" | sort -n | sed -r 's/apm-6.7.2-metric-(.*)/\1/'`
close_delete_apm_metric672_index(){
    echo "start match apm_overDateIndex"
    for i in `echo $apm_metric672_datetime`;do
		indexOriDate=`echo $i | head -n 1`
		indexFormatDate=`echo $i | head -n 1 | sed 's/\.//g'`
		apm_overDateIndex="apm-6.7.2-metric-$indexOriDate"
		if [[ $before7day -ge $indexFormatDate ]]; then    
			echo -e "\n$(date '+%Y-%m-%d %H:%M:%S')   es_index match successful,当前索引:\e[0;35m$apm_overDateIndex\e[0m"
			curl -XPOST -u $ES_USER:$ES_PASSWORD "$ES_URL/$apm_overDateIndex/_close"
			echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 关闭完成"
			if [[ ` echo $? ` == 0 ]];then
				curl -XDELETE -u $ES_USER:$ES_PASSWORD "$ES_URL/$apm_overDateIndex" 	
				echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 删除完成"
			else
				echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 关闭失败,无法进行删除"
			fi		
		else
			echo -e "\n$(date '+%Y-%m-%d %H:%M:%S')   es_index match fail,当前索引:\e[0;35m$apm_overDateIndex\e[0m"
			echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 没有过期,不需要关闭,over.."
		fi	
    done	
}
#3 apm-6.7.2-transaction-
apm_transaction672_datetime=`curl -s -u $ES_USER:$ES_PASSWORD $ES_URL/_cat/indices | awk '{print $3}' | grep "apm-6.7.2-transaction-" | sort -n | sed -r 's/apm-6.7.2-transaction-(.*)/\1/'`
close_delete_apm_transaction672_index(){
    echo "start match apm_overDateIndex"
    for i in `echo $apm_transaction672_datetime`;do
		indexOriDate=`echo $i | head -n 1`
		indexFormatDate=`echo $i | head -n 1 | sed 's/\.//g'`
		apm_overDateIndex="apm-6.7.2-transaction-$indexOriDate"
		if [[ $before7day -ge $indexFormatDate ]]; then    
			echo -e "\n$(date '+%Y-%m-%d %H:%M:%S')   es_index match successful,当前索引:\e[0;35m$apm_overDateIndex\e[0m"
			curl -XPOST -u $ES_USER:$ES_PASSWORD "$ES_URL/$apm_overDateIndex/_close"
			echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 关闭完成"
			if [[ ` echo $? ` == 0 ]];then
				curl -XDELETE -u $ES_USER:$ES_PASSWORD "$ES_URL/$apm_overDateIndex" 	
				echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 删除完成"
			else
				echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 关闭失败,无法进行删除"
			fi		
		else
			echo -e "\n$(date '+%Y-%m-%d %H:%M:%S')   es_index match fail,当前索引:\e[0;35m$apm_overDateIndex\e[0m"
			echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 没有过期,不需要关闭,over.."
		fi	
    done	
}

#4 apm-6.7.2-span-
apm_span672_datetime=`curl -s -u $ES_USER:$ES_PASSWORD $ES_URL/_cat/indices | awk '{print $3}' | grep "apm-6.7.2-span-" | sort -n | sed -r 's/apm-6.7.2-span-(.*)/\1/'`
close_delete_apm_span672_index(){
    echo "start match apm_overDateIndex"
    for i in `echo $apm_span672_datetime`;do
		indexOriDate=`echo $i | head -n 1`
		indexFormatDate=`echo $i | head -n 1 | sed 's/\.//g'`
		apm_overDateIndex="apm-6.7.2-span-$indexOriDate"
		if [[ $before7day -ge $indexFormatDate ]]; then    
			echo -e "\n$(date '+%Y-%m-%d %H:%M:%S')   es_index match successful,当前索引:\e[0;35m$apm_overDateIndex\e[0m"
			curl -XPOST -u $ES_USER:$ES_PASSWORD "$ES_URL/$apm_overDateIndex/_close"
			echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 关闭完成"
			if [[ ` echo $? ` == 0 ]];then
				curl -XDELETE -u $ES_USER:$ES_PASSWORD "$ES_URL/$apm_overDateIndex" 	
				echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 删除完成"
			else
				echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 关闭失败,无法进行删除"
			fi		
		else
			echo -e "\n$(date '+%Y-%m-%d %H:%M:%S')   es_index match fail,当前索引:\e[0;35m$apm_overDateIndex\e[0m"
			echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 没有过期,不需要关闭,over.."
		fi	
    done	
}
#5 apm-7.2.0-error-
apm_error720_datetime=`curl -s -u $ES_USER:$ES_PASSWORD $ES_URL/_cat/indices | awk '{print $3}' | grep "apm-7.2.0-error-" | sort -n | sed -r 's/apm-7.2.0-error-(.*)/\1/'`
close_delete_apm_error720_index(){
    echo "start match apm_overDateIndex"
    for i in `echo $apm_error720_datetime`;do
		indexOriDate=`echo $i | head -n 1`
		indexFormatDate=`echo $i | head -n 1 | sed 's/\.//g'`
		apm_overDateIndex="apm-7.2.0-error-$indexOriDate"
		if [[ $before7day -ge $indexFormatDate ]]; then    
			echo -e "\n$(date '+%Y-%m-%d %H:%M:%S')   es_index match successful,当前索引:\e[0;35m$apm_overDateIndex\e[0m"
			curl -XPOST -u $ES_USER:$ES_PASSWORD "$ES_URL/$apm_overDateIndex/_close"
			echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 关闭完成"
			if [[ ` echo $? ` == 0 ]];then
				curl -XDELETE -u $ES_USER:$ES_PASSWORD "$ES_URL/$apm_overDateIndex" 	
				echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 删除完成"
			else
				echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 关闭失败,无法进行删除"
			fi		
		else
			echo -e "\n$(date '+%Y-%m-%d %H:%M:%S')   es_index match fail,当前索引:\e[0;35m$apm_overDateIndex\e[0m"
			echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 没有过期,不需要关闭,over.."
		fi	
    done	
}

#6 apm-7.2.0-metric-
apm_metric720_datetime=`curl -s -u $ES_USER:$ES_PASSWORD $ES_URL/_cat/indices | awk '{print $3}' | grep "apm-7.2.0-metric-" | sort -n | sed -r 's/apm-7.2.0-metric-(.*)/\1/'`
close_delete_apm_metric720_index(){
    echo "start match apm_overDateIndex"
    for i in `echo $apm_metric720_datetime`;do
		indexOriDate=`echo $i | head -n 1`
		indexFormatDate=`echo $i | head -n 1 | sed 's/\.//g'`
		apm_overDateIndex="apm-7.2.0-metric-$indexOriDate"
		if [[ $before7day -ge $indexFormatDate ]]; then    
			echo -e "\n$(date '+%Y-%m-%d %H:%M:%S')   es_index match successful,当前索引:\e[0;35m$apm_overDateIndex\e[0m"
			curl -XPOST -u $ES_USER:$ES_PASSWORD "$ES_URL/$apm_overDateIndex/_close"
			echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 关闭完成"
			if [[ ` echo $? ` == 0 ]];then
				curl -XDELETE -u $ES_USER:$ES_PASSWORD "$ES_URL/$apm_overDateIndex" 	
				echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 删除完成"
			else
				echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 关闭失败,无法进行删除"
			fi		
		else
			echo -e "\n$(date '+%Y-%m-%d %H:%M:%S')   es_index match fail,当前索引:\e[0;35m$apm_overDateIndex\e[0m"
			echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 没有过期,不需要关闭,over.."
		fi	
    done	
}
#7 apm-7.2.0-onboarding-
apm_onboarding720_datetime=`curl -s -u $ES_USER:$ES_PASSWORD $ES_URL/_cat/indices | awk '{print $3}' | grep "apm-7.2.0-onboarding-" | sort -n | sed -r 's/apm-7.2.0-onboarding-(.*)/\1/'`
close_delete_apm_onboarding720_index(){
    echo "start match apm_overDateIndex"
    for i in `echo $apm_onboarding720_datetime`;do
		indexOriDate=`echo $i | head -n 1`
		indexFormatDate=`echo $i | head -n 1 | sed 's/\.//g'`
		apm_overDateIndex="apm-7.2.0-onboarding-$indexOriDate"
		if [[ $before7day -ge $indexFormatDate ]]; then    
			echo -e "\n$(date '+%Y-%m-%d %H:%M:%S')   es_index match successful,当前索引:\e[0;35m$apm_overDateIndex\e[0m"
			curl -XPOST -u $ES_USER:$ES_PASSWORD "$ES_URL/$apm_overDateIndex/_close"
			echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 关闭完成"
			if [[ ` echo $? ` == 0 ]];then
				curl -XDELETE -u $ES_USER:$ES_PASSWORD "$ES_URL/$apm_overDateIndex" 	
				echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 删除完成"
			else
				echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 关闭失败,无法进行删除"
			fi		
		else
			echo -e "\n$(date '+%Y-%m-%d %H:%M:%S')   es_index match fail,当前索引:\e[0;35m$apm_overDateIndex\e[0m"
			echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 没有过期,不需要关闭,over.."
		fi	
    done	
}
#8 apm-7.2.0-span-
apm_span720_datetime=`curl -s -u $ES_USER:$ES_PASSWORD $ES_URL/_cat/indices | awk '{print $3}' | grep "apm-7.2.0-span-" | sort -n | sed -r 's/apm-7.2.0-span-(.*)/\1/'`
close_delete_apm_span720_index(){
    echo "start match apm_overDateIndex"
    for i in `echo $apm_span720_datetime`;do
		indexOriDate=`echo $i | head -n 1`
		indexFormatDate=`echo $i | head -n 1 | sed 's/\.//g'`
		apm_overDateIndex="apm-7.2.0-span-$indexOriDate"
		if [[ $before7day -ge $indexFormatDate ]]; then    
			echo -e "\n$(date '+%Y-%m-%d %H:%M:%S')   es_index match successful,当前索引:\e[0;35m$apm_overDateIndex\e[0m"
			curl -XPOST -u $ES_USER:$ES_PASSWORD "$ES_URL/$apm_overDateIndex/_close"
			echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 关闭完成"
			if [[ ` echo $? ` == 0 ]];then
				curl -XDELETE -u $ES_USER:$ES_PASSWORD "$ES_URL/$apm_overDateIndex" 	
				echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 删除完成"
			else
				echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 关闭失败,无法进行删除"
			fi		
		else
			echo -e "\n$(date '+%Y-%m-%d %H:%M:%S')   es_index match fail,当前索引:\e[0;35m$apm_overDateIndex\e[0m"
			echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 没有过期,不需要关闭,over.."
		fi	
    done	
}
#9 apm-7.2.0-transaction-
apm_transaction720_datetime=`curl -s -u $ES_USER:$ES_PASSWORD $ES_URL/_cat/indices | awk '{print $3}' | grep "apm-7.2.0-transaction-" | sort -n | sed -r 's/apm-7.2.0-transaction-(.*)/\1/'`
close_delete_apm_transaction720_index(){
    echo "start match apm_overDateIndex"
    for i in `echo $apm_transaction720_datetime`;do
		indexOriDate=`echo $i | head -n 1`
		indexFormatDate=`echo $i | head -n 1 | sed 's/\.//g'`
		apm_overDateIndex="apm-7.2.0-transaction-$indexOriDate"
		if [[ $before7day -ge $indexFormatDate ]]; then    
			echo -e "\n$(date '+%Y-%m-%d %H:%M:%S')   es_index match successful,当前索引:\e[0;35m$apm_overDateIndex\e[0m"
			curl -XPOST -u $ES_USER:$ES_PASSWORD "$ES_URL/$apm_overDateIndex/_close"
			echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 关闭完成"
			if [[ ` echo $? ` == 0 ]];then
				curl -XDELETE -u $ES_USER:$ES_PASSWORD "$ES_URL/$apm_overDateIndex" 	
				echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 删除完成"
			else
				echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 关闭失败,无法进行删除"
			fi		
		else
			echo -e "\n$(date '+%Y-%m-%d %H:%M:%S')   es_index match fail,当前索引:\e[0;35m$apm_overDateIndex\e[0m"
			echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 没有过期,不需要关闭,over.."
		fi	
    done	
}
#10 nginx-log-
nginxlog_datetime=`curl -s -u $ES_USER:$ES_PASSWORD $ES_URL/_cat/indices | awk '{print $3}' | grep "nginx-log-" | sort -n | sed -r 's/nginx-log-(.*)/\1/'`
close_delete_nginxlog_index(){
    echo "start match apm_overDateIndex"
    for i in `echo $nginxlog_datetime`;do
		indexOriDate=`echo $i | head -n 1`
		indexFormatDate=`echo $i | head -n 1 | sed 's/\.//g'`
		apm_overDateIndex="nginx-log-$indexOriDate"
		if [[ $before30day -ge $indexFormatDate ]]; then    
			echo -e "\n$(date '+%Y-%m-%d %H:%M:%S')   es_index match successful,当前索引:\e[0;35m$apm_overDateIndex\e[0m"
			curl -XPOST -u $ES_USER:$ES_PASSWORD "$ES_URL/$apm_overDateIndex/_close"
			echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 关闭完成"
			if [[ ` echo $? ` == 0 ]];then
				curl -XDELETE -u $ES_USER:$ES_PASSWORD "$ES_URL/$apm_overDateIndex" 	
				echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 删除完成"
			else
				echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 关闭失败,无法进行删除"
			fi		
		else
			echo -e "\n$(date '+%Y-%m-%d %H:%M:%S')   es_index match fail,当前索引:\e[0;35m$apm_overDateIndex\e[0m"
			echo -e "$(date '+%Y-%m-%d %H:%M:%S')   索引\e[0;35m$apm_overDateIndex\e[0m, 没有过期,不需要关闭,over.."
		fi	
    done	
}

if [ -n "$PARAMETER" ]; then
   case "$PARAMETER" in
    foo)		
		echo -e "\033[46;31m1. Do close && delete close_delete_apm_error672_index!!!\033[0m"
		close_delete_apm_error672_index	
		
		echo -e "\033[46;31m2. Do close && delete close_delete_apm_metric672_index!!!\033[0m"
		close_delete_apm_metric672_index
		
		echo -e "\033[46;31m3. Do close && delete close_delete_apm_transaction672_index!!!\033[0m"
		close_delete_apm_transaction672_index	
		
		echo -e "\033[46;31m4. Do close && delete close_delete_apm_span672_index!!!\033[0m"
		close_delete_apm_span672_index	
		
		echo -e "\033[46;31m5. Do close && delete close_delete_apm_error720_index!!!\033[0m"
		close_delete_apm_error720_index	
		
		echo -e "\033[46;31m6. Do close && delete close_delete_apm_metric720_index!!!\033[0m"
		close_delete_apm_metric720_index	
		
		echo -e "\033[46;31m7. Do close && delete close_delete_apm_onboarding720_index!!!\033[0m"
		close_delete_apm_onboarding720_index	
		
		echo -e "\033[46;31m8. Do close && delete close_delete_apm_span720_index!!!\033[0m"
		close_delete_apm_span720_index	
		
		echo -e "\033[46;31m9. Do close && delete close_delete_apm_transaction720_index!!!\033[0m"
		close_delete_apm_transaction720_index	
		
		echo -e "\033[46;31m10. Do close && delete close_delete_nginxlog_index!!!\033[0m"
		close_delete_nginxlog_index	
		echo -e "-----------------------------------------------\033[46;31mAll done!!!\033[0m-----------------------------------------------"
        ;;  
    *)  
        usage
        exit
        ;;  
    esac
else
    echo -e "\033[46;31merror\033[0m: please input parameter"
fi

最后linux crontab 设置如下:
00 00 * * * /bin/bash /home/admin/.snapshot-elk/auto_del_overDate_index.sh foo >> /home/admin/.snapshot-elk/del-elkOverDateIndex-running.log

运行日志如下

对elasticsearch过期索引的定时删除_第1张图片

对elasticsearch过期索引的定时删除_第2张图片

你可能感兴趣的:(linux-devops)