python 脚本
import os
import datetime
def getXDaysAgo(x):
now = datetime.datetime.now()
xdaysago = (now - datetime.timedelta(x)).strftime('%Y%m%d')
return xdaysago
def getIndex():
os.system('curl -XGET "http://es01:9200/_cat/indices?v" > /tmp/newly')
def delIndex(days):
with open('/tmp/newly','r') as f :
for line in f.readlines():
index = line.strip().split()[2]
if str(getXDaysAgo(days)) in index:
command = "curl -XDELETE http://es01:9200/%s" % index
print('delete %s' % index)
os.system(command)
else:
pass
getIndex()
delIndex(3) #要清理的日期
crontab增加
0 1 * * * cd /wdzj/script;/usr/bin/python clean_eslogs.py >>es.log