清理es索引
依赖安装
pip install requests-html -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
如果安装失败,可能是Python解释器版本太高了,可以卸载掉,使用pycharm安装Python解释器
from requests_html import HTMLSession
import json
import re
# 请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36',
'Content-Type': 'application/json',
'Cookie': 'PLAY_SESSION=eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InVzZXJuYW1lIjoiYWRtaW4ifSwibmJmIjoxNjM1MzAyNzgwLCJpYXQiOjE2MzUzMDI3ODB9.49ngzEl44NPWD1Hrgd94_D6pvLg8cHpPl7HneYay8iA'
}
# 请求url
url = "http://10.10.1.100:19000/cluster_changes"
s = HTMLSession()
login_data = {"host": "http://10.10.1.100:9200"}
# cookie = {
# "Cookie": "PLAY_SESSION=eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InVzZXJuYW1lIjoiYWRtaW4ifSwibmJmIjoxNjM1MzAyNzgwLCJpYXQiOjE2MzUzMDI3ODB9.49ngzEl44NPWD1Hrgd94_D6pvLg8cHpPl7HneYay8iA"
# }
# 无格式要求直接传 login_data
# cookie写在post参数里面不生效
#res = s.post(url=url, headers=headers, cookies=cookie, data=json.dumps(login_data))
res = s.post(url=url, headers=headers, data=json.dumps(login_data))
# 渲染出一个Element对象的HTML内容
#print(res.html.html)
# 获取一个Element对象内的文本内容
#print(res.html.text)
# 把json转换为字典
res_dict = json.loads(res.html.html)
#print(type(res_dict))
list = res_dict['body']['indices']
#print(type(list))
url_del = "http://10.10.1.100:19000/overview/delete_indices"
#data_del = {"indices":"%{[app]}-2021.10.20","host":"http://10.10.1.100:9200"}
for i in range(len(list)-1):
#print(list[i])
# 使用正则匹配
pattern = re.compile(r'leliven-lecent-.*-2021.10.2\d')
#pattern = re.compile(r'leliven-lecent-.*-2021.10.2\d')
result = pattern.findall(list[i])
if len(result) != 0:
#print(result)
data_del = {"indices": result[0], "host": "http://10.10.1.100:9200"}
#print(data_del)
# 发送请求删除索引
#s.post(url=url_del,headers=headers,data=json.dumps(data_del))