Removing old indices in elasticsearch

后端 未结 9 2053
生来不讨喜
生来不讨喜 2020-12-07 21:12

I have the many of my logs indexed in logstash-Year-Week format. That is if i want to delete indices older than a few weeks, how can I achieve that in elasticsearch. Is ther

相关标签:
9条回答
  • 2020-12-07 21:34

    yanb (yet another 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    
    
    0 讨论(0)
  • 2020-12-07 21:35
    curator_cli delete_indices --filter_list '{"filtertype":"none"}' 
    

    will delete all or filter:

     --filter_list '[{"filtertype":"age","source":"creation_date","direction":"older","unit":"days","unit_count":13},{"filtertype":"pattern","kind":"prefix","value":"logstash"}]'
    
    0 讨论(0)
  • 2020-12-07 21:44

    Curator would be an ideal match here. You can find the link here - https://github.com/elastic/curator

    A command like below should work just fine -

    curator --host <IP> delete indices --older-than 30 --prefix "twitter-" --time-unit days  --timestring '%Y-%m-%d'
    

    You can keep in this in the CRON for removing the indices occasionally.

    You can find some examples and docs here - https://www.elastic.co/guide/en/elasticsearch/client/curator/current/examples.html

    0 讨论(0)
提交回复
热议问题