首页 文章

Kibana 4.1 - 使用JSON输入从@timestamp创建一个时段字段以获取直方图

提问于
浏览
3

Edit: I found the answer, see below for Logstash <= 2.0 ===>

Plugin created for Logstash 2.0

对于使用Logstash 2.0或更高版本感兴趣的人,我创建了一个简单的插件:

创业板在这里:

https://rubygems.org/gems/logstash-filter-dateparts

这是文档和源代码:

https://github.com/mikebski/logstash-datepart-plugin

我在Logstash中使用@Timestamp获得了大量数据,持续了几周 . 我有一个持续时间字段是一个数字字段,我可以做一个日期直方图 . 我想在一天中的小时内进行直方图,而不是x - > y日期的线性直方图 . 我希望x轴为0 - > 23而不是日期x - >日期y .

我想我可以使用 JSON Input 高级文本输入在结果集中添加一个字段,该结果集是@timestamp的一小时 . 帮助文本说: Any JSON formatted properties you add here will be merged with the elasticsearch aggregation definition for this section. For example shard_size on a terms aggregation 这让我相信它可以完成,但没有给出任何例子 .

编辑添加:

我已经尝试根据下面的链接在脚本字段中设置一个条目,但它不会像4.1上的博客中的示例那样工作 . 尝试添加格式为 number 且名称为 test_day_of_week 的字段时,以下脚本出错: Integer.parseInt("1234")

问题看起来像脚本不是很强大 . 奇怪的是,我想在示例中完成他们正在做的事情(添加月份日期,星期几等字段......) . 如果脚本是 doc['@timestamp'] ,我可以让字段工作,但我无法操纵时间戳 .

文档说Lucene表达式是允许的,并显示了GIS类型的东西的一些trig和GCD示例,但没有任何日期...

BLOG有这样的更新:

更新:作为安全预防措施,从版本4.0.0-RC1开始,Kibana脚本字段默认为Lucene Expressions,而不是Groovy,作为脚本语言 . 由于Lucene Expressions仅支持对数字字段的操作,因此以下处理日期数学的示例在Kibana 4.0.0-RC1版本中不起作用 .

现在没有关于如何实际执行此操作的建议 . 我想我可以启用Groovy插件...

有任何想法吗?

EDIT - THE SOLUTION:

我使用Ruby添加了一个过滤器,这很简单:

基本上,在ruby脚本中,您可以访问 event['field'] ,您可以创建新的脚本 . 我使用Ruby时间位根据事件的 @timestamp 创建新字段 .

ruby {
                code => "ts = event['@timestamp']; event['weekday'] = ts.wday; event['hour'] = ts.hour; event['minute'] = ts.min; event['second'] = ts.sec; event['mday'] = ts.day; event['yday'] = ts.yday; event['month'] = ts.month;"
        }

1 回答

  • 0

    这似乎不再适用于Logstash 1.5.4 - Ruby日期元素似乎不可用,然后抛出“rubyexception”并且不会将字段添加到logstash事件中 .

    我花了一些时间寻找一种方法来恢复Groovy脚本字段中的功能,这些字段不能动态编写脚本,为我提供诸如“hourofday”,“dayofweek”等字段 . 我所做的是直接在Elasticsearch节点上添加这些groovy脚本文件,如下所示:

    /etc/elasticsearch/scripts/ hourofday.groovy dayofweek.groovy weekofyear.groovy ......等等 .

    这些脚本文件包含一行Groovy,如下所示:

    Integer.parseInt(new Date(doc["@timestamp"].value).format("d")) (dayofmonth) Integer.parseInt(new Date(doc["@timestamp"].value).format("u")) (dayofweek)

    要在Kibana中引用这些内容,首先要创建一个新搜索并保存它,或者选择一个现有的已保存搜索(请在更改之前获取现有JSON的副本,以防万一)在“设置 - >保存的对象 - >搜索“页面 . 然后,您可以修改查询以添加“脚本字段”,因此您可以得到如下内容:

    {
        "query" : {
          ...
        },
        "script_fields": {
            "minuteofhour": {
              "script_file": "minuteofhour"
            },
            "hourofday": {
              "script_file": "hourofday"
            },
            "dayofweek": {
              "script_file": "dayofweek"
            },
            "dayofmonth": {
              "script_file": "dayofmonth"
            },
            "dayofyear": {
              "script_file": "dayofyear"
            },
            "weekofmonth": {
              "script_file": "weekofmonth"
            },
            "weekofyear": {
              "script_file": "weekofyear"
            },
            "monthofyear": {
              "script_file": "monthofyear"
            }
          }
        }
    

    如图所示,"script_fields"行应该落在"query"之外,否则会出错 . 还要确保脚本文件可供所有Elasticsearch节点使用 .

相关问题