我是Logstash的新手,我试图找到一种记录Nginx事件的方法。我不想记录所有访问事件,而只记录404事件,所以我尝试了如下操作:
我当前的配置如下
input {
beats {
port => 5044
host => "localhost"
}
}
filter {
if [fields][type] == "nginx" {
if [fields][category] == "access" {
grok {
match => { "message" => ["%{IPORHOST:[nginx][access][remote_ip]} - %{DATA:[nginx][access][user_name]} \[%{HTTPDATE:[nginx][access][time]}\] \"%{WORD:[nginx][access][method]} %{DATA:[nginx][access][url]} HTTP/%{NUMBER:[nginx][access][http_version]}\" %{NUMBER:[nginx][access][response_code]} %{NUMBER:[nginx][access][body_sent][bytes]} \"%{DATA:[nginx][access][referrer]}\" \"%{DATA:[nginx][access][agent]}\""] }
remove_field => "message"
}
if "404" != [nginx][access][response_code] {
drop {}
}
}
}
}
output {
lumberjack {
hosts => ["listener.logz.io"]
port => 5006
ssl_certificate => "/usr/share/logstash/keys/TrustExternalCARoot.crt"
codec => "json_lines"
}
}
不幸的是,下降根本不起作用。我一直在尝试调试它,但是日志并没有显示很多有用的东西。我事件试图将drop移到if之外,但所有事件仍在记录中。
下面的答案:
所以我的问题是: 1.我不完全了解filebeat和logstash的工作方式 2.我查看了旧版本的示例代码
在较新的版本中,filebeat中传递的其他字段将被压入“ fields”键下。除此之外,在我的配置中我还拥有fields_under_root:true,却不知道它的真正作用。
因此我在filebeat配置中删除了关键的fields_under_root:true,然后一切正常,因为现在过滤器中的if实际上起作用了。
答案 0 :(得分:1)
尝试一下:
input {
beats {
port => 5044
host => "localhost"
}
}
output {
if "404" != [nginx][access][response_code] {
lumberjack {
hosts => ["listener.logz.io"]
port => 5006
ssl_certificate => "/usr/share/logstash/keys/TrustExternalCARoot.crt"
codec => "json_lines"
}
}
}
通过以下方式调试Logstash配置:(适用于Logstash 5.1 +)
/usr/share/logstash/bin/logstash --config.test_and_exit -f <the config file/folder>
编辑:
如果它不起作用,请尝试在输入和输出之间添加此过滤器:
filter {
if [fileset][module] == "apache2" {
if [fileset][name] == "access" {
grok {
match => { "message" => ["%{IPORHOST:[apache2][access][remote_ip]} - %{DATA:[apache2][access][user_name]} \[%{HTTPDATE:[apache2][access][time]}\] \"%{WORD:[apache2][access][method]} %{DATA:[apache2][access][url]} HTTP/%{NUMBER:[apache2][access][http_version]}\" %{NUMBER:[apache2][access][response_code]} %{NUMBER:[apache2][access][body_sent][bytes]}( \"%{DATA:[apache2][access][referrer]}\")?( \"%{DATA:[apache2][access][agent]}\")?",
"%{IPORHOST:[apache2][access][remote_ip]} - %{DATA:[apache2][access][user_name]} \\[%{HTTPDATE:[apache2][access][time]}\\] \"-\" %{NUMBER:[apache2][access][response_code]} -" ] }
}
}
}
}
原因是,如果不这样做,就无法访问[nginx] [access] [response_code]。
这实际上应该起作用。