使用filebeat收集日志到elasticsearch

1.引言

  • 环境:

elastic stack 版本:6.5.1
使用以下组件
--elasticsearch 
--kibana
--filebeat

服务程序日志保存目录:/home/liujg/dev/crush-backend-cpp/crush/gateway/bin/Debug.
有多个rotation日志文件,日志文件名后缀".log".
日志记录格式:行号|时间戳|进程ID|线程ID|日志级别|消息内容

  • 目标

利用filebeat把服务日志逐条提取,写入elasticsearch,通过kibana查看.


2.配置filebeat

filebeat配置/etc/filebeat/filebeat.yml内容如下:

filebeat.inputs:
- input_type: log
  paths:
    - /home/liujg/dev/crush-backend-cpp/crush/gateway/bin/Debug/*.log
  tags: ["my-service", "hardware", "test"]
  fields: {project: "myproject", instance-id: "574734885120952459"}
  
output.elasticsearch:
  hosts: ["localhost:9200"]
  pipeline: "test-pipeline"

paths:提取的日志文件,所有*.log文件
tags,fields: 都用于标记日志,供日志查询过滤。可用于区分日志的服务类型,服务实例.(倾向用fields)


tags和fields的区别可参考(未见权威定论,仅供参考)
Tags vs Fields
https://discuss.elastic.co/t/tags-vs-fields/24014

可以配置多个input.每个有自己的fields,tags. (一个filebeat可以处理多个服务的日志)
Configure inputs
https://www.elastic.co/guide/en/beats/filebeat/master/configuration-filebeat-options.html
https://www.elastic.co/guide/en/beats/filebeat/master/filebeat-input-log.html


3.清除elasticsearch数据

操作elasticsearch索引.

  • 查看索引
$curl 'localhost:9200/_cat/indices?v'

返回内容:

health status index                     uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .kibana_1                 3lHZbBZDTYKJbw6dT0wWBQ   1   0          3            0      9.1kb          9.1kb
yellow open   filebeat-6.5.1-2018.12.04 NxDy6YLPTb-dWC8ldOlD_w   3   1          2            0     12.4kb         12.4kb
  • 删除索引

删除指定索引.

$ curl -XDELETE 'localhost:9200/filebeat-6.5.1-2018.12.04

删除file-*索引后,通过浏览器查看kibana,提示Looks like you don't have any logging indices.
表明日志已清空.


4.生成日志

启动程序生成日志文件,文件名为2018-12-05_0000.log。
内容如下:

1|2018-12-05, 17:12:16.823414|8020|140509537564864|debug|A debug severity messag
2|2018-12-05, 17:12:16.833594|8020|140509242611456|error|connect to host:192.168.88.135port:3079failed.error:Connection refused

5.验证

验证数据进入elasticsearch.
分别通过api和kibana查看.

  • 查看索引
curl 'localhost:9200/_cat/indices?v'

返回内容:

health status index                     uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .kibana_1                 3lHZbBZDTYKJbw6dT0wWBQ   1   0          3            0      9.1kb          9.1kb
yellow open   filebeat-6.5.1-2018.12.05 tFzyRYRcTPKVRnVY0ekzaA   3   1          2            0      6.7kb          6.7kb

打开kibana页面
http://192.168.21.190:5601

查看日志,可见新增的日志内容.
提取出的字段,tags,fields都可以在发现页面进行过滤。


6.索引查询命令

  • 查询索引

查询全部内容

$ curl 'localhost:9200/filebeat-6.5.1-2018.12.04/_search?q=*&pretty'

q=*表示查询所有内容.
pretty:漂亮的格式

  • 统计
$ curl 'localhost:9200/filebeat-6.5.1-2018.12.04/_count?pretty'
$ curl 'localhost:9200/filebeat-*/_count?pretty'
  • 条件查询
$ curl -H'Content-Type: application/json'  -XGET 'http://localhost:9200/filebeat-6.5.1-2018.12.04/_search?pretty=true' -d'
{
  "query" : { "match" : { "message" : "info目录" }}
}
'

猜你喜欢

转载自blog.csdn.net/wherwh/article/details/84848208