随笔 - 911  文章 - 5  评论 - 94  阅读 - 243万

ElasticSearch查看删除关闭索引

curl -XDELETE 'http://10.1.2.2:9200/iis_log_2019-07'     #删除名为/iis_log_2019-07的索引

curl -XPOST 'http://10.1.2.2:9200/iis_log_2019-07/_close/'   #关闭名为/iis_log_2019-07的索引(_open打开)

curl  10.1.2.2:9200/_cat/indices/iis_log* #查看iis_log开头的所有索引

curl  10.1.2.2:9200/_cat/indices/iis_log_2018-07' #查看iis_log_2018-07的索引

复制代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime,os
from dateutil.relativedelta import relativedelta

#关闭前第3个月的索引
def index_close(indexname,hmonths):
    dt_m = (datetime.date.today() - relativedelta(months=hmonths)).strftime('%Y-%m')
    iname = '%s_%s' % (indexname,dt_m)
    url = 'http://10.1.2.2:9200/%s/_close/' % iname
    print(url)
    m = os.popen('curl -XPOST %s' % url)
    print(m.readlines())

# index_close('iis_logl',3)

#删除前第12个月的索引
def index_delete(indexname,hmonths):
    dt_m = (datetime.date.today() - relativedelta(months=hmonths)).strftime('%Y-%m')
    iname = '%s_%s' % (indexname,dt_m)
    url = 'http://10.1.2.2:9200/%s' % iname
    print(url)
    m = os.popen('curl -XDELETE %s' % url)
    print(m.readlines())

index_delete('iis_log',12)
复制代码

 

复制代码
#关闭前1个月的索引,索引以天为单位产生,如sec_mail_2020-04-28)
def index_close_days(indexname,nmonths):
        dt_m = (datetime.date.today() - relativedelta(months=nmonths)).strftime('%Y-%m')
        dt_n = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        y,m = dt_m.split('-')
        days = (calendar.monthrange(int(y),int(m)))[1]
        for d in range(days):
            d = str(d+1).rjust(2,'0')
            iname = '%s_%s-%s' % (indexname,dt_m,d)
            url = 'http://10.1.2.2:9200/%s/_close/' % iname
            # print(url)
            rs = os.popen('curl -XPOST %s' % url)
            with open(logfile,'a') as fw:
                fw.write('%s\n%s\n%s\n'% (dt_n,url,rs.read()))

#删除前3个月的索引,索引以天为单位产生,如sec_mail_2020-04-28)
def index_delete_days(indexname,nmonths):
        dt_m = (datetime.date.today() - relativedelta(months=nmonths)).strftime('%Y-%m')
        dt_n = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        y,m = dt_m.split('-')
        days = (calendar.monthrange(int(y),int(m)))[1]
        for d in range(days):
            d = str(d+1).rjust(2,'0')
            iname = '%s_%s-%s' % (indexname,dt_m,d)
            url = 'http://10.1.2.2:9200/%s' % iname
            # print(url)
            rs = os.popen('curl -XDELETE %s' % url)
            with open(logfile,'a') as fw:
                fw.write('%s\n%s\n%s\n'% (dt_n,url,rs.read()))

index_close_days('sec_mail',1)
index_delete_days('sec_mail',3)
复制代码

 

posted on   momingliu11  阅读(6965)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2014-07-17 新建共享,NTFS权限设置
2013-07-17 MDT概念说明
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示