Created
March 12, 2018 13:12
-
-
Save alexcherkesov/19dab7f75f170c82847dec59aa2ca771 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import requests | |
import re | |
import datetime | |
elasticsearch_url = 'http://elasticsearch' | |
req = requests.get(elasticsearch_url + '/_aliases?pretty=true') | |
indexes_json = req.json() | |
patt = re.compile(r'-\d{4}\.\d{2}\.\d{2}') | |
logs_indexes = [] | |
for index in indexes_json: | |
m = patt.findall(index) | |
if m: | |
prefix = index.replace(m[0], '') | |
str_date = m[0].replace('-', '') | |
d = datetime.datetime.strptime(str_date, "%Y.%m.%d").date() | |
logs_indexes.append({'date': d, 'index': index}) | |
sorted_indexes = sorted(logs_indexes, key=lambda k: k['date'], reverse=True) | |
if len(sorted_indexes) > 60: | |
sorted_indexes = sorted_indexes[0:60] | |
sorted_indexes_names = [index['index'] for index in sorted_indexes] | |
logs_indexes_name = [index['index'] for index in logs_indexes] | |
to_remove = [] | |
for index in logs_indexes_name: | |
if index not in sorted_indexes_names: | |
to_remove.append(index) | |
if to_remove: | |
for x in to_remove: | |
print('Removing "' + x + '"...') | |
try: | |
requests.delete(elasticsearch_url + '/' + x) | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment