• python 脚本定时删除 elk索引


    脚本如下

    一、python 脚本如下

    #! /usr/bin/python
    # -*- coding=utf-8 -*-
    
    import urllib
    import urllib.request
    import re
    import datetime
    import time
    import codecs
    from urllib.request import urlopen
    import requests
    
    def match(urlGet):
        urlGet1 = urllib.request.urlopen(urlGet).read()
        #去除空格
        urlGet2 = codecs.getdecoder("unicode_escape")(urlGet1)[0]
        urlGet3 = re.sub(' +','|',urlGet2)
        #已换行为分隔符
        urlGet4 = urlGet3.split('
    ')
        return urlGet4
    
    def indexGet(urlGet4):
        for indexFull in urlGet4:
            try:
                #获取索引名称
                indexpattern = indexFull.split('|')[2::9]
                #正则匹配年月日
                pattern = re.compile(r'd{4}.d{1,2}.d{1,2}')
                #将j列表转成字符串,获取匹配的索引
                resMatch = pattern.findall("".join(indexpattern))
                # print(resMatch)
                #30天以前索引
                dataNow = datetime.date.today()
                daysBefore30 = dataNow - datetime.timedelta(days=30)
                dateFormat = daysBefore30.__format__('%Y.%m.%d')
                if dateFormat in resMatch:
                    httpDelete(dateFormat)
                else:
                    pass
            except:
                pass
    
    def httpDelete(urlDeleteDate):
        indexFonts = ['ngx-','tom-','hap-','switch-']
        for indexFont in indexFonts:
            urlDelete = 'http://192.168.1.198:9200/%s%s' % (indexFont,urlDeleteDate)
            print(urlDelete)
            # responseDelete = requests.delete(urlDelete)
    
    if __name__ == '__main__':
        urlGet = 'http://192.168.1.198:9200/_cat/indices'
        indexGet(match(urlGet))
    

    二、linux crontab 

    #每天凌晨1点执行。删除30天前的索引
    00 01 * * * /usr/bin/python /home/shell/delIndex.py
    
  • 相关阅读:
    122. 买卖股票的最佳时机 II
    45. 跳跃游戏 II
    134. 加油站
    55. 跳跃游戏
    714. 买卖股票的最佳时机含手续费
    121. 买卖股票的最佳时机
    860. 柠檬水找零
    开发环境安装合集(部分搬运)
    javascript的单例模式
    javascript 工厂模式
  • 原文地址:https://www.cnblogs.com/xzlive/p/11851549.html
Copyright © 2020-2023  润新知