Created
February 26, 2016 13:21
-
-
Save replsv/6a49a1ea54cfc864c067 to your computer and use it in GitHub Desktop.
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/bash | |
# es-maintenance-script.sh | |
# Notes: | |
# - https://www.elastic.co/guide/en/elasticsearch/guide/current/retiring-data.html | |
# - http://tech.superhappykittymeow.com/?p=296 | |
# | |
# Author: Gabriel C <shellcooking@gmail.com> | |
# Display instructions | |
help() | |
{ | |
cat << EOF | |
es-maintenance-script.sh | |
Deletes old indexes and closes indexes until yesterday. | |
USAGE: ./es-maintenance-script.sh [opt] | |
Opt: | |
-help Show this message | |
-i Indices to keep (default: 20) | |
-es ES Url (default: http://localhost:9200) | |
-prefix Consistent index name (default: logstash) | |
-l Log file for execution | |
EOF | |
} | |
# Default values | |
ES="http://localhost:9200" | |
KEEP=20 | |
PREFIX="logstash" | |
# Pattern for dates | |
RE_D="^[0-9]+$" | |
while getopts ":i:e:prefix:l:help" flag | |
do | |
case "$flag" in | |
help) | |
help | |
exit 0 | |
;; | |
i) | |
if [[ $OPTARG =~ $RE_D ]]; then | |
KEEP=$OPTARG | |
else | |
echo -e "${ERROR} Indexes to keep must be an integer.\n" | |
exit 1 | |
fi | |
;; | |
es) | |
ES=$OPTARG | |
;; | |
prefix) | |
PREFIX=$OPTARG | |
;; | |
l) | |
LOGFILE=$OPTARG | |
;; | |
?) | |
help | |
exit 1 | |
;; | |
esac | |
done | |
# Get the indices | |
INDICES=`curl -s "${ES}/_cat/indices?v" | awk '/'$PREFIX'/{match($0, /[:blank]*('$PREFIX'.[^ ]+)[:blank]*/, m); print m[1];}' | sort -r` | |
if [ -z "$INDICES" ]; then | |
echo "No indices returned containing '${PREFIX}' from ${ES}." | |
exit 1 | |
fi | |
if [ -n "$LOGFILE" ] && ! [ -e $LOGFILE ]; then | |
touch $LOGFILE | |
fi | |
declare -a INDEX=($INDICES) | |
# Closing indices | |
if [ ${#INDEX[@]} -gt 1 ]; then | |
MAX=`expr ${KEEP} - 1` | |
for index in `seq 1 $MAX`; do | |
index=${INDEX[index]} | |
if [ -n "$index" ]; then | |
echo `date "+[%Y-%m-%d %H:%M] "`" Closing index: $index." | |
curl -s -XPOST "${ES}/$index/_close" > /dev/null | |
if [ ! -z "$LOGFILE" ]; then | |
echo `date "+[%Y-%m-%d %H:%M] "`" Closing index: $index." >> $LOGFILE | |
fi | |
fi | |
done | |
else | |
echo "No indices to close" | |
fi | |
# Deleting indices | |
if [ ${#INDEX[@]} -gt $KEEP ]; then | |
for index in ${INDEX[@]:$KEEP}; do | |
if [ -n "$index" ]; then | |
echo `date "+[%Y-%m-%d %H:%M] "`" Deleting index: $index." | |
curl -s -XDELETE "${ES}/$index" > /dev/null | |
if [ ! -z "$LOGFILE" ]; then | |
echo `date "+[%Y-%m-%d %H:%M] "`" Deleting index: $index." >> $LOGFILE | |
fi | |
fi | |
done | |
else | |
echo "No indices to delete" | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment