summaryrefslogtreecommitdiff
path: root/status-autoclear.py
blob: 23b88ff51f2f64807d1bad8401768f0262cae897 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3

# This script checks on the status of the cluster
# and if it's in a failed state due to a shard
# collision on allocation for the creation of
# an index, it will automatically clear the
# index.

from elasticsearch import Elasticsearch
from elasticsearch.exceptions import RequestError
import requests
import json
from datetime import datetime,timedelta

import sys

ES_HOST="elasticsearch-production"
ES_PORT=9200

es = Elasticsearch([{'host':ES_HOST,'port':ES_PORT}])

health = es.cluster.health()

if health['status'] == 'red':
    try:
        alloc = es.cluster.allocation_explain()
        # ferret out the stuck index and delete it
        troublemaker = alloc["index"]
        if alloc['unassigned_info']['reason'] == "ALLOCATION_FAILED":
            # let's try to remove it
            es.indices.delete(index=troublemaker, timeout=60)
        else:
            # haven't seen it before... dump error and get a human
            print(alloc)
            sys.exit(1)
    except RequestError as re:
        # something we don't know how to handle... get a human
        raise re
        sys.exit(1)

# either not red, or the delete worked
sys.exit(0)