ELK日志收集系统介绍及过期系统日志清理
1、编写脚本定时清理index
startDeleteOldLog.sh
chmod +x startDeleteOldLog.sh
#!/bin/bash

###################################

###################################
#清除最近多少天的日志,默认30天
past_day_count=$1
if [ ! $past_day_count ]; then
    past_day_count=1;
fi
#待清除的索引匹配规则  vhlloguat-6.8.3-2024.02.04  vhllogsit-6.8.3-2024.02.04
index_prefix=$2
if [ ! $index_prefix ]; then
    index_prefix="vhlloguat\-6.8.3\-*";
fi
#ES地址
es_host=$3
if [ ! $es_host ]; then
    es_host=localhost:9200;
fi
echo "准备清理掉ES[$es_host]内索引前缀为[$index_prefix]的超过当前时间前$past_day_count天的信息......"

function delete_indices() {
    index_name=$1
    index_date=$2
 comp_date=`date -d "$past_day_count day ago" +"%Y-%m-%d"`
 date1="$index_date 00:00:00"
 date2="$comp_date 00:00:00"

 t1=`date -d "$date1" +%s`
 t2=`date -d "$date2" +%s`

 if [ $t1 -le $t2 ]; then        
     curl -XDELETE http://$es_host/$index_name
 fi
}

curl -XGET http://$es_host/_cat/indices | awk -F" " '{print $3}' | egrep "$index_prefix" | sort | while read LINE
do   
    index_name=$LINE;
    index_date=`echo $LINE | awk -F"-" '{print $NF}' | egrep "[0-9]*\.[0-9]*\.[0-9]*" | uniq  | sed 's/\./-/g'`
    if [ $index_date ]; then 
        delete_indices $index_name $index_date
 fi
done

echo "清理完成!"
 
2、设置定时任务
crontab -e
0 2 * * * sh /app/software/elk/elasticsearch-6.8.3/bin/startDeleteOldLog.sh
0 1 * * * sh /app/software/elk/elasticsearch-6.8.3/bin/startDeleteOldLogSIT.sh
service crond restart #重启crontab服务
crontab -l 查看定时任务
cat /var/log/cron 历史查看记录
Feb 20 02:00:01 core-O-test01 CROND[23276]: (core) CMD (sh /app/software/elk/elasticsearch-6.8.3/bin/startDeleteOldLog.sh)
Feb 20 02:00:01 core-O-test01 CROND[23278]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Feb 20 02:00:01 core-O-test01 CROND[23279]: (core) CMD (sh /app/software/elk/elasticsearch-6.8.3/bin/startDeleteOldLogSIT.sh)
删除其中一个
如果你想删除当前用户的某一个crontab任务,那么使用crontab -e进入编辑器,再删除对应的任务。
crontab -r 清空使用者目前的 crontab
 
posted on 2024-10-09 14:32  Old-Kang  阅读(6)  评论(0编辑  收藏  举报