Created
March 31, 2023 14:10
-
-
Save mamashin/57a08c0556ef5edaddd0297f1a7a9768 to your computer and use it in GitHub Desktop.
Remove Zabbix indexes from Elastic that are older than 1 year (looks like str-2022-02-01)
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
#!/usr/bin/env python3 | |
from datetime import datetime, timedelta | |
import requests | |
import re | |
ES_HOST = 'http://localhost:9200' | |
def string_to_date(date_string): | |
return datetime.strptime(date_string, '%Y-%m-%d').date() | |
def current_date_minus_days(days: int = 365): | |
return datetime.now().date() - timedelta(days=days) | |
def delete_index(index_name): | |
url = f"{ES_HOST}/{index_name}" | |
response = requests.delete(url) | |
print(f'Delete {index_name}, status: {response.status_code}') | |
def search_elastic_indexes(): | |
match_name = re.compile(r'\w+-(\d{4}-\d{2}-\d{2})') | |
url = f"{ES_HOST}/_cat/indices?h=index&format=json" | |
response = requests.get(url) | |
if response.status_code != 200: | |
print(f'Error: {response.status_code}') | |
return None | |
one_year_ago = current_date_minus_days() | |
for indx in response.json(): | |
index_name = match_name.match(indx['index']) | |
if index_name: | |
if string_to_date(index_name.group(1)) < one_year_ago: | |
delete_index(indx['index']) | |
if __name__ == '__main__': | |
search_elastic_indexes() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment