yyyyMMdd HH:mm:ss,SSS"] //格式化时间
target => "@timestamp" //替换默认的时间字段
}
}
output {
elasticsearch {
hosts => "localhost:9200"
}
}
如要匹配的日志格式为:“[DEBUG][20170811 10:07:31,359][DefaultBeanDefinitionDocumentReader:106] Loading bean definitions”,解析出该日志的时间字段的方式有:
① 通过引入写好的表达式文件,如表达式文件为customer_patterns,内容为:
CUSTOMER_TIME %{YEAR}%{MONTHNUM}%{MONTHDAY}\s+%{TIME}
注:内容格式为:[自定义表达式名称] [正则表达式]
然后logstash中就可以这样引用:
filter {
grok {
patterns_dir => ["./customer-patterms/mypatterns"] //引用表达式文件路径
match => [ "message" , "%{CUSTOMER_TIME:customer_time}" ] //使用自定义的grok表达式
}
}
② 以配置项的方式,规则为:(?<自定义表达式名称>正则匹配规则),如:
filter {
grok {
match => [ "message" , "(?<customer_time>%{YEAR}%{MONTHNUM}%{MONTHDAY}\s+%{TIME})" ]
}
}
问题:如何在Kibana中通过选择不同的系统日志模块来查看数据
一般在Kibana中显示的日志数据混合了来自不同系统模块的数据,那么如何来选择或者过滤只查看指定的系统模块的日志数据?
解决方案:新增标识不同系统模块的字段或根据不同系统模块建ES索引
1、新增标识不同系统模块的字段,然后在Kibana中可以根据该字段来过滤查询不同模块的数据
这里以第二种部署架构讲解,在Filebeat中的配置内容为:
filebeat.prospectors:
-
paths:
- /home/project/elk/logs/account.log
input_type: log
multiline:
pattern: '^\['
negate: true
match: after
fields: //新增log_from字段
log_from: account
-
paths:
- /home/project/elk/logs/customer.log
input_type: log
multiline:
pattern: '^\['
negate: true
match: after
fields:
log_from: customer
output:
logstash:
hosts: ["localhost:5044"]
通过新增:log_from字段来标识不同的系统模块日志
2、根据不同的系统模块配置对应的ES索引,然后在Kibana中创建对应的索引模式匹配,即可在页面通过索引模式下拉框选择不同的系统模块数据。
这里以第二种部署架构讲解,分为两步:
① 在Filebeat中的配置内容为:
filebeat.prospectors:
-
paths:
- /home/project/elk/logs/account.log
input_type: log
multiline:
pattern: '^\['
negate: true
match: after
document_type: account
-
paths:
- /home/project/elk/logs/customer.log
input_type: log
multiline:
pattern: '^\['
negate: true
match: after
document_type: customer
output:
logstash:
hosts: ["localhost:5044"]
通过document_type来标识不同系统模块
② 修改Logstash中output的配置内容为:
output {
elasticsearch {
hosts => "localhost:9200"
index => "%{type}"
}
}
在output中增加index属性,%{type}表示按不同的document_type值建ES索引
四、总结
本文主要介绍了ELK实时日志分析的三种部署架构,以及不同架构所能解决的问题,这三种架构中第二种部署方式是时下最流行也是最常用的部署方式,最后介绍了ELK作在日志分析中的一些问题与解决方案,说在最后,ELK不仅仅可以用来作为分布式日志数据集中式查询和管理,还可以用来作为项目应用以及服务器资源监控等场景,更多内容请看官网。