Logstash 日志存储的日志旋转脚本,用于清除超过两周的日志
我正试图想出一个最好的方法来清除日志存储服务器中超过两周的日志 对于那些不知道的人,Logstash将其日志存储在Elasticsearch中。我工作的地方有一个非常稳定的麋鹿群(Elasticsearch/Logstash/Kibana) 删除logstash索引的典型方法是使用如下curl命令:Logstash 日志存储的日志旋转脚本,用于清除超过两周的日志,logstash,Logstash,我正试图想出一个最好的方法来清除日志存储服务器中超过两周的日志 对于那些不知道的人,Logstash将其日志存储在Elasticsearch中。我工作的地方有一个非常稳定的麋鹿群(Elasticsearch/Logstash/Kibana) 删除logstash索引的典型方法是使用如下curl命令: #curl --user admin -XDELETE http://localhost:9200/logstash-2015.06.06 Enter host password for user
#curl --user admin -XDELETE http://localhost:9200/logstash-2015.06.06
Enter host password for user 'admin':
{"acknowledged":true}
现在我要寻找的是一种程序化的方法,可以更改日志存储索引中的日期,从而自动清除任何超过两周的索引
我正在考虑使用bash来完成这项工作
我希望你能举一些例子来说明如何做,或者给我一些建议
谢谢
谢谢!!但是你觉得你能用auth帮我把这件事做好吗
这就是我迄今为止所尝试的:
[root@logs:~] #curator --help | grep -i auth
--http_auth TEXT Use Basic Authentication ex: user:pass
[root@logs:~] #curator delete indices --older-than 14 --time-unit days --timestring %Y.%m.%d --regex '^logstash-' --http_auth admin:secretsauce
Error: no such option: --http_auth
[root@logs:~] #curator delete indices --older-than 14 --time-unit days --timestring %Y.%m.%d --regex '^logstash-' --http_auth admin:secretsauce
Error: no such option: --http_auth
[root@logs:~] #curator delete indices --http_auth admin:secretsauce --older-than 14 --time-unit days --timestring %Y.%m.%d --regex '^logstash-'
Error: no such option: --http_auth
使用。要删除超过14天的索引,可以运行以下命令:
curator delete indices --older-than 14 --time-unit days --timestring %Y.%m.%d --regex '^logstash-'
报告解释了其中的许多细节。URL方便地提供在--help输出的顶部:
$ curator --help
Usage: curator [OPTIONS] COMMAND [ARGS]...
Curator for Elasticsearch indices.
See http://elastic.co/guide/en/elasticsearch/client/curator/current
。国旗上写着:
此标志必须位于任何命令之前
如果策展人出于这样或那样的原因不为您工作,您可以运行以下bash脚本:
#!/bin/bash
: ${2?"Usage: $0 [number of days] [base url of elastic]"}
days=${1}
baseURL=${2}
curl "${baseURL}/_cat/indices?v&h=i" | grep logstash | sort --key=1 | awk -v n=${days} '{if(NR>n) print a[NR%n]; a[NR%n]=$0}' | awk -v baseURL="$baseURL" '{printf "curl -XDELETE '\''%s/%s'\''\n", baseURL, $1}' | while read x ; do eval $x ; done
ElasticSearch X-Pack允许您设置策略,根据年龄自动删除索引。这是一个相当复杂的解决方案,并支付: 策展人似乎维护得很好,支持ElasticSearch的最新版本,做你想做的事 或者,这里有一个BASH脚本。但是,由于我使用了非POSIX
date-ud
,它在BSD或Mac上不起作用
我使用systemd每天运行这个
#!/usr/bin/env bash
elasticsearchURL="http://localhost:9200"
date_format="%Y.%m.%d"
today_seconds=$(date +"%s")
let seconds_per_day=24*60*60
let delay_seconds=$seconds_per_day*7
let cutoff_seconds=$today_seconds-$delay_seconds
cutoff_date=$(date -ud "@$cutoff_seconds" +"$date_format")
indices=$(curl -XGET "${elasticsearchURL}/_cat/indices" | cut -d ' ' -f 3 | grep -P "\d{4}\.\d{2}\.\d{2}")
echo "Deleting indexes created before the cutoff date $cutoff_date."
for index in $indices; do
index_date=$(echo "$index" | grep -P --only-matching "\d{4}\.\d{2}\.\d{2}")
if [[ $index_date < $cutoff_date ]]; then
echo "Deleting old index $index"
curl -XDELETE "${elasticsearchURL}/$index"
echo ""
fi
done
#/usr/bin/env bash
elasticsearchURL=”http://localhost:9200"
日期格式=“%Y.%m.%d”
今天\u秒=$(日期+%s)
让每天的秒数=24*60*60
让延迟秒=每天$seconds\u*7
让截止时间=今天时间-$延迟时间
截止日期=$(日期-ud“@$cutoff\u seconds”+“$date\u格式”)
索引=$(curl-XGET“${elasticsearchURL}/_cat/index”| cut-d'-f3 | grep-P“\d{4}\.\d{2}.\d{2}”)
echo“删除截止日期$cutoff_date之前创建的索引。”
以美元指数表示的指数;做
index_date=$(echo“$index”| grep-P--仅匹配“\d{4}\.\d{2}\.\d{2}”)
如果[$index_date<$cution_date]];然后
echo“删除旧索引$index”
curl-xdelite“${elasticsearchURL}/$index”
回声“”
fi
完成
为此,有一个特殊的实用程序。它必须安装为
然后,您需要在中的“hosts”参数中将地址写入ElasticSerach服务器。在Windows上,此文件应位于用户文件夹中,例如:c:\Users\yourUserName\.curator\curator.yml
然后,您需要创建一个包含操作“curatorRotateLogs.yml”的文件,例如:
---
# Remember, leave a key empty if there is no value. None will be a string,
# not a Python "NoneType"
actions:
1:
action: delete_indices
description: >-
Delete indices older than 45 days (based on index name), for logstash-
prefixed indices. Ignore the error if the filter does not result in an
actionable list of indices (ignore_empty_list) and exit cleanly.
options:
ignore_empty_list: True
disable_action: False
filters:
- filtertype: pattern
kind: prefix
value: logstash-
- filtertype: age
source: name
direction: older
timestring: '%Y.%m.%d'
unit: days
unit_count: 14
然后运行调度程序:“C:\Program Files\elasticsearch curator\curator.exe”C:\MyСoolFolder\curatorRotateLogs.yml谢谢Magnus!我感谢你的帮助!但我还有一个问题不适合评论。你能看一下吗?我每天都对弹性材料印象越来越深刻!漂亮的小工具!这确实是一个不同的问题,但我怀疑问题在于
--http\u auth
是curator命令的一个选项,而不是它的delete index子命令,也就是说,您应该运行curator--http\u auth。。。删除索引--早于…
。是的!就这样#策展人--http_auth admin:secretsause delete index--早于14--时间单位天--时间字符串%Y.%m.%d--正则表达式“^logstash-”,感谢您的帮助!它可以工作,只是需要用XDELETE替换XGET