ES 索引操作

ES 索引操作

# 查询索引,并且过滤掉 es 系统的索引, dt_m 为查询过滤的条件
"curl -k -u %s:%s http://%s:9200/_cat/indices?v | grep green | grep %s | grep -v '.monitoring\|.watch\|.kibana\|.security'  |awk '{print $3}'" %(es_info['username'], es_info['password'], es_info['host'],dt_m)

# 关闭索引
"curl -XPOST -k -u %s:%s http://%s:9200/%s/_close/" % (es_info['username'], es_info['password'], es_info['host'], index_name)

# 打开索引
"curl -XPOST -k -u %s:%s http://%s:9200/%s/_open/" % (es_info['username'], es_info['password'], es_info['host'], index_name)

# 删除索引
"curl -XDELETE -k -u %s:%s http://%s:9200/%s" % (es_info['username'], es_info['password'], es_info['host'], index_name)

python 脚本删除索引

# -*- encoding: utf-8 -*-

import requests, paramiko, datetime
from dateutil.relativedelta import relativedelta

# 企业微信机器人,发送消息
def msg(text):
    # 调用机器人
    # 机器人
    rebot = 'xxxxxxxx'
    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 get_file_list(hostname, port, username, key_name, command):
    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,
                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()  # 关闭连接

    return file_list

if __name__ == '__main__':
    # prometheus 主机信息
    # host_info = {'hostname': 'xxx.xxx.xxx', 'port': 22, 'username': 'root', 'password': 'xxx'}
    host_info = {'hostname': 'xxx.xxx.xxx.xxx', 'port': 22, 'username': 'root', 'password': './keys/xxxxxxxxx'}
    es_info = {'username': 'xxxx', 'password': 'xxxxx', 'host': 'xxx.xxx.xxx.xxx'}
    # 获取过期月日期
    expriy_month = 1



    dt_m = (datetime.date.today() - relativedelta(months=expriy_month)).strftime('%Y.%m')
    print(dt_m)

    # 获取需要删除的index key
    command = "curl -k -u %s:%s http://%s:9200/_cat/indices?v | grep green | grep %s | grep -v '.monitoring\|.watch\|.kibana\|.security'  |awk '{print $3}'" %(es_info['username'], es_info['password'], es_info['host'],dt_m)

    index_list = get_file_list(host_info['hostname'], host_info['port'], host_info['username'], host_info['password'], command)
    if index_list:
        print('需要关闭的索引: ' + 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['username'], es_info['password'], es_info['host'], index_name)
            print(command)
            get_file_list(host_info['hostname'], host_info['port'], host_info['username'], host_info['password'],
                          command)
        # 企业微信机器人
        #text = '#### Elasticsearch 关闭' + dt_m + ' 的索引成功!!!'
        #msg(text)
    else:
        print('获取索引失败')
        # 企业微信机器人
        #text = '#### Elasticsearch 关闭' + dt_m + ' 的索引失败!!!'
        #msg(text)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值