【logstash】 - logstash配置语言基础


在网上很难找到logstash中文资料,ruby也没了解过,看官方文档太吃力,而个人要求也不高,使用loggstash能够提取想要的字段便可。html


如下内容纯粹想固然的理解:mysql


logstash配置格式
ios

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

input {
  ...#读取数据,logstash已提供很是多的插件,好比能够从file、redis、syslog等读取数据
}

filter {
  ...#想要从不规则的日志中提取关注的数据,就须要在这里处理。经常使用的有grok、mutate等
}

output {
  ...#输出数据,在上面处理后的数据输出到file、elasticsearch等
}



logstash处理过程:正则表达式


1.从input中的插件中读入数据,按行处理(与awk同样)redis

file{sql

path => "/var/log/maillog"apache

start_position => "beginning"数组

}ruby

2.在filter中进行数据处理bash

首先读取第一行,把内容传给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"
}
相关文章
相关标签/搜索