Skip to content

Instantly share code, notes, and snippets.

@blzzua
Created April 18, 2023 20:16
Show Gist options
  • Save blzzua/dd263f8ba81c6ae7d5e4b2e97f36ec54 to your computer and use it in GitHub Desktop.
Save blzzua/dd263f8ba81c6ae7d5e4b2e97f36ec54 to your computer and use it in GitHub Desktop.
elasticsearch count_size_by_index_pattern.py
#!/bin/python3
import re
import requests
SIZE_SUFFIX = {'tb': 1024*1024*1024*1024,'gb': 1024*1024*1024, 'mb': 1024*1024, 'kb': 1024, 'b': 1}
def read_size_data(url='http://127.0.0.1:9200/_cat/indices?h=index,pri.store.size'):
response = requests.get(url)
if response.status_code == 200:
return response.text
else:
return ''
def sizeof_fmt(num, suffix="b"):
for unit in [" ", " k", " m", " g"]:
if abs(num) < 1024.0:
return f"{num:3.1f}{unit}{suffix}"
num /= 1024.0
return '"{num:.1f}t{suffix}'
def print_formated_group_size(groups_size):
max_grp_name_len = max(map(len,groups_size.keys()))
for i, size in sorted(groups_size.items(), key=lambda x: x[1]):
ind_list = [t[1].strip(i+'-') for t in indices if t[0] == i]
cnt = len(ind_list)
if cnt >= 3:
short_ind_str = ind_list[0] + '..' + ind_list[-1]
elif cnt == 2:
short_ind_str = ind_list[0] + ' ' + ind_list[-1]
elif cnt == 1:
short_ind_str = ind_list[0]
else:
short_ind_str
f_indexname = i.rjust(max_grp_name_len)
f_size = sizeof_fmt(size).rjust(9)
print(f'{f_size}\t{f_indexname}\tcnt: {cnt}\t indices: {short_ind_str}')
if __name__ == '__main__':
groups_size = {}
indices = []
for row in read_size_data('http://127.0.0.1:9200/_cat/indices?h=index,pri.store.size').split('\n'):
if row.strip() == '':
break # abort on empty rows
ind_name, size_h = re.split(' +', row)
m = re.match(r'([\w._\-]+)-\d{4}([-.])\d{2}(\1\d{2})?', ind_name)
if not m:
# skip non-patterned index
continue
ind_group = re.match(r'([\w._\-]+)-\d{4}([-.])\d{2}(\1\d{2})?', ind_name).group(1)
if size_h[-2:] in ('gb','mb','kb'):
size_b = int(float(size_h[:-2]) * SIZE_SUFFIX.get(size_h[-2:]))
elif size_h[-1:] == 'b':
size_b = int(float(size_h[:-1]) * SIZE_SUFFIX.get(size_h[-1:]))
else:
size_b = 0
indices.append((ind_group, ind_name, size_b))
if ind_group in groups_size:
groups_size[ind_group] += size_b
else:
groups_size[ind_group] = size_b
# default action: return as human readable text to stdout
print_formated_group_size(groups_size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment