热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ELKlogstash配置语法

2019独角兽企业重金招聘Python工程师标准数据类型logstash支持的数据类型有:array数组可以是单个或者多个字符串值。path[varlogm

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

数据类型

logstash支持的数据类型有:

  • array
    数组可以是单个或者多个字符串值。
    path => [ "/var/log/messages", "/var/log/*.log" ]
    path => "/data/mysql/mysql.log"
    如果指定了多次,追加数组。此实例path数组包含三个字符串元素。
  • boolean
    布尔值必须是TRUE或者false。true和false不能有引号。
    ssl_enable => true
  • bytes
    指定字节单位。支持的单位有SI (k M G T P E Z Y) 和 Binary (Ki Mi Gi Ti Pi Ei Zi Yi)。Binary单位基于1024,SI单位基于1000。不区分大小写和忽略值与单位之间的空格。如果没有指定单位,默认是byte。
    my_bytes => "1113" # 1113 bytes
    my_bytes => "10MiB" # 10485760 bytes
    my_bytes => "100kib" # 102400 bytes
    my_bytes => "180 mb" # 180000000 bytes
  • Codec
    logstash编码名称用来表示数据编码。用于input和output段。便于数据的处理。如果input和output使用合适的编码,就无需单独的filter对数据进行处理。
    codec => "json"
  • hash
    键值对,注意多个键值对用空格分隔,而不是逗号。
    match => {
    "field1" => "value1"
    "field2" => "value2"
    ... }
  • number
    必须是有效的数值,浮点数或者整数。
    port => 33
  • password
    一个单独的字符串。
    my_password => "password"
  • path
    一个代表有效的操作系统路径。
    my_path => "/tmp/logstash"
  • string
    name => "Hello world"
    name => 'It\'s a beautiful day'

字段引用

logstash字段引用语法。要在 Logstash 配置中使用字段的值,只需要把字段的名字写在中括号 [] 里就行了,这就叫字段引用。还需注意字段层次。如果引用的是一个顶级字段,可以省略[],直接指定字段名。要引用嵌套的字段,需要指定完整的路径,如[top-level field][nested field]。

下面有五个顶级字段(agent, ip, request, response, ua) 和三个嵌套字段 (status, bytes, os)。

{ "agent": "Mozilla/5.0 (compatible; MSIE 9.0)", "ip": "192.168.24.44", "request": "/index.html" "response": { "status": 200, "bytes": 52353 }, "ua": { "os": "Windows 7" } }

1

2

3

4

5

6

7

8

9

10

11

12

{

  "agent": "Mozilla/5.0 (compatible; MSIE 9.0)",

  "ip": "192.168.24.44",

  "request": "/index.html"

  "response": {

    "status": 200,

    "bytes": 52353

  },

  "ua": {

    "os": "Windows 7"

  }

}

为了引用os字段,需指定[ua][os]。引用顶级字段如request,可以简单指定request即可。

sprintf格式

字段引用格式也可以用于logstash调用sprintf格式。这种格式可以从其他字符串中引用字段值。如:

output { statsd { increment => "apache.%{[response][status]}" } }

1

2

3

4

5

output {

  statsd {

    increment => "apache.%{[response][status]}"

  }

}

也可以格式化时间。如:

output { file { path => "/var/log/%{type}.%{+yyyy.MM.dd.HH}" } }

1

2

3

4

5

output {

  file {

    path => "/var/log/%{type}.%{+yyyy.MM.dd.HH}"

  }

}

 

条件判断

使用条件来决定filter和output处理特定的事件。

logstash条件类似于编程语言。条件支持if、else if、else语句,可以嵌套。

条件语法如下:

if EXPRESSION { ... } else if EXPRESSION { ... } else { ... }

1

2

3

4

5

6

7

if EXPRESSION {

  ...

} else if EXPRESSION {

  ...

} else {

  ...

}

比较操作有:

  • 相等: &#61;&#61;, !&#61;, <, >, <&#61;, >&#61;
  • 正则: &#61;~(匹配正则), !~(不匹配正则)
  • 包含: in(包含), not in(不包含)

布尔操作&#xff1a;

  • and(与), or(或), nand(非与), xor(非或)

一元运算符&#xff1a;

  • !(取反)
  • ()(复合表达式), !()(对复合表达式结果取反)

如mutate filter删除secret字段对于action是login的&#xff1a;

filter { if [action] &#61;&#61; "login" { mutate { remove &#61;> "secret" } } }

1

2

3

4

5

filter {

  if [action] &#61;&#61; "login" {

    mutate { remove &#61;> "secret" }

  }

}

在一个条件里指定多个表达式&#xff1a;

output { # Send production errors to pagerduty if [loglevel] &#61;&#61; "ERROR" and [deployment] &#61;&#61; "production" { pagerduty { ... } } }

1

2

3

4

5

6

7

8

output {

  # Send production errors to pagerduty

  if [loglevel] &#61;&#61; "ERROR" and [deployment] &#61;&#61; "production" {

    pagerduty {

    ...

    }

  }

}

在in条件&#xff0c;可以比较字段值&#xff1a;

filter { if [foo] in [foobar] { mutate { add_tag &#61;> "field in field" } } if [foo] in "foo" { mutate { add_tag &#61;> "field in string" } } if "hello" in [greeting] { mutate { add_tag &#61;> "string in field" } } if [foo] in ["hello", "world", "foo"] { mutate { add_tag &#61;> "field in list" } } if [missing] in [alsomissing] { mutate { add_tag &#61;> "shouldnotexist" } } if !("foo" in ["hello", "world"]) { mutate { add_tag &#61;> "shouldexist" } } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

filter {

  if [foo] in [foobar] {

    mutate { add_tag &#61;> "field in field" }

  }

  if [foo] in "foo" {

    mutate { add_tag &#61;> "field in string" }

  }

  if "hello" in [greeting] {

    mutate { add_tag &#61;> "string in field" }

  }

  if [foo] in ["hello", "world", "foo"] {

    mutate { add_tag &#61;> "field in list" }

  }

  if [missing] in [alsomissing] {

    mutate { add_tag &#61;> "shouldnotexist" }

  }

  if !("foo" in ["hello", "world"]) {

    mutate { add_tag &#61;> "shouldexist" }

  }

}

 

output { if "_grokparsefailure" not in [tags] { elasticsearch { ... } } }

1

2

3

4

5

output {

  if "_grokparsefailure" not in [tags] {

    elasticsearch { ... }

  }

}

字段引用、sprintf格式、条件判断只能用于filter和output&#xff0c;不能用于input。

&#64;metadata字段

在logstash1.5版本开始&#xff0c;有一个特殊的字段&#xff0c;叫做&#64;metadata。&#64;metadata包含的内容不会作为事件的一部分输出。

input { stdin { } } filter { mutate { add_field &#61;> { "show" &#61;> "This data will be in the output" } } mutate { add_field &#61;> { "[&#64;metadata][test]" &#61;> "Hello" } } mutate { add_field &#61;> { "[&#64;metadata][no_show]" &#61;> "This data will not be in the output" } } } output { if [&#64;metadata][test] &#61;&#61; "Hello" { stdout { codec &#61;> rubydebug } } }

1

2

3

4

5

6

7

8

9

10

11

12

13

input { stdin { } }

 

filter {

  mutate { add_field &#61;> { "show" &#61;> "This data will be in the output" } }

  mutate { add_field &#61;> { "[&#64;metadata][test]" &#61;> "Hello" } }

  mutate { add_field &#61;> { "[&#64;metadata][no_show]" &#61;> "This data will not be in the output" } }

}

 

output {

  if [&#64;metadata][test] &#61;&#61; "Hello" {

    stdout { codec &#61;> rubydebug }

  }

}

查看输出&#xff1a;

$ bin/logstash -f ../test.conf Logstash startup completed asdf { "message" &#61;> "asdf", "&#64;version" &#61;> "1", "&#64;timestamp" &#61;> "2015-03-18T23:09:29.595Z", "host" &#61;> "www.ttlsa.com", "show" &#61;> "This data will be in the output" }

1

2

3

4

5

6

7

8

9

10

$ bin/logstash -f ../test.conf

Logstash startup completed

asdf

{

       "message" &#61;> "asdf",

      "&#64;version" &#61;> "1",

    "&#64;timestamp" &#61;> "2015-03-18T23:09:29.595Z",

          "host" &#61;> "www.ttlsa.com",

          "show" &#61;> "This data will be in the output"

}

"asdf"变成message字段内容。条件与&#64;metadata内嵌的test字段内容判断成功&#xff0c;但是输出并没有展示&#64;metadata字段和其内容。

不过&#xff0c;如果指定了metadata &#61;> true&#xff0c;rubydebug codec允许显示&#64;metadata字段的内容。

stdout { codec &#61;> rubydebug { metadata &#61;> true } }

1

stdout { codec &#61;> rubydebug { metadata &#61;> true } }

下面是输出的内容&#xff1a;

$ bin/logstash -f ../test.conf Logstash startup completed asdf { "message" &#61;> "asdf", "&#64;version" &#61;> "1", "&#64;timestamp" &#61;> "2015-03-18T23:10:19.859Z", "host" &#61;> "www.ttlsa.com", "show" &#61;> "This data will be in the output", "&#64;metadata" &#61;> { "test" &#61;> "Hello", "no_show" &#61;> "This data will not be in the output" } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

$ bin/logstash -f ../test.conf

Logstash startup completed

asdf

{

       "message" &#61;> "asdf",

      "&#64;version" &#61;> "1",

    "&#64;timestamp" &#61;> "2015-03-18T23:10:19.859Z",

          "host" &#61;> "www.ttlsa.com",

          "show" &#61;> "This data will be in the output",

     "&#64;metadata" &#61;> {

           "test" &#61;> "Hello",

        "no_show" &#61;> "This data will not be in the output"

    }

}

可以看到&#64;metadata字段及其子字段内容。

注意&#xff1a;只有rubydebug codec可以显示&#64;metadata字段内容。

确保&#64;metadata字段临时需要&#xff0c;不希望最终输出。最常见的情景是filter的时间字段&#xff0c;需要一临时的时间戳。如&#xff1a;

input { stdin { } } filter { grok { match &#61;> [ "message", "%{HTTPDATE:[&#64;metadata][timestamp]}" ] } date { match &#61;> [ "[&#64;metadata][timestamp]", "dd/MMM/yyyy:HH:mm:ss Z" ] } } output { stdout { codec &#61;> rubydebug } }

1

2

3

4

5

6

7

8

9

10

input { stdin { } }

 

filter {

  grok { match &#61;> [ "message", "%{HTTPDATE:[&#64;metadata][timestamp]}" ] }

  date { match &#61;> [ "[&#64;metadata][timestamp]", "dd/MMM/yyyy:HH:mm:ss Z" ] }

}

 

output {

  stdout { codec &#61;> rubydebug }

}

输出结果&#xff1a;

$ bin/logstash -f ../test.conf Logstash startup completed 02/Mar/2014:15:36:43 &#43;0100 { "message" &#61;> "02/Mar/2014:15:36:43 &#43;0100", "&#64;version" &#61;> "1", "&#64;timestamp" &#61;> "2014-03-02T14:36:43.000Z", "host" &#61;> "example.com" }

1

2

3

4

5

6

7

8

9

$ bin/logstash -f ../test.conf

Logstash startup completed

02/Mar/2014:15:36:43 &#43;0100

{

       "message" &#61;> "02/Mar/2014:15:36:43 &#43;0100",

      "&#64;version" &#61;> "1",

    "&#64;timestamp" &#61;> "2014-03-02T14:36:43.000Z",

          "host" &#61;> "example.com"

}

 


转:https://my.oschina.net/u/3362827/blog/1054033



推荐阅读
  • SpringMVC接收请求参数的方式总结
    本文总结了在SpringMVC开发中处理控制器参数的各种方式,包括处理使用@RequestParam注解的参数、MultipartFile类型参数和Simple类型参数的RequestParamMethodArgumentResolver,处理@RequestBody注解的参数的RequestResponseBodyMethodProcessor,以及PathVariableMapMethodArgumentResol等子类。 ... [详细]
  • 本文介绍了Java工具类库Hutool,该工具包封装了对文件、流、加密解密、转码、正则、线程、XML等JDK方法的封装,并提供了各种Util工具类。同时,还介绍了Hutool的组件,包括动态代理、布隆过滤、缓存、定时任务等功能。该工具包可以简化Java代码,提高开发效率。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了Perl的测试框架Test::Base,它是一个数据驱动的测试框架,可以自动进行单元测试,省去手工编写测试程序的麻烦。与Test::More完全兼容,使用方法简单。以plural函数为例,展示了Test::Base的使用方法。 ... [详细]
  • 本文介绍了如何使用JSONObiect和Gson相关方法实现json数据与kotlin对象的相互转换。首先解释了JSON的概念和数据格式,然后详细介绍了相关API,包括JSONObject和Gson的使用方法。接着讲解了如何将json格式的字符串转换为kotlin对象或List,以及如何将kotlin对象转换为json字符串。最后提到了使用Map封装json对象的特殊情况。文章还对JSON和XML进行了比较,指出了JSON的优势和缺点。 ... [详细]
  • 阿里Treebased Deep Match(TDM) 学习笔记及技术发展回顾
    本文介绍了阿里Treebased Deep Match(TDM)的学习笔记,同时回顾了工业界技术发展的几代演进。从基于统计的启发式规则方法到基于内积模型的向量检索方法,再到引入复杂深度学习模型的下一代匹配技术。文章详细解释了基于统计的启发式规则方法和基于内积模型的向量检索方法的原理和应用,并介绍了TDM的背景和优势。最后,文章提到了向量距离和基于向量聚类的索引结构对于加速匹配效率的作用。本文对于理解TDM的学习过程和了解匹配技术的发展具有重要意义。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 本文介绍了使用AJAX的POST请求实现数据修改功能的方法。通过ajax-post技术,可以实现在输入某个id后,通过ajax技术调用post.jsp修改具有该id记录的姓名的值。文章还提到了AJAX的概念和作用,以及使用async参数和open()方法的注意事项。同时强调了不推荐使用async=false的情况,并解释了JavaScript等待服务器响应的机制。 ... [详细]
  • 本文介绍了作者在开发过程中遇到的问题,即播放框架内容安全策略设置不起作用的错误。作者通过使用编译时依赖注入的方式解决了这个问题,并分享了解决方案。文章详细描述了问题的出现情况、错误输出内容以及解决方案的具体步骤。如果你也遇到了类似的问题,本文可能对你有一定的参考价值。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文介绍了Oracle存储过程的基本语法和写法示例,同时还介绍了已命名的系统异常的产生原因。 ... [详细]
  • 使用圣杯布局模式实现网站首页的内容布局
    本文介绍了使用圣杯布局模式实现网站首页的内容布局的方法,包括HTML部分代码和实例。同时还提供了公司新闻、最新产品、关于我们、联系我们等页面的布局示例。商品展示区包括了车里子和农家生态土鸡蛋等产品的价格信息。 ... [详细]
  • Ihaveaworkfolderdirectory.我有一个工作文件夹目录。holderDir.glob(*)>holder[ProjectOne, ... [详细]
  • 正则表达式及其范例
    为什么80%的码农都做不了架构师?一、前言部分控制台输入的字符串,编译成java字符串之后才送进内存,比如控制台打\, ... [详细]
author-avatar
陀飞轮小朱朱
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有