2015-10-30 38 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スクリプトを使用して、ちょうどあなたが

#!/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の)を維持したい日の#と30を変更

#!/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 

キュレーターは、キュレーターが実行したものを出力するだけのドライランフラグを提供します。 config.ymlファイルで定義したログファイルに出力されます。 config_file.ymlで定義されたロギングキーでない場合、curratorはコンソールに出力します。インデックスを削除するには、あなたがしたい場合は、設定ファイルのconfig_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: 

--- 
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です--dryランフラグなし

を上記のコマンドを実行します。毎週、毎月などのインデックスを自動的に削除します。それからちょうどこれらのフォルダのいずれかでシェルスクリプトを入れ

#!/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スクリプトを記述します。/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)の回答は、キュレーター4.xから古くなっています。これは、現在の弾性探査のインストールにあてはまるはずです(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"}]' 
関連する問題