ElasticSearch 数据库作为日志服务器收集日志,随着时间,日志量增加,集群会变得越来越慢,我们就需要去关闭历史的索引,每天去执行计划任务关闭索引,如果日志量过大也可以删除日志文章来源地址https://www.toymoban.com/news/detail-599301.html
# -*- encoding: utf-8 -*-
"""
@File : es_index_close.py
"""
import requests, paramiko, datetime
from dateutil.relativedelta import relativedelta
from time import sleep
# 企业微信机器人,发送消息
def msg(text):
# 调用机器人
# 企业微信机器人
rebot = 'xxxxxx'
headers = {'Content-Type': 'application/json;charset=utf-8'}
api_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=" + rebot
data = {
"msgtype": "markdown",
"markdown": {
"content": text,
}
}
r = requests.post(api_url, headers=headers, json=data)
print(r.text)
# 得到一个Overssh的返回值
def exec_command(hostname, port, username, key_name, command):
file_list = ""
try:
ssh = paramiko.SSHClient() # 创建SSH对象
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 允许连接不在know_hosts文件中的主机
# ssh.connect(hostname=hostname, port=port, username=username, password=key_name) # 连接服务器
ssh.connect(hostname=hostname, port=port, username=username, timeout=5,
pkey=paramiko.RSAKey.from_private_key_file(key_name)) # 连接服务器
stdin, stdout, stderr = ssh.exec_command(command)
file_list = str(stdout.read(), encoding="utf-8")
ssh.close() # 关闭连接
except:
print("exec_command error !!!")
return file_list
# 获取日期格式,根据不同的index 类型
def get_expried_date(index_type,expried_date):
# 获取过期月日期格式类型
if index_type == 1:
dt_m = (datetime.date.today() - relativedelta(days=expried_date)).strftime('%Y.%m.%d')
elif index_type == 2:
dt_m = (datetime.date.today() - relativedelta(days=expried_date)).strftime('%Y%m%d')
# print(dt_m)
return dt_m
def close_es_index_v2(es_list):
for es_instance in es_list:
es_info = es_list[es_instance]
print('#### 开始关闭' + es_instance + ' 的索引#### ')
expried_date = get_expried_date(es_info['idx_t'], es_info['expried_date'])
command = "curl -k -u %s:%s http://%s:9200/_cat/indices/*%s*? | grep open | awk '{print $3}'" \
%(es_info['es_user'], es_info['es_pw'], es_info['es_host'],expried_date)
# print(command)
index_list = exec_command(es_info['host'], es_info['port'], es_info['host_user'], es_info['host_pw'], command)
if index_list:
index_list = index_list.splitlines()
for index_name in index_list:
command = "curl -XPOST -k -u %s:%s http://%s:9200/%s/_close/" \
%(es_info['es_user'], es_info['es_pw'], es_info['es_host'],index_name)
print(command)
exec_command(es_info['host'], es_info['port'], es_info['host_user'], es_info['host_pw'], command)
text = '#### Elasticsearch 关闭 ' + es_instance + ' 的索引成功!!! #### '
else:
text = '#### Elasticsearch 关闭 ' + es_instance + ' 未获取到索引值!!! #### '
print(text)
msg(text)
def main():
'''
es_list 中 host ssh 跳板机ip
es_list 中 host_pw ssh 跳板机 sshkey
es_list 中 idx_t 1代表2021.08; 2代表202108
'''
es_list = {
'ES_host':
{'host': 'xxx', 'port': 22, 'host_user': 'root', 'host_pw': './keys/xxx_rsa',
'es_host': 'xxx', 'es_user': 'admin', 'es_pw': 'xxx', 'idx_t': 2, 'expried_date': 14},
}
close_es_index_v2(es_list)
if __name__ == '__main__':
main()
文章来源:https://www.toymoban.com/news/detail-599301.html
到了这里,关于ElasticSearch 关闭索引的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!