ElasticSearch 关闭索引

 ElasticSearch  数据库作为日志服务器收集日志,随着时间,日志量增加,集群会变得越来越慢,我们就需要去关闭历史的索引,每天去执行计划任务关闭索引,如果日志量过大也可以删除日志

# -*- 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()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值