logstash配置解析nginx日志常用操作

我用的是filebeat,

input {
  beats {
                port => 5783
        }
}
filter {
    json {
      source => "message"
    }

}
output {
    elasticsearch {
            hosts => [ "xxx:9200","xxxxx:9200" ]
            index => "nginxaccesslog-%{+YYYY.MM.dd}"
    }

nginx输出要开json格式,不然你搞grok会搞很久,不要问我为什么,如果你的正则very good,那么当我没说。

如果你解析完了以后要吧message给remove掉,那么很简单,mutate很容易做到这点:

  if [event][dataset] !="nginx.error" {
    mutate {
          remove_field  => ["message"]
    }
  }

mutate一般结合if来做,

https://www.elastic.co/guide/en/logstash/current/event-dependent-configuration.html

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

就跟c语法一摸一样,表达式里面支持的运算有三类,

You can use these comparison operators:

    equality: ==, !=, <, >, <=, >=
    regexp: =~, !~ (checks a pattern on the right against a string value on the left)
    inclusion: in, not in

Supported boolean operators are:

    and, or, nand, xor

Supported unary operators are:

    !


这东西还是强大,一个dsl语言,所以还是需要一定的学习成本。我一直觉得技术迭代是一方面,当你熟悉了某个技术以后再去用另一个,学习成本是一定要考虑的要素,除非你的学习能力超强或者是团队有其他人support,否则大部分人还是会选择他熟悉的技术栈。

官网还提供了比较详尽的pipeline语法,
https://www.elastic.co/guide/en/logstash/current/configuration-file-structure.html
忘了看看还是挺有必要的。

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