1、简述bash
当日志发送到ELK以后,Elasticsearch随着日志的增长,占用磁盘量会愈来愈大。这时候,须要咱们写角本按期DELETE日志。角本写法,也很简单,只是发送HTTP的DELETE方式到:http://<ip>:<port>/*-yyyy.MM.dd*便可。curl
2、按期删除Elasticsearch中日志的角本:新建一个es-index-clear.sh到/opt目录下,内容以下:函数
#/bin/bash #es-index-clear #只保留15天内的日志索引 LAST_DATA=`date -d "-15 days" "+%Y.%m.%d"` #删除上个月份全部的索引 curl -XDELETE 'http://127.0.0.1:9200/*-'${LAST_DATA}'*'
3、使用crontab -e添加定时任务:执行crontab -e,在打开的内容中,输入(前面‘0 * * * *’表示Cron表达式,能够参考我前面的文章):url
好比下列表示每小时整时执行一次:spa
0 * * * * /opt/es-index-clear.sh
若是要天天凌晨执行一次:rest
0 0 * * * /opt/es-index-clear.sh
4、启动定时任务,并开机自动运行日志
systemctl enable crond
systemctl restart crond
systemctl status crond
5、也能够把es-index-clear.sh内容换成其它优秀代码,以下:code
#!/bin/bash ################################### #删除早于十天的ES集群的索引 ################################### function delete_indices() { comp_date=`date -d "10 day ago" +"%Y-%m-%d"` date1="$1 00:00:00" date2="$comp_date 00:00:00" t1=`date -d "$date1" +%s` t2=`date -d "$date2" +%s` if [ $t1 -le $t2 ]; then echo "$1时间早于$comp_date,进行索引删除" #转换一下格式,将相似2017-10-01格式转化为2017.10.01 format_date=`echo $1| sed 's/-/\./g'` curl -XDELETE http://127.0.0.1:9200/*$format_date fi } curl -XGET http://127.0.0.1:9200/_cat/indices | awk -F" " '{print $3}' | awk -F"-" '{print $NF}' | egrep "[0-9]*\.[0-9]*\.[0-9]*" | sort | uniq | sed 's/\./-/g' | while read LINE do #调用索引删除函数 delete_indices $LINE done
6、删除完后,再看占用量:orm