【问题标题】:log rotation script for logstash to purge logs greater than two weeks oldlogstash 的日志轮换脚本,用于清除超过两周的日志
【发布时间】:2015-07-15 03:45:28
【问题描述】:

我正在尝试想出最好的方法来清除超过两周前的 logstash 服务器中的日志。

对于那些不知道的人,Logstash 将其日志存储在 Elasticsearch 中。我工作的地方有一个非常稳定的 ELK 堆栈(Elasticsearch/Logstash/Kibana)。

删除logstash索引的典型方法是使用如下curl命令:

#curl --user admin -XDELETE http://localhost:9200/logstash-2015.06.06
Enter host password for user 'admin':
{"acknowledged":true}

现在我正在寻找一种编程方式来更改 logstash 索引中的日期,以自动清除任何超过两周的索引。

我正在考虑使用 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

【问题讨论】:

    标签: logstash


    【解决方案1】:

    使用Curator。要删除超过 14 天的索引,您可以运行以下命令:

    curator delete indices --older-than 14 --time-unit days --timestring %Y.%m.%d --regex '^logstash-'
    

    【讨论】:

    • 感谢马格努斯!感谢您的帮助!但我还有一个不适合 cmets 的问题。你能看看吗?我每天对弹性的东西印象越来越深刻!不错的小工具!
    • 这确实是一个不同的问题,但我怀疑问题在于 --http_auth 是 curator 命令的一个选项,而不是它的删除索引子命令,即您应该运行 curator --http_auth ... delete indices --older-than ...
    • 是的!就是这样。 #curator --http_auth admin:secretsauce delete indices --older-than 14 --time-unit days --timestring %Y.%m.%d --regex '^logstash-' 感谢您的帮助!
    【解决方案2】:

    如果 curator 由于某种原因无法为您工作,您可以运行以下 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
    

    【讨论】:

    • 它可以工作,只是需要用 XDELETE 替换 XGET
    【解决方案3】:

    online documentation for Curator 解释了其中许多细节。该 URL 在 --help 输出的顶部很容易提供:

    $ curator --help
    Usage: curator [OPTIONS] COMMAND [ARGS]...
    
      Curator for Elasticsearch indices.
    
      See http://elastic.co/guide/en/elasticsearch/client/curator/current
    

    There's an entire sub-section on flags。在documentation for the --http_auth 标志中它说:

    这个标志必须在任何命令之前。

    【讨论】:

      【解决方案4】:

      ElasticSearch X-Pack 允许您设置策略以根据年龄自动删除索引。这是一个相当复杂的解决方案,并且还付费:https://www.elastic.co/guide/en/elasticsearch/reference/current/index-lifecycle-management.html

      Curator 似乎维护得很好,支持最新版本的 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
      

      【讨论】:

        【解决方案5】:

        为此,有一个特殊的实用程序"Curator" from Elastic。必须安装为specified in the documentation

        然后你需要在the configuration File的“hosts”参数中写入ElasticSerach服务器的地址。在 Windows 上,此文件应位于用户文件夹中,例如:c:\Users\yourUserName\.curator\curator.yml

        那么你需要通过documentation创建一个带有动作“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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-11-12
          • 2020-08-06
          • 2018-12-08
          • 2011-04-11
          • 2017-05-13
          • 1970-01-01
          • 2013-06-06
          • 2019-02-20
          相关资源
          最近更新 更多