Elasticsearch索引自动删除

简介

脚本分2部分,1部分查找符合条件的索引名,2脚本调用1脚本,进行删除操作

脚本

查找符合条件的,默认大于30天

# coding:utf-8

__author__ = 'Jipu FANG'

from elasticsearch import  Elasticsearch
import re
import time
import datetime

now = time.localtime()
data1 = datetime.datetime(now[0], now[1], now[2])

es=Elasticsearch("http://192.168.30.135:9200")


res = es.cat.indices()

l = res.strip().split()
def dindex(day=30):
    index = []
    for i in l:
        if re.search('\d+\.\d+\.\d+$', i):
            itime = time.strptime(re.findall('\d+\.\d+\.\d+$', i)[0], "%Y.%m.%d")
            data2 = datetime.datetime(itime[0], itime[1], itime[2])
            d = (data1-data2).days
            if int(d) > int(day):
                index.append(i)
    return index

if __name__ == '__main__':
    print dindex()

对符合条件的索引,进行删除操作

# coding:utf-8

__author__ = 'Jipu FANG'

import requests
import json
import time
from multiprocessing.dummy import Pool as ThreadPool
import  re
import indexs

'''
delect index    url:"http://192.168.30.135:9200/app-2017.05.16"  headers:'Content-Type: application/json' data:{"query": {"match_all":{}}}'
select log  curl: "http://192.168.30.135:9200/_search"  headers:'Content-Type: application/json' data:{"query": {"match": {"message": {"query": "ERROR|77" }}}'
'''

# request API
class ES_API:
    def __init__(self, url, data, headers):
        self.url=url
        self.data=data
        self.headers=headers

    def delete(self):
        r = requests.delete(url=self.url, data=json.dumps(self.data), headers=self.headers)
        v=r.text
        print(v)

    def post(self):
        r = requests.post(url=self.url, data=json.dumps(self.data), headers=self.headers)
        v=r.text
        print(v)

# 删除索引,day保留多少天
def delete_index(day):
    for i in indexs.dindex(day):
        url = r"http://192.168.30.135:9200/%s" %(i)
        headers = {'Content-Type':'application/json'}
        data = {"query": {"match_all":{}}}
        C=ES_API(url, data, headers)
        C.delete()
        time.sleep(3)
    return "Delete indexs OK!"

# 关闭索引,day保留多少天,当索引处于关闭状态,资源占用比较少
def close_index(day):
    for i in indexs.dindex(day):
        url = r"http://192.168.30.135:9200/%s/_close?pretty" %(i)
        headers = {'Content-Type':'application/json'}
        data = {}
        C=ES_API(url, data, headers)
        C.post()
        time.sleep(3)
    return "index status close ok!"


delete_index(30)
time.sleep(60)
close_index(15)

转载于:https://www.cnblogs.com/GXLo/p/7405631.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值