zoukankan      html  css  js  c++  java
  • 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
    
  • 相关阅读:
    MySql 学习之 一条更新sql的执行过程
    MySql 学习之 一条查询sql的执行过程
    VUE基本介绍
    ESMAScript6基本介绍
    npm
    tensorflow2.0 评估函数
    网页引入mathjax,latex
    Veno File Manager
    tensorflow 测量工具,与自定义训练
    tensorflow自定义网络结构
  • 原文地址:https://www.cnblogs.com/xzlive/p/11851549.html
Copyright © 2011-2022 走看看