curator.py

elasticsearch curator to delete index by pattern

1

Votes

!/usr/bin/env python

""" date: 2018.01.28 author: hadasgo description: delete elasticsearch eslog cluster indexes (maintain 14 days) dependencies: elasticsearch-curator """

import os import sys import argparse import requests from datetime import datetime

webhook_url = "https://web-hook-here" date = datetime.now().strftime("%Y-%m-%dT%H%M%S") parser = argparse.ArgumentParser(description='Process curator arguments..') parser.add_argument('-d', dest='daysto_preserve', type=int, action='store', help='Remove indexes older than 14 days', default=14) parser.add_argument('-e', dest='elb_endpoint', action='store', help='ELB endpoint to access cluster with') parser.add_argument('-l', dest='log_path', action='store', help='Logs of the operations to be stored at', default='/data/curator-scripts/') parser.add_argument('--dry-run', dest='dry_run', action='store_true', help='Whether to run in dry run mode') args = parser.parse_args()

if the argument is not int, python script will throw exception so consider to try and catch

curator_log_file = os.path.join(args.log_path, os.path.basename(file)+"{}.log".format(date))

if args.daysto_preserve < 14: args.daysto_preserve = 14 print("input of the days to preserve was too low, defaulting to 14")

dry_run_key = '--dry-run' if args.dry_run else '' command = "curator --logfile {} {}"\ " --host {} delete indices --older-than {}"\ " --time-unit days --timestring '%Y-%m-%d'"\ " --prefix logs".format(curator_log_file, dry_run_key, args.elb_endpoint, args.daysto_preserve)

os.system(command) payload = None try: with open(curator_log_file, 'r') as rf: if dry_run_key=="--dry-run": for line in rf: if "DRY RUN MODE. No changes will be made." in line: payload = "DRY RUN MODE passed successfully" break else: for line in rf: if "Job completed successfully." in line: payload = "Curator finished successfully, \n" + prevline break elif "No indices matched provided args:" in line: payload = "Curator found no indices to delete" break prevline = line

except Exception as e: print(e)

json_payload = {'text' : payload} requests.post(url=webhook_url, json=json_payload)

Vote Here

You must earn at least 1 vote on your snippets to be allowed to vote

Terms Of Use

Privacy Policy

Featured snippets are MIT license

Gears & Masters

Advertise

DevOpsnipp.com © 2020

medium.png