zoukankan      html  css  js  c++  java
  • Elasticsearch的一个清理Index脚本

    前言

    • 公司在刚开始进行elk调研的时候,肯定会考虑到磁盘的增长,导致会自动清理掉一部分时间久远的日志内容

    使用python编写这个脚本

    • Retention为想最多保存的天数,可以根据自己的集群磁盘容量进行合适的调整
    #!/usr/bin/env python
    # -*- conding:utf-8 -*-
    # Author : QiuMeng
    
    
    import urllib
    import urllib2
    import json
    import datetime
    
    Retention = 30
    
    today = datetime.datetime.now()
    
    def http_delete(index_name):
        url = 'http://127.0.0.1:9200/' + index_name
        request = urllib2.Request(url)
        request.get_method = lambda:'DELETE'
        request = urllib2.urlopen(request)
        return request.read()
    
    home_url = 'http://127.0.0.1:9200/_cat/indices'
    response = urllib2.urlopen(home_url)
    index_list = response.read()
    
    file = open('Index_List','w+')
    file.write(index_list)
    
    with open('Index_List','r') as f:
        for line in file.readlines():
            for i in line.split('	'):
            index_date = i.split()[2][-10:]
            if len(index_date) == 10:
                date_day = datetime.datetime.strptime(index_date,'%Y.%m.%d')
                diff = (today-date_day).days
                if diff > Retention:
                    index_name = i.split()[2]
                    http_delete(index_name)
    
    
    
  • 相关阅读:
    排序算法 之 冒泡排序 插入排序 希尔排序 堆排序
    DataStructure之线性表以及其实现
    使用可重入函数进行更安全的信号处理
    内存经济学
    电脑通用技能
    循环套餐的逻辑
    占用了多少内存
    索引的用法
    电脑的眼缘
    字符串积木
  • 原文地址:https://www.cnblogs.com/forsaken627/p/6510897.html
Copyright © 2011-2022 走看看