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)
Elasticsearch索引自动删除的更多相关文章
- Elasticsearch索引自动套用模板
公司ELK系统目前的设置是每月自动将日志信息记录至新的索引中,将日志数据按月分索引保存,在扩展的ELK架构中,利Logstash对接rabbitmq,获取日志消息,自动持久化至Elasticsearc ...
- elasticsearch索引自动清理
一 es 基本操作 查看所有的索引文件: curl -XGET http://localhost:9200/_cat/indices?v GET /_cat/indices?v DELETE /fi ...
- Remove 以及dorp做实验验证MongoDB删除文档后索引是否会自动删除
下面是实验步骤: > db.things.find(){ "_id" : ObjectId("5652d71a1524dc14663060e8"), &q ...
- Elasticsearch 索引、更新、删除文档
一.Elasticsearch 索引(新建)一个文档的命令: curl XPUT ' http://localhost:9200/test_es_order_index/test_es_order_t ...
- MongoDB自动删除过期数据--TTL索引
前序: 由于公司业务需求,对于3个月前的过期数据需要进行删除动作,以释放空间和方便维护 本来想的是使用crontab写个脚本定时执行,但是看到Mongo本身就有自动删除过期数据的功能,所以还是用一 ...
- Elasticsearch索引(company)_Centos下CURL增删改
目录 返回目录:http://www.cnblogs.com/hanyinglong/p/5464604.html 1.Elasticsearch索引说明 a. 通过上面几篇博客已经将Elastics ...
- ES3:ElasticSearch 索引
ElasticSearch是文档型数据库,索引(Index)定义了文档的逻辑存储和字段类型,每个索引可以包含多个文档类型,文档类型是文档的集合,文档以索引定义的逻辑存储模型,比如,指定分片和副本的数量 ...
- Elasticsearch索引和文档操作
列出所有索引 现在来看看我们的索引 GET /_cat/indices?v 响应 health status index uuid pri rep docs.count docs.deleted st ...
- windows系统中 利用kibana创建elasticsearch索引等操作
elasticsearch之借用kibana平台创建索引 1.安装好kibana平台 确保kibana以及elasticsearch正常运行 2.打开kibana平台在Dev Tools 3.创建一个 ...
随机推荐
- SSM框架开发web项目系列(二) MyBatis真正的力量
前言 上篇SSM框架环境搭建篇,演示了我们进行web开发必不可少的一些配置和准备工作,如果这方面还有疑问的地方,可以先参考上一篇“SSM框架开发web项目系列(一) 环境搭建篇”.本文主要介绍MyBa ...
- [flask/python/web] 解析flask web开发(Miguel著)一书第11章主页不显示博文表单的问题
---------------------------------------------以下内容2017.7.14更新---------------------------------------- ...
- null id in entry (don't flush the Session after an exception occurs) 解决方法
最近在学习基于ssh的注解的系统,然后在实现往数据库增加记录时可以增加第一个,第二个就报错,在网上查了很多资料,大多都是 该异常信息是在提示我们没有为数据中的非空字段设置值. 然后就一直没有明白 明明 ...
- 《Linux命令行与shell脚本编程大全》第二十二章 gawk进阶
gawk是一门功能丰富的编程语言,你可以通过它所提供的各种特性来编写好几程序处理数据. 22.1 使用变量 gawk编程语言支持两种不同类型的变量: 内建变量和自定义变量 22.1.1 内建变量 ga ...
- dijkstra最小花费
//Gang #include<iostream> #include<cstring> #include<algorithm> #include<cstdio ...
- 初识分布式计算:从MapReduce到Yarn&Fuxi
这些年,云计算.大数据的发展如火如荼,从早期的以MapReduce为代表的基于文件系统的离线数据计算,到以Spark为代表的内存计算,以及以Storm为代表的实时计算,还有图计算等等.只要数据规模 ...
- 51Nod 1110 距离之和最小 V3 中位数 思维
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i].点P到点P[i]的带权距离 = 实际距离 ...
- ELK介绍
为什么用到ELK: 一般我们需要进行日志分析场景:直接在日志文件中 grep.awk 就可以获得自己想要的信息.但在规模较大的场景中,此方法效率低下,需要集中化的日志管理,所有服务器上的日志收集汇总. ...
- kali git 环境配置
kali 配置很简单,输入apt-get install git 我输入时返回了错误,执行命令dpkg --configure -a 就行了
- SpringBoot多数据源配置
准备环境: jdk1.8 eclipse tomcat8.0 第一步:在配置文件添加如下信息: spring.datasource.primary.url=jdbc:mysql://localhost ...