简介

脚本分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. 初学PHP心得(第一天)

    我是PHP初学者,听说女生挺适合学这门语言的.所以,我就下定决心,来好好的探究下它,希望它能成为我开启IT道路的第一道关卡. 今天心血来潮,来记录下一天的成果和收获吧.既然想法有了,那就要去实现它.于 ...

  2. C#开发Windows窗体应用程序的步骤

    使用C#开发应用程序时,一般包括创建项目.界面设计.设置属性.编写程序代码.保存项目.程序运行等6个步骤. 1.创建项目 在Visual Studio2017开发环境中选择“文件”→“新建”→“项目” ...

  3. 机器学习算法 - 最近邻规则分类KNN

    上节介绍了机器学习的决策树算法,它属于分类算法,本节我们介绍机器学习的另外一种分类算法:最近邻规则分类KNN,书名为k-近邻算法. 它的工作原理是:将预测的目标数据分别跟样本进行比较,得到一组距离的数 ...

  4. noip考前模板大整理

    //归并排序求逆序对 #include<bits/stdc++.h> #define ll long long using namespace std; ]; ll ans; ]; voi ...

  5. 写出优雅又地道的pythonic代码(转自网络)

    本文是Raymond Hettinger在2013年美国PyCon演讲的笔记(视频, 幻灯片). 示例代码和引用的语录都来自Raymond的演讲.这是我按我的理解整理出来的,希望你们理解起来跟我一样顺 ...

  6. 导弹拦截(pascal)

    导弹拦截 [问题描述] 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕 ...

  7. 真正的精通Java是种什么样的境界?

    会在不适合使用java的地方不用java! 作为一名软件开发者,要追求的,应该是不断地提升自己分析问题把握事物关键点,实事求是地给出切实可行且能"一剑封喉"的优雅解决方案的能力,再 ...

  8. Angular页面加载闪现解决方案 ng-cloak

    在做Angular项目时,经常会遇见在浏览器上闪烁表达式({{ express }} ),及模块(div)的闪烁,会闪现/闪烁隐藏的数据,之前用过vue.js,可以通过v-clock解决,同理Angu ...

  9. Vue 事件

    一.事件冒泡 方法一.使用event.cancelBubble = true来阻止冒泡 <div @click="show2()"> <input type=&q ...

  10. zeppelin0.7.3源码编译

    操作系统: Centos7.X Python版本: Python2.7 Maven版本:3.1.* Git:1.8.3.* JAVA:java1.7+ node npm bower grunt 每次执 ...