Created
July 27, 2022 10:09
-
-
Save bluecmd/cfeb0c6e32ee41e36a079ac6cf59520e to your computer and use it in GitHub Desktop.
Prune old indices from Elastiflow
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
#!/usr/bin/python3 | |
import datetime | |
import re | |
import requests | |
import urllib | |
MAX_AGE_DAYS = 30 | |
HOST = 'http://localhost:5601' | |
ir = requests.get(urllib.parse.urljoin(HOST, '/api/index_management/indices')) | |
to_delete = [] | |
for index in ir.json(): | |
m = re.match(r'elastiflow-flow-codex.*([0-9]{4})\.([0-9]{2})\.([0-9]{2})', index['name']) | |
if m is None: | |
continue | |
date = datetime.date(int(m.group(1)), int(m.group(2)), int(m.group(3))) | |
if datetime.date.today() - date > datetime.timedelta(days=MAX_AGE_DAYS): | |
to_delete.append(index['name']) | |
if to_delete: | |
resp = requests.post( | |
urllib.parse.urljoin(HOST, '/api/index_management/indices/delete'), | |
json={"indices":to_delete}, | |
headers={'kbn-xsrf': 'batch'}) | |
if resp.status_code != 200: | |
print(resp.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment