ELK环境搭建之filebeat根据不同的监控日志文件建立不同的索引
搭建好ELK环境之后,开始监控日志文件。
服务器上有七八个不同类型的业务日志文件。在使用filebeat时,如果没有进行区分,则在收集日志时会统一建立到一个名为filebeat-6.4.2-*的索引文件中。可以正常查询,但是没有合适的分类,且不利于以后的管理。
所以将监控的不同日志建立不同的索引。按照以下步骤处理
一、建立索引模板文件.为不同的日志文件建立不同所以模板。由于我这里是单节点,这样建立的话减少碎片和空间使用。
多节点的请查询资料,我没有环境/需求去尝试。这里还可以做字段的映射 mapping处理。
PUT /_template/index-*
{
"index_patterns" : "index*",
"order" : 0,
"settings" : {
"number_of_shards" : 3,
"number_of_replicas": 0
}
}
二、在日志监控端,修改配置文件。可以根据多个文件,配置多个项
- input输入项
filebeat.inputs:
## 配置输入源
- type: log
enabled: true
paths:
- /Users/chenhailong/logs/work.log
fileds:
source: work
- type: log
enabled: true
paths:
- /Users/chenhailong/logs/error.log
fields:
source: error
有时候日志文件会出现一些异常信息,格式较长,会换行,为了避免一行显示,做一些格式化的处理
查看日志的完整信息
multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'
multiline.negate: true
multiline.match: after
//解释:如果不是以日期开头的行信息则追加在后面
2. ouput输出项
##配置输出源
output.elasticsearch:
# Array of hosts to connect to.
hosts: ["localhost:9200"]
indices:
- index: "work-1-%{+yyyy.MM.dd}"
when:
contains:
fields.source: "work"
- index: "error-1-%{+yyyy.MM.dd}"
when:
contains:
fields.source: "error"
切记日志有新的内容写入,才能看到效果, 注意格式|注意格式|注意格式
根据每天的监控会产生如下对应的索引文件
三、根据不同的索引文件,可以进行快速的查询
如:查询最近5分钟的日志中带有 exception 或者 error关键字的信息
--根据条件查询数据
POST /filebeat*/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"@timestamp": {
"gte": "now-5m/m",
"lte": "now/m",
"format": "epoch_millis"
}
}
}
],
"filter": [
{
"multi_match": {
"type": "best_fields",
"query": "exception or error",
"lenient": true
}
}
]
}
}
}
以下是返回的结果
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 23,
"successful": 23,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
祝君成功
使用kibana的开发工具console做一些elasticsearch的基本查询和设置
原创文章,转载请标明本文链接: ELK环境搭建之filebeat根据不同的监控日志文件建立不同的索引