6.Filebeat的安装及收集日志到Elasticsearch并使用自定义索引
  IS4yhiOomKTv 2023年11月02日 24 0
利用 Filebeat 收集日志
Filebeat是用于转发和集中日志数据的轻量级传送程序.作为服务器上的代理安装,Filebeat监视指定的日志文件或位置,收集日志事件,并将它们转发到Elasticsearch或Logstash进行索引.
Logstash也可以直接收集日志,但需要安装JDK并且会占用至少500M以上的内存 
生产一般使用filebeat代替logstash, 基于go开发,部署方便,重要的是只需要10M多内存,比较节约资源

Filebeat 官方说明

https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-overview.html

Filebeat在机器上安装的代理软件,收集某个机器上的日志

Filebeat支持的数据来源
Multiline messages
AWS CloudWatch
AWS S3
Azure Event Hub
Azure Blob Storage
CEL
Cloud Foundry
CometD
Container
Entity Analytics
filestream
GCP Pub/Sub
HTTP Endpoint
HTTP JSON
journald
Kafka
Log
MQTT
NetFlow
Office 365 Management Activity API
Redis
Stdin                                   #标准输入
Syslog
TCP
UDP
Unix
Filebeat输出的目标
Elasticsearch Service
Elasticsearch
Logstash
Kafka
Redis
File
Console      #输出到控制台
注意: Filebeat 支持多个输入,但不支持同时有多个输出
安装 Filebeat 和配置说明
安装
https://mirrors.tuna.tsinghua.edu.cn/elasticstack/7.x/apt/pool/main/f/filebeat/
ubuntu安装:
[root@ubuntu2004 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/elasticstack/7.x/apt/pool/main/f/filebeat/filebeat-7.17.5-amd64.deb

rocky安装:
[root@rocky2004 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/elasticstack/7.x/yum/7.17.5/filebeat-7.17.5-x86_64.rpm
查看官方文档编写输入和输出
https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-overview.html
如:从标准输入读取再输出至标准输出
输入:查看官方文档的inputs中的Stdin标准输入格式
输出:查看官方文档的output中的Stdin标准输出格式

[root@ubuntu2004 ~]# vim /etc/filebeat/stdin.yml 
filebeat.inputs: 
- type: stdin 
  enabled: true 
output.console: 
  pretty: true 
  enable: true
  
手动执行filebeat命令触发文件的执行(内存消耗较小)
[root@ubuntu2004 ~]#filebeat -c -e /etc/filebeat/stdin.yml
.....
hello,es  #手动输入后回车,输出如下
{  
  "@timestamp": "2022-07-09T14:48:22.446Z",  
  "@metadata": {   
    "beat": "filebeat",  
    "type": "_doc",  
    "version": "7.17.5"
  }, 
  "agent": {   
    "name": "es-web01",  
    "type": "filebeat", 
    "version": "7.17.5",  
    "hostname": "es-web01",  
    "ephemeral_id": "8918da58-f04c-4b74-9dfe-4e53952c2530",  
    "id": "fffb2178-8110-400c-b3f9-ce8920aeb61a"
  },
  "message": "hello,es",   #真正的数据只有此行,其它都是Filebeat添加的元数据
  "log": { 
    "offset": 0, 
    "file": {    
      "path": "" 
    }
  }, 
  "input": { 
    "type": "stdin"
  }, 
  "ecs": {   
    "version": "1.12.0"
  }, 
  "host": {  
    "name": "es-web01"
  } 
}
从标准输入读取再输出至 Json 格式的文件
[root@ubuntu2004 ~]#vim /etc/filebeat/stdin_file.yml
filebeat.inputs: 
- type: stdin 
  enabled: true
  json.keys_under_root: true
output.file:
  path: "/tmp/filebeat"
  filename: filebeat

手动执行filebeat命令触发文件的执行
[root@ubuntu2004 ~]#filebeat -c -e /etc/filebeat/stdin_file.yml
.....
hello,es  #手动输入后回车

输出到文件中
[root@ubuntu2004 ~]# ls /tmp/filebeat/filebeat
[root@ubuntu2004 ~]# cat /tmp/filebeat/filebeat
{"@timestamp":"2022-08-01T13:53:48.259Z","@metadata": {"beat":"filebeat","type":"_doc","version":"7.17.5"},"host": {"name":"ubuntu2004.wang.org"},"agent": {"hostname":"ubuntu2004.wang.org","ephemeral_id":"e17c1494-889d-49fb-b3d7- 5cb573b1f152","id":"d1fdb707-5e4f-47b3-944b- 8f6d1477190a","name":"ubuntu2004.wang.org","type":"filebeat","version":"7.17.5"} ,"ecs":{"version":"1.12.0"},"log":{"offset":0,"file":{"path":""}},"json": {},"message":"","input":{"type":"stdin"}}
从文件读取再输出至标准输出
[root@ubuntu2004 ~]# vim /etc/filebeat/file.yml 
filebeat.inputs: 
  - type: log
    enabled: true 
    paths: 
    - /var/log/filebeat.log
output.console:
  pretty: true 
  enable: true
  
手动执行filebeat命令触发文件的执行
[root@ubuntu2004 ~]#filebeat -c -e /etc/filebeat/file.yml 
[root@ubuntu2004 ~]#echo "This is a test log" > /var/log/filebeat.log
收集系统日志到Elasticsearch
[root@ubuntu2004 ~]# vim /etc/filebeat/filebeat.yml 
filebeat.inputs: 
  - type: log
    enabled: true 
    paths: 
    - /var/log/filebeat.log
output.elasticsearch:
  hosts:["10.0.0.101:9200"]
重启filebeat  
[root@ubuntu2004 ~]# systemctl restart rsyslog.service

![image-20230628080413557](利用 Filebeat 收集日志.assets/image-20230628080413557.png)

自定义索引名称收集日志到Elasticsearch,定义不同的索引名区分不同的业务
[root@ubuntu2004 ~]# vim /etc/filebeat/filebeat.yml 
filebeat.inputs: 
  - type: log
    enabled: true 
    paths: 
    - /var/log/filebeat.log
output.elasticsearch:
  hosts:["10.0.0.101:9200"]
  index: "meng-%{[agent.version]}-%{+yyyy.MM.dd}"
setup.ilm.enabled: false        
setup.template.name: "meng"     
setup.template.pattern: "meng-*"
重启filebeat  
[root@ubuntu2004 ~]# systemctl restart rsyslog.service

上一次/var/log/filebeat.log文件已经被filebeat收集,内容相同则不会再次收集,需增加内容,让filebeat再次收集
cp /var/log/filebeat.log json.txt
cat json.txt >> /var/log/filebeat.log

![image-20230628081509183](利用 Filebeat 收集日志.assets/image-20230628081509183.png)

json.keys_under_root: true使输出信息转换成json格式,不再输出一堆
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  vxoexqgjyiCS   2023年11月19日   27   0   0 linuxvim数据
  YKMEHzdP8aoh   2023年11月30日   33   0   0 vimUDP
  xIUntf9oR6GI   2023年11月28日   31   0   0 sedvim基础命令
  mjtHZIki74si   2023年12月06日   32   0   0 ubuntubash
  vxoexqgjyiCS   2023年11月22日   25   0   0 linuxvimbash