
正文
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 requestsdef match(urlGet):
urlGet1 = urllib.request.urlopen(urlGet).read()
#去除空格
urlGet2 = codecs.getdecoder("unicode_escape")(urlGet1)[0]
urlGet3 = re.sub(' +','|',urlGet2)
#已换行为分隔符
urlGet4 = urlGet3.split('\n')
return urlGet4def 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:
passdef 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






