logstash语法配置


条件判断(只适用于filter和output)

logstash在处理数据(配置文件中)时,支持通过判断条件(if,else if,else)语句进行编写

语法格式:

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

**`EXPRESSION`:** - 比较运算符(只支持小写): * `==`,`!=`,`<`,`>`,`<=`,`>=` * `=~(匹配正则)`,`!~(不匹配正则)` * `in(包含)`,`not in(不包含)` - 布尔运算符: * `and(与)`,`or(或)`,`nand(非与)`,`xor(非或)` - 一元运算符: * `!(取反)` * `()(复合表达式)`,`!()(对复合表达式取反)`

在比较运算中,字段用[],字符用"",数组[array_name][index_name]


当action字段为"login"时,删除secret字段 ``` filter { if [action] == "login" { mutate { remove_field => "secret" } } if [fields][log_name] == "nginx1" { #array数组写法 ... } } ```
一个条件指定多个表达式 ``` output { # Send production errors to pagerduty if [loglevel] == "ERROR" and [deployment] == "production" { pagerduty { ... } } } ```
`in`操作符判断字段是否包含 ``` 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" } } } output { if "_grokparsefailure" not in [tags] { elasticsearch { ... } } } ```

sprintf输出

匹配到的字段输出都可使用%{}括起来引用

output {
  statsd {
    increment => "apache.%{[response][status]}"
  }
}
#也可以格式化时间,但是这里的yyyy时间定义是这台logstash的时间,而不是filter插件中date模块分析的日期,如果需要以日志的时间,则需要将日志时间拆分后存储成另外的字段,后在output中引用。
output {
  file {
    path => "/var/log/%{type}.%{+yyyy.MM.dd.HH}"
  }
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM