Skip to content

Instantly share code, notes, and snippets.

@ed00m
Forked from EikeDehling/abusehub-reindex.py
Created March 20, 2020 19:13
Show Gist options
  • Save ed00m/8f9413d7ee2635360c13b4787fafd88b to your computer and use it in GitHub Desktop.
Save ed00m/8f9413d7ee2635360c13b4787fafd88b to your computer and use it in GitHub Desktop.

Revisions

  1. @EikeDehling EikeDehling created this gist Sep 8, 2017.
    38 changes: 38 additions & 0 deletions abusehub-reindex.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    #!bin/python

    from elasticsearch import Elasticsearch
    from datetime import datetime
    import time

    es = Elasticsearch()

    indices_state = es.cluster.state()['metadata']['indices']

    for source_index in sorted(indices_state.keys(), reverse=True):

    # Skip closed indices
    if indices_state[source_index]['state'] != 'open':
    print "Opening closed index {0}".format(source_index)
    es.indices.open(source_index)
    time.sleep(5)

    # Indices are called like this : "logstash-2016.10.07"
    try:
    date = datetime.strptime(source_index, 'logstash-%Y.%m.%d')
    except Exception, e:
    # Index name does not match pattern, skip
    continue
    destination_index = "logstash-{0}-{1}".format(date.year, date.month)

    print "Reindexing data in index {0} into {1}".format(source_index, destination_index)

    result = es.reindex({
    "source": {"index": source_index},
    "dest": {"index": destination_index}
    }, wait_for_completion=True, request_timeout=300)

    print result

    if result['total'] and result['took'] and not result['timed_out']:
    print "Seems reindex was successfull, going to delete the old index!"
    es.indices.delete(source_index, timeout='300s')