Created
          June 17, 2016 22:18 
        
      - 
      
- 
        Save tcotav/93b731eefee532f12e4840f167952ae6 to your computer and use it in GitHub Desktop. 
    simple python script to delete ELK stack indices older than N days. couldn't use curator because of f'd up ssl situation :D
  
        
  
    
      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/python | |
| import requests | |
| import json | |
| import datetime | |
| """ | |
| Delete any indices older than N days | |
| """ | |
| base_url="https://your-url/" | |
| DELETE_OLDER_THAN=7 | |
| def processIndex(idxName, targetDT): | |
| # convert idxName to datetime | |
| idxDate=datetime.datetime.strptime(idxName[9:],"%Y.%m.%d") | |
| # check if < targetDT | |
| if idxDate < targetDT: | |
| # if yes, run delete | |
| print idxName | |
| requests.delete("%s/%s" % (base_url, idxName), verify=False) | |
| targetDT = datetime.datetime.now() - datetime.timedelta(days=DELETE_OLDER_THAN) | |
| # get list of indices in last N + 10 | |
| r=requests.get("%s/%s" % (base_url, "_all/_stats"), verify=False) | |
| ddata= r.json() | |
| for k in ddata["indices"]: | |
| # logstash-2016.06.17 | |
| if k[:9] == "logstash-": | |
| processIndex(k, targetDT) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment