Removing old indices in elasticsearch

后端 未结 9 2142
生来不讨喜
生来不讨喜 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    
    

提交回复
热议问题