简介

脚本分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)

Elasticsearch索引自动删除的更多相关文章

  1. Elasticsearch索引自动套用模板

    公司ELK系统目前的设置是每月自动将日志信息记录至新的索引中,将日志数据按月分索引保存,在扩展的ELK架构中,利Logstash对接rabbitmq,获取日志消息,自动持久化至Elasticsearc ...

  2. elasticsearch索引自动清理

    一 es 基本操作 查看所有的索引文件:  curl -XGET http://localhost:9200/_cat/indices?v GET /_cat/indices?v DELETE /fi ...

  3. Remove 以及dorp做实验验证MongoDB删除文档后索引是否会自动删除

    下面是实验步骤: > db.things.find(){ "_id" : ObjectId("5652d71a1524dc14663060e8"), &q ...

  4. Elasticsearch 索引、更新、删除文档

    一.Elasticsearch 索引(新建)一个文档的命令: curl XPUT ' http://localhost:9200/test_es_order_index/test_es_order_t ...

  5. MongoDB自动删除过期数据--TTL索引

      前序: 由于公司业务需求,对于3个月前的过期数据需要进行删除动作,以释放空间和方便维护 本来想的是使用crontab写个脚本定时执行,但是看到Mongo本身就有自动删除过期数据的功能,所以还是用一 ...

  6. Elasticsearch索引(company)_Centos下CURL增删改

    目录 返回目录:http://www.cnblogs.com/hanyinglong/p/5464604.html 1.Elasticsearch索引说明 a. 通过上面几篇博客已经将Elastics ...

  7. ES3:ElasticSearch 索引

    ElasticSearch是文档型数据库,索引(Index)定义了文档的逻辑存储和字段类型,每个索引可以包含多个文档类型,文档类型是文档的集合,文档以索引定义的逻辑存储模型,比如,指定分片和副本的数量 ...

  8. Elasticsearch索引和文档操作

    列出所有索引 现在来看看我们的索引 GET /_cat/indices?v 响应 health status index uuid pri rep docs.count docs.deleted st ...

  9. windows系统中 利用kibana创建elasticsearch索引等操作

    elasticsearch之借用kibana平台创建索引 1.安装好kibana平台 确保kibana以及elasticsearch正常运行 2.打开kibana平台在Dev Tools 3.创建一个 ...

随机推荐

  1. 微信官方团队放出了UI库,看来以后前端还要学WeChatUI了,哈哈

    已经在github上发布,网址如下:https://github.com/weui/weui

  2. 【原创】通过搬瓦工vps搭建SS环境,供学习用

    博主前段时间处于某些不可告人的目的,但又限于各类科学工具被禁的窘境,用搬瓦工的vps搭建了次SS环境,现在就来回顾并不知廉耻的传授下经验. 第一步:购买vps 1.登录官网 https://bwh1. ...

  3. 【转】如何使用Git上传本地项目到github?(mac版)

    原文链接:http://www.cnblogs.com/lijiayi/p/pushtogithub.html 在此假设你已经在 github 上创建好了一个项目,像这样: 并且你已经完成了自己的项目 ...

  4. 《Linux命令行与shell脚本编程大全》第二十一章 sed进阶

    本章介绍一些sed编辑器提供的高级特性. 21.1 多行命令 按照之前的知识,所有的sed编辑器命令都是针对单行数据执行操作的. 在sed编辑器读取数据流时,它会基于换行符的位置将数据分成行,一次处理 ...

  5. 为什么win记事本编辑的shell在linux中运行会报错

    结论:win记事本使用的格式有别于linux,二者不可混用.linux使用一个叫vi的编辑器. 解决办法:使用vi命令建立文件,在其中敲shell.命令:vi  > filename   (敲完 ...

  6. createElement的应用

    原生js表单生成列表实现原理 这里用到的一些方法有 insertBefore() createElement() appendChild() removeChild() and so on~~ < ...

  7. 简介CentOS与 Ubuntu的不同

    1.关于登录用户 centos可以使用root登录  ubuntu不能使用root登录 centos普通用户默认不能通过sudo取得root权限执行命令, ubuntu可以,centos需要sudo时 ...

  8. Python处理json字符串转化为字典

    有一个需求,需要用python把json字符串转化为字典 inp_str = " {'k1':123, 'k2': '345','k3','ares'} " import json ...

  9. 使用oracle DB_LINK的一个注意点

    今天使用db_link的时候遇到了个有趣的问题,和大家分享一下; 环境:Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bi ...

  10. Loadrunner 读取文件

    char buffer[1000]; long file_stream; char * filename = "d:\log.txt"; file_stream=fopen(fil ...