我有许多以logstash-Year-Week格式索引的日志。那就是如果我想删除超过几周的索引,那么我怎样才能在elasticsearch中实现这一点。有没有一种简单,无缝的方式来做到这一点?删除elasticsearch中的旧索引
回答
馆长将是这里的理想匹配。 您可以在这里找到链接 - https://github.com/elastic/curator
像下面的命令应该只是罚款 -
curator --host <IP> delete indices --older-than 30 --prefix "twitter-" --time-unit days --timestring '%Y-%m-%d'
你可以保持这个在CRON用于偶尔去除指数。
你可以找到一些例子和文档在这里 - https://www.elastic.co/guide/en/elasticsearch/client/curator/current/examples.html
请看Curator,这是专门为这种用例开发的工具。
的样本命令,对于文档:
curator --host 10.0.0.2 delete indices --older-than 30 --time-unit days \
--timestring '%Y.%m.%d'
我使用bash脚本,只是改变了30你要保持
#!/bin/bash
# Zero padded days using %d instead of %e
DAYSAGO=`date --date="30 days ago" +%Y%m%d`
ALLLINES=`/usr/bin/curl -s -XGET http://127.0.0.1:9200/_cat/indices?v | egrep logstash`
echo
echo "THIS IS WHAT SHOULD BE DELETED FOR ELK:"
echo
echo "$ALLLINES" | while read LINE
do
FORMATEDLINE=`echo $LINE | awk '{ print $3 }' | awk -F'-' '{ print $2 }' | sed 's/\.//g' `
if [ "$FORMATEDLINE" -lt "$DAYSAGO" ]
then
TODELETE=`echo $LINE | awk '{ print $3 }'`
echo "http://127.0.0.1:9200/$TODELETE"
fi
done
echo
echo -n "if this make sence, Y to continue N to exit [Y/N]:"
read INPUT
if [ "$INPUT" == "Y" ] || [ "$INPUT" == "y" ] || [ "$INPUT" == "yes" ] || [ "$INPUT" == "YES" ]
then
echo "$ALLLINES" | while read LINE
do
FORMATEDLINE=`echo $LINE | awk '{ print $3 }' | awk -F'-' '{ print $2 }' | sed 's/\.//g' `
if [ "$FORMATEDLINE" -lt "$DAYSAGO" ]
then
TODELETE=`echo $LINE | awk '{ print $3 }'`
/usr/bin/curl -XDELETE http://127.0.0.1:9200/$TODELETE
sleep 1
fi
done
else
echo SCRIPT CLOSED BY USER, BYE ...
echo
exit
fi
yanb(另一个bash)的天#
#!/bin/bash
searchIndex=logstash-monitor
elastic_url=logging.core.k94.kvk.nl
elastic_port=9200
date2stamp() {
date --utc --date "$1" +%s
}
dateDiff(){
case $1 in
-s) sec=1; shift;;
-m) sec=60; shift;;
-h) sec=3600; shift;;
-d) sec=86400; shift;;
*) sec=86400;;
esac
dte1=$(date2stamp $1)
dte2=$(date2stamp $2)
diffSec=$((dte2-dte1))
if ((diffSec < 0)); then abs=-1; else abs=1; fi
echo $((diffSec/sec*abs))
}
for index in $(curl -s "${elastic_url}:${elastic_port}/_cat/indices?v" | grep -E " ${searchIndex}-20[0-9][0-9]\.[0-1][0-9]\.[0-3][0-9]" | awk '{ print $3 }'); do
date=$(echo ${index: -10} | sed 's/\./-/g')
cond=$(date +%Y-%m-%d)
diff=$(dateDiff -d $date $cond)
echo -n "${index} (${diff})"
if [ $diff -gt 1 ]; then
echo "/DELETE"
# curl -XDELETE "${elastic_url}:${elastic_port}/${index}?pretty"
else
echo ""
fi
done
如果您使用elasticsearch版本5.x,则需要安装策展人版本4.x。 您可以从documentation
中看到版本兼容性和安装步骤。安装完成。然后,只需运行命令
curator --config path/config_file.yml [--dry-run] path/action_file.yml
Curator提供干运行标志以输出Curator会执行的操作。输出将位于您在config.yml文件中定义的日志文件中。如果没有在config_file.yml中定义记录密钥,那么currator将输出到控制台。要删除索引,而不--dry-运行标志运行上述命令
配置文件config_file.yml是
---
client:
hosts:
- 127.0.0.1
port: 9200
logging:
loglevel: INFO
logfile: "/root/curator/logs/actions.log"
logformat: default
blacklist: ['elasticsearch', 'urllib3']
操作文件action_file.yml是
---
actions:
1:
action: delete_indices
description: >-
Delete indices older than 7 days (based on index name), for logstash-
prefixed indices. Ignore the error if the filter does not result in an
actionable list of indices (ignore_empty_list) and exit cleanly.
options:
ignore_empty_list: True
timeout_override:
continue_if_exception: False
disable_action: False
filters:
- filtertype: pattern
kind: prefix
value: logstash-
exclude:
- filtertype: age
source: name
direction: older
timestring: '%Y.%m.%d'
unit: days
unit_count: 7
exclude:
如果你想自动删除指数每周,每月等。然后就写这样
#!/bin/bash
# Script to delete the log event indices of the elasticsearch weekly
#This will delete the indices of the last 7 days
curator --config /path/config_file.yml /path/action_file.yml
的bash脚本把shell脚本这些文件夹中的一个:/etc/cron.daily, /etc/cron.hourly, /etc/cron.monthly or /etc/cron.weekly
和你的工作就完成了。
注意:请确保在您的配置和操作文件中使用正确的缩进。否则它将无法工作。
谢谢,这是针对策展人4.2这个答案的当前(2017)版本:) – chrisan
这就是策展人工作的方式! [Vineeth Mohan](https://stackoverflow.com/users/976646/vineeth-mohan)的答案已经过时,并且目前应该适用于大多数弹性搜索安装(其中5.x是最新的)。 – jonashackt
curator_cli delete_indices --filter_list '{"filtertype":"none"}'
将删除所有或过滤器:
--filter_list '[{"filtertype":"age","source":"creation_date","direction":"older","unit":"days","unit_count":13},{"filtertype":"pattern","kind":"prefix","value":"logstash"}]'
- 1. Elasticsearch禁用删除索引
- 2. 离线删除Elasticsearch索引
- 3. 删除Elasticsearch索引设置
- 4. Elasticsearch:删除索引中的重复项
- 5. 在Elasticsearch在索引中删除
- 6. 删除归档的Elasticsearch索引设置
- 7. 删除Elasticsearch中的旧条目
- 8. Elasticsearch:自动索引删除/到期
- 9. 使用Tire删除/删除ElasticSearch中的索引文档(使用ActsAsParanoid软删除)
- 10. 在不删除映射的情况下删除Elasticsearch索引
- 11. ElasticSearch:无法使用API删除索引,手动删除?
- 12. elasticsearch ttl force删除旧邮件
- 13. ElasticSearch异步删除? 200在删除Rails应用程序中的索引之后
- 14. 如何清除ElasticSearch索引?
- 15. 如何删除elasticsearch中以前索引的文档?
- 16. 如何删除elasticsearch索引中的所有文档
- 17. 从elasticsearch Head Plugin中删除类似命名的索引
- 18. 从python的ElasticSearch索引中删除文档
- 19. 如何删除Elasticsearch索引中的所有percolator查询
- 20. 如何删除旧的Hibernate搜索索引
- 21. 如何删除Python中的旧引用?
- 22. 从R中删除索引从矩阵中删除索引
- 23. 删除索引
- 24. 从ElasticSearch索引中删除一个文档,编号为
- 25. 从谷歌搜索删除旧网站索引
- 26. 带有Curator的AWS Elasticsearch 5.1 4.2.6 - 删除索引不起作用?
- 27. 如何删除ElasticSearch索引的特定分片
- 28. 如何限制Elasticsearch集群上的索引创建/删除?
- 29. 删除索引函数删除索引前的所有元素
- 30. 删除索引按索引名称和类型使用elasticSearch 2.3.3在java
这正是我一直在寻找。你有关于馆长应用的文件吗? –
这不适用于策展人v4或更新版本。它需要一个配置文件和一个动作文件,其中描述了策展人的动作。 –
查看@ sachchit-bansal作为策展人的回答4.2范例 – chrisan