#!/bin/bash
# author: Wang XiaoQiang
# crontab -e
# 0 0 * * * /root/script/del_esindex.sh
# auto delete day ago elasticsearch index
dtime=`date -d "7 day ago" +%Y-%m-%d`
dtime_stamp=`date -d "$dtime" +%s`
indexs=`curl -s 'http://127.0.0.1:9200/_cat/indices' | awk '$3~/^logstash/{print $3}'`
for line in $indexs;do
index=$line
itime=`echo $line | awk -F - '{print $3}' | tr '.' '-'`
itime_stamp=`date -d "$itime" +%s`
if [ $itime_stamp -lt $dtime_stamp ];then
curl -X DELETE "http://127.0.0.1:9200/$index" > /dev/null >&
fi
done