ES定期清理日志
定期清理日志脚本
[root@ELK ~]# cat /root/jws/indexclean.sh #!/bin/bash #清理的主机地址 dst_ip=1.1.1.1 # 需要清理几天以前的索引日志 CLEAN_DAY=90 # 获取N天以前的年月日 YEAR=$(date -d "$(date -I) -$CLEAN_DAY"day"" +%Y) MONTH=$(date -d "$(date -I) -$CLEAN_DAY"day"" +%m) DAY=$(date -d "$(date -I) -$CLEAN_DAY"day"" +%d) # 获取需要过滤N天前的ES索引数据 DATA=`curl -s http://${dst_ip}:9200/_cat/indices?v \ |awk '/[0-9]{4}.[0-9]{2}.[0-9]{2}/{print $3}' |awk -F[-.] '{s2=mktime($(NF-2)" "$(NF-1)" "$NF" 0 0 0");\ s1=systime()}{if(s1-s2 >= 86400*"'$CLEAN_DAY'") print $0}'` echo "即将清理"$YEAR'-'$MONTH'-'$DAY"前ES索引数据>>>>>>>>" echo "清理数据为: "$DATA # 执行清理工作 for i in $DATA do sleep 1 curl -XDELETE http://${dst_ip}:9200/$i done echo "=======================" echo "执行完成" echo "=======================" [root@ELK ~]#
ES查询命令:
[root@ELK ~]# curl -s -XGET 'http://1.1.1.1:9200/_cat/indices?v&h=index,store.size&bytes=mb' | egrep *-2023.05.22 | awk '{ s1=$2} {if(s1 >= 1024) print $1 " " $2/1024"gb"}'
输出超过1G的索引
curl -s -XGET 'http://1.1.1.1:9200/_cat/indices?v&h=index,store.size&bytes=mb' | awk '{if($2 >= 1024) {printf "%s %.2fgb\n", $1,$2/1024}}' | sort -u
命令可参考:https://www.cnblogs.com/crazymakercircle/p/15575624.html