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)