ELK定期清理索引脚本

[root@elk100 scripts]# vim es_clear.sh 

#!/bin/bash

workdir="/root/scripts"
cfgdir=$workdir/conf

CLEAN_LOG_FILE="/root/scripts/logs/es_clean.log"

datasave=10

# 只打印不清理,用于清理前测试 0:不清理 1:清理
clean_status=1

es_user="elastic:Wsjy_1234"


clean_es_data()
{
   echo "`date +%Y-%m-%d,%H:%M:%S` clean es data" >>$CLEAN_LOG_FILE
   end_date=`date -d "${datasave} days ago" +%Y%m%d`
   #app_prefix="nginx_log_"
   app_prefix=$1
   curl -u$es_user -s "localhost:9200/_cat/indices?v&pretty" |grep "$app_prefix" | awk '{print $3}' | awk -F "_" '{if($4<="'$end_date'") print $0}' | while read index_app
   do
           if [ "a$clean_status" == "a0" ];then
                   echo "localhost:9200/${index_app}?pretty" >>$CLEAN_LOG_FILE
           else
                   curl -u$es_user -s -XDELETE "localhost:9200/${index_app}?pretty"
           fi
   done
}

date
echo " Begin..."
clean_es_data "vpn_nginx_log_"


date
echo " End..."

猜你喜欢

转载自www.cnblogs.com/bugbeta/p/12009837.html