2015-10-30 63 views
16

我有许多以logstash-Year-Week格式索引的日志。那就是如果我想删除超过几周的索引,那么我怎样才能在elasticsearch中实现这一点。有没有一种简单,无缝的方式来做到这一点?删除elasticsearch中的旧索引

回答

19

馆长将是这里的理想匹配。 您可以在这里找到链接 - 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

+0

这正是我一直在寻找。你有关于馆长应用的文件吗? –

+4

这不适用于策展人v4或更新版本。它需要一个配置文件和一个动作文件,其中描述了策展人的动作。 –

+0

查看@ sachchit-bansal作为策展人的回答4.2范例 – chrisan

6

请看Curator,这是专门为这种用例开发的工具。

的样本命令,对于文档:

curator --host 10.0.0.2 delete indices --older-than 30 --time-unit days \ 
    --timestring '%Y.%m.%d' 
10

我使用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 
0

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  
13

如果您使用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和你的工作就完成了。

注意:请确保在您的配置和操作文件中使用正确的缩进。否则它将无法工作。

+1

谢谢,这是针对策展人4.2这个答案的当前(2017)版本:) – chrisan

+0

这就是策展人工作的方式! [Vineeth Mohan](https://stackoverflow.com/users/976646/vineeth-mohan)的答案已经过时,并且目前应该适用于大多数弹性搜索安装(其中5.x是最新的)。 – jonashackt

0
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"}]' 
相关问题