【logstash】 - logstash配置語言基礎


在網上很難找到logstash中文資料,ruby也沒了解過,看官方文檔太吃力,而我的要求也不高,使用loggstash可以提取想要的字段即可。


以下內容純粹想當然的理解:


logstash配置格式

#官方文檔:http://www.logstash.net/docs/1.4.2/

input {
  ...#讀取數據,logstash已提供非常多的插件,比如可以從file、redis、syslog等讀取數據
}

filter {
  ...#想要從不規則的日誌中提取關注的數據,就需要在這裏處理。常用的有grok、mutate等
}

output {
  ...#輸出數據,在上面處理後的數據輸出到file、elasticsearch等
}



logstash處理過程:


1.從input中的插件中讀入數據,按行處理(與awk一樣)

file{

path => "/var/log/maillog"

start_position => "beginning"

}

2.在filter中進行數據處理

首先讀取第一行,把內容傳給message字段(message與awk中的$0相似)。

grok{}從message中取需要的數據,主要使用正則表達式。

mutate{}主要是修改數據,比如取得一個字段的值,可以使用mutate進行數據處理。

3.把處理後的數據輸出去各個插件


處理完一行數據後,重複上面的動作,直到把數據全部處理完成。




logstash配置語言


網址:http://www.logstash.net/docs/1.4.2/configuration


#:註釋

Boolean:true 或者false
Examples:
debug => true


String(字符串)

name => "Hello world"
#字符串放在雙引號內
abc => "%{name}"
#這樣abc的值就是name的值


Number

port => 33


Array(數組)

path => [ "/var/log/messages", "/var/log/*.log" ]
path => "/data/mysql/mysql.log"

#path包含三個路徑。


Hash

match => {
  "field1" => "value1"
  "field2" => "value2"
  ...
}
#把多個字段放在{}中,每個字段使用 "key" => "value"


Field References(字段引用)

{
  "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"
  }
}

#字段引用使用[]號,比如使用status做判斷,if [status] = 200 {}
#若是要取得字段的值,使用 %{ip}
#取os的值,需要這樣:[ua][os],可以把ua看作數組名,os是下標。


Conditionals(條件語句)


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


equality, etc: ==, !=, <, >, <=, >=
regexp: =~, !~ (正則表達式)
inclusion: in, not in
and, or, nand, xor
!

#例子如下:
filter {
  if [action] == "login" {
    mutate { remove => "secret" }
  }
}


output {
  if [type] == "apache" {
    if [status] =~ /^5\d\d/ {
      nagios { ...  }
    } else if [status] =~ /^4\d\d/ {
      elasticsearch { ... }
    }

    statsd { increment => "apache.%{status}" }
  }
}

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

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" }
  }
}
Or, to test if grok was successful:

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





前面關於mutate處理alter日誌,存在非常多的問題。比如原字符串裏面有多個:符號,就會描述顯示不全。使用grok處理如下:

input{

	stdin{
		type => "hxwtest"

	    }	

}


filter{

grok{
	match => ["message","(?<ORAERR_ID>^O[A-Z]{2}-[0-9]{5}):(?<ORA_DESC>.*)"]
}

grok{
    #(?<組名>regex) 把regex捕獲的內容放到組名中,組名會當作一個字段。(?<=:)環視
	match => ["message","(?<TEST>(?<=:).*)"]
}


if "_grokparsefailure" not in [tags]{
	mutate{
			add_field => {"NGSUBTEST" => "%{TEST}"}
		}
	}



#把TEST中的空格去掉
mutate {gsub => ["TEST"," ",""]}



}

output{

      stdout{
		codec => rubydebug
	}

}


結果如下:

ORA-01589: alter database oracle lkjldkfjdkf
{
       "message" => "ORA-01589: alter database oracle lkjldkfjdkf\r",
      "@version" => "1",
    "@timestamp" => "2014-12-13T02:50:46.671Z",
          "type" => "hxwtest",
          "host" => "huangwen",
     "ORAERR_ID" => "ORA-01589",
      "ORA_DESC" => " alter database oracle lkjldkfjdkf\r",
          "TEST" => "alterdatabaseoraclelkjldkfjdkf\r",
     "NGSUBTEST" => " alter database oracle lkjldkfjdkf\r"
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章