用python定时清理es的内容,做elk中日志定期删除

1、在linux中安装好python,非常简单,这里不多做赘述。
2、在linux创建个文件:clear_es.py 并写入内容:

import os
import time

localtime = time.localtime(time.time())

dataStr = os.popen('curl 10.233.47.105:9200/_cat/indices').read()
dataArr = dataStr.split('\n')

codeArr = []

for dataOne in dataArr:
    dataOneArr = dataOne.split( )
    if len(dataOneArr) == 10 :
        one = dataOneArr[3]
        codeArr.append(one)
print(localtime)
print('start clear')
for root, dirs, files in os.walk('/var/openebs/local/pvc-c28cca68-a482-4d8f-b859-5abff8a939a2'):
    for dir in dirs:
        if(root.endswith('indices')):
            if(dir not in codeArr):
                print('delete: ' +  root + '/' + dir)
                print(os.popen('rm -rf ' + root + '/' + dir ).read())

3、在linux定时任务中加入:crontab -e

00 3 * * *  /usr/bin/python /root/clear_es/clear_es.py >> /root/clear_es/clear.log  2>&1

即可。

注:再来一个定时任务,加入这个脚本一起清理更佳

30 2 * * *  /opt/clearEsLog/clearEsLog.sh >> /opt/clearEsLog/clear.log  2>&1
#! /bin/sh

clearDate=`date -d '3 days ago' +'%Y.%m.%d'`

clearDate2=*`date -d '3 days ago' +'%Y-%m-%d'`*

echo 10.233.47.105:9200/$clearDate2 &&

echo 10.233.47.105:9200/*-$clearDate &&

curl -XDELETE  10.233.47.105:9200/$clearDate2   &&

curl -XDELETE  10.233.47.105:9200/*-$clearDate

注意设置你自己的es的地址!

你可能感兴趣的:(Python,ElasticSearch,python,elasticsearch,elk)