1. 程式人生 > >Logstash 配置語法

Logstash 配置語法

轉載:http://www.ttlsa.com/elk/elk-logstash-configuration-syntax/

資料型別

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)。

 

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格式。這種格式可以從其他字串中引用欄位值。如:

 

1

2

3

4

5

output {

  statsd {

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

  }

}

也可以格式化時間。如:

 

1

2

3

4

5

output {

  file {

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

  }

}

 

條件判斷

使用條件來決定filter和output處理特定的事件。

logstash條件類似於程式語言。條件支援if、else if、else語句,可以巢狀。

條件語法如下:

 

1

2

3

4

5

6

7

if EXPRESSION {

  ...

} else if EXPRESSION {

  ...

} else {

  ...

}

比較操作有:

  • 相等: ==!=<><=>=
  • 正則: =~(匹配正則)!~(不匹配正則)
  • 包含: in(包含)not in(不包含)

布林操作:

  • and(與)or(或)nand(非與)xor(非或)

一元運算子:

  • !(取反)
  • ()(複合表示式), !()(對複合表示式結果取反)

如mutate filter刪除secret欄位對於action是login的:

 

1

2

3

4

5

filter {

  if [action] == "login" {

    mutate { remove => "secret" }

  }

}

在一個條件裡指定多個表示式:

 

1

2

3

4

5

6

7

8

output {

  # Send production errors to pagerduty

  if [loglevel] == "ERROR" and [deployment] == "production" {

    pagerduty {

    ...

    }

  }

}

在in條件,可以比較欄位值:

 

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 => "field in field" }

  }

  if [foo] in "foo" {

    mutate { add_tag => "field in string" }

  }

  if "hello" in [greeting] {

    mutate { add_tag => "string in field" }

  }

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

    mutate { add_tag => "field in list" }

  }

  if [missing] in [alsomissing] {

    mutate { add_tag => "shouldnotexist" }

  }

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

    mutate { add_tag => "shouldexist" }

  }

}

 

 

1

2

3

4

5

output {

  if "_grokparsefailure" not in [tags] {

    elasticsearch { ... }

  }

}

欄位引用、sprintf格式、條件判斷只能用於filter和output,不能用於input。

@metadata欄位

在logstash1.5版本開始,有一個特殊的欄位,叫做@metadata。@metadata包含的內容不會作為事件的一部分輸出。

 

1

2

3

4

5

6

7

8

9

10

11

12

13

input { stdin { } }

 

filter {

  mutate { add_field => { "show" => "This data will be in the output" } }

  mutate { add_field => { "[@metadata][test]" => "Hello" } }

  mutate { add_field => { "[@metadata][no_show]" => "This data will not be in the output" } }

}

 

output {

  if [@metadata][test] == "Hello" {

    stdout { codec => rubydebug }

  }

}

檢視輸出:

 

1

2

3

4

5

6

7

8

9

10

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

Logstash startup completed

asdf

{

       "message" => "asdf",

      "@version" => "1",

    "@timestamp" => "2015-03-18T23:09:29.595Z",

          "host" => "www.ttlsa.com",

          "show" => "This data will be in the output"

}

"asdf"變成message欄位內容。條件與@metadata內嵌的test欄位內容判斷成功,但是輸出並沒有展示@metadata欄位和其內容。

不過,如果指定了metadata => true,rubydebug codec允許顯示@metadata欄位的內容。

 

1

stdout { codec => rubydebug { metadata => true } }

下面是輸出的內容:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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

Logstash startup completed

asdf

{

       "message" => "asdf",

      "@version" => "1",

    "@timestamp" => "2015-03-18T23:10:19.859Z",

          "host" => "www.ttlsa.com",

          "show" => "This data will be in the output",

     "@metadata" => {

           "test" => "Hello",

        "no_show" => "This data will not be in the output"

    }

}

可以看到@metadata欄位及其子欄位內容。

注意:只有rubydebug codec可以顯示@metadata欄位內容。

確保@metadata欄位臨時需要,不希望最終輸出。最常見的情景是filter的時間欄位,需要一臨時的時間戳。如:

 

1

2

3

4

5

6

7

8

9

10

input { stdin { } }

 

filter {

  grok { match => [ "message", "%{HTTPDATE:[@metadata][timestamp]}" ] }

  date { match => [ "[@metadata][timestamp]", "dd/MMM/yyyy:HH:mm:ss Z" ] }

}

 

output {

  stdout { codec => rubydebug }

}

輸出結果:

 

1

2

3

4

5

6

7

8

9

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

Logstash startup completed

02/Mar/2014:15:36:43 +0100

{

       "message" => "02/Mar/2014:15:36:43 +0100",

      "@version" => "1",

    "@timestamp" => "2014-03-02T14:36:43.000Z",

          "host" => "example.com"

}