Last active
June 2, 2021 02:47
-
-
Save ohsawa0515/263910d27495f7a08c8d to your computer and use it in GitHub Desktop.
Elasticsearchのlogstash形式の古いインデックスを削除するスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# インデックスのプレフィックス | |
PREFIX="logstash-" | |
if [ $1 ]; then | |
PREFIX=$1 | |
fi | |
# OLDER_THAN日以上経過したインデックスを削除する | |
OLDER_THAN=30 | |
if [ $2 ]; then | |
OLDER_THAN=$2 | |
fi | |
FROM=`date +%Y/%m/%d --date "${OLDER_THAN} days ago"` | |
CURRENT_DATE=$FROM | |
while : | |
do | |
INDEX=`date --date "${CURRENT_DATE}" "+$PREFIX%Y.%m.%d"` | |
# インデックスの存在確認をしていく | |
# 200(OK) の場合は存在し、404(Not Found)の場合は存在しない | |
STATUS_CODE=`curl -s --head http://localhost:9200/${INDEX} | egrep -o "HTTP\/1\.1 ([0-9])+" | awk '{print $2}'` | |
# インデックスが存在しない場合はループを抜ける | |
if [ $STATUS_CODE = "404" ]; then | |
break | |
fi | |
# インデックス削除 | |
echo "Delete index ${INDEX}" | |
curl -w'\n' -XDELETE "http://localhost:9200/${INDEX}" | |
CURRENT_DATE=`date -d "$CURRENT_DATE 1 day ago" "+%Y/%m/%d"` | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment