赞
踩
Filebeat是一个轻量级的日志采集器
当面对成百上千、甚至成千上万的服务器、虚拟机和容器生成的日志时,Filebeat可以提供一种轻量型方法,用于转发和汇总日志与文件,关于Filebeat的记住以下两点:
用于监控、收集服务器日志文件.
流程如下:
可以直接wget下载
# 进入文件夹
cd /opt/elk/
# 下载
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-8.8.1-linux-x86_64.tar.gz
# 解压文件
tar -zxvf filebeat-8.8.1-linux-x86_64.tar.gz
# 重命名
mv filebeat-8.8.1-linux-x86_64 filebeat
然后进入到filebeat目录下,创建对应的配置文件
# 进入文件夹
cd filebeats
# 创建配置文件
vim shengxia.yml
添加如下内容
filebeat.inputs: # filebeat input输入
- type: stdin # 标准输入
enabled: true # 启用标准输入
setup.template.settings:
index.number_of_shards: 3 # 指定下载数
output.console: # 控制台输出
pretty: true # 启用美化功能
enable: true
在我们添加完配置文件后,我们就可以对filebeat进行启动了
./filebeat -e -c shengxia.yml
然后我们在控制台输入hello,就能看到我们会有一个json的输出,是通过读取到我们控制台的内容后输出的,内容如下
{ "@timestamp": "2023-05-31T22:57:58.700Z", "@metadata": {#元数据信息 "beat": "filebeat", "type": "_doc", "version": "8.8.1" }, "log": { "offset": 0, "file": { "path": "" } }, "message": "hello",#元数据信息 "input": {#控制台标准输入 "type": "stdin"#元数据信息 }, "ecs": { "version": "8.0.0" }, "host": { "name": "elk-node1" }, "agent": {#版本以及主机信息 "id": "5d5e4b99-8ee3-42f5-aae3-b0492d723730", "name": "elk-node1", "type": "filebeat", "version": "8.8.1", "ephemeral_id": "24b4fd16-5466-4d7e-b4b8-b73d41f77de0" } }
再次创建一个文件,叫 shengxia-log.yml,然后在文件里添加如下内容
filebeat.inputs:
- type: log
enabled: true
paths:
- /opt/elk/logs/*.log
setup.template.settings:
index.number_of_shards: 3
output.console:
pretty: true
enable: true
添加完成后,我们在到下面目录创建一个日志文件
# 创建文件夹
mkdir -p /opt/elk/logs
# 进入文件夹
cd /opt/elk/logs
# 追加内容
echo "hello world" >> test.log
然后再次启动filebeat
./filebeat -e -c shengxia-log.yml
能够发现,它已经成功加载到了我们的日志文件 test.log
同时我们还可以继续往文件中追加内容
echo "are you ok ?" >> a.log
追加后,我们再次查看filebeat,也能看到刚刚我们追加的内容
可以看出,已经检测到日志文件有更新,立刻就会读取到更新的内容,并且输出到控制台。
当我们的元数据没办法支撑我们的业务时,我们还可以自定义添加一些字段
filebeat.inputs:
- type: log
enabled: true
paths:
- /opt/elk/logs/*.log
tags: ["web", "test"] #添加自定义tag,便于后续的处理
fields: #添加自定义字段
from: web-test
fields_under_root: true #true为添加到根节点,false为添加到子节点中
setup.template.settings:
index.number_of_shards: 3
output.console:
pretty: true
enable: true
添加完成后,重启 filebeat
./filebeat -e -c shengxia-log.yml
然后添加新的数据到 test.log中
echo "Today is nice" >> test.log
我们就可以看到字段在原来的基础上,增加了两个
我们可以通过配置,将修改成如下所示
filebeat.inputs:
- type: log
enabled: true
paths:
- /opt/elk/logs/*.log
tags: ["web", "test"]
fields:
from: web-test
fields_under_root: false
setup.template.settings:
index.number_of_shards: 1
output.elasticsearch:
hosts: ["192.168.40.150:9200","192.168.40.137:9200","192.168.40.138:9200"]
启动成功后,我们就能看到它已经成功连接到了es了
然后我们到刚刚的 logs文件夹向 a.log文件中添加内容
echo "Elastic test" >> test.log
在ES中,我们可以看到,多出了一个 filebeat的索引库
然后我们浏览对应的数据,看看是否有插入的数据内容
Filebeat主要由下面几个组件组成: harvester、prospector 、input
prospector负责管理harvester并找到所有要读取的文件来源
如果输入类型为日志,则查找器将查找路径匹配的所有文件,并为每个文件启动一个harvester
Filebeat目前支持两种prospector类型:log和stdin
Filebeat如何保持文件的状态
一个input负责管理harvester,并找到所有要读取的源
如果input类型是log,则input查找驱动器上与已定义的log路径匹配的所有文件,并为每个文件启动一个harvester
每个input都在自己的Go例程中运行
下面的例子配置Filebeat从所有匹配指定的log模式的文件中读取行
filebeat.inputs:
- type: log
paths:
- /var/log/*.log
- /var/path2/*.log
./filebeat -e -c shengxia-es.yml
./filebeat -e -c shengxia-es.yml -d "publish"
部署教程可以参考这篇博客:Nginx的安装启动和停止_
启动完成后,我们通过下面命令,就可以获取到nginx中的内容了
[root@elk-node1 sbin]# tail -f /usr/local/nginx1/logs/access.log
192.168.40.1 - - [01/Jun/2023:05:13:23 +0800] "GET / HTTP/1.1" 200 615 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.43"
192.168.40.1 - - [01/Jun/2023:05:13:23 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.40.150:8080/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.43"
创建一个 nginx-log.yml配置文件,启动nginx的模块功能
./filebeat modules enable nginx
nginx-log.yml配置文件内容
filebeat.inputs:
- type: log
enabled: true
paths:
- /usr/local/nginx1/logs/*.log #nginx日志存放路径
tags: ["nginx"]
fields_under_root: false
setup.template.settings:
index.number_of_shards: 1
output.elasticsearch:
hosts: ["192.168.40.150:9200","192.168.40.137:9200","192.168.40.138:9200"]
可以看到,在message中已经获取到了nginx的日志,但是,内容并没有经过处理,只是读取到原数据。
要想实现日志数据的读取以及处理都是自己手动配置的,其实,在Filebeat中,有大量的Module,可以简化我们的配置,直接就可以使用,如下:
查看模块列表
./filebeat modules list
你可以看到,内置了很多的module,但是都没有启用,如果需要启用需要进行enable
操作:
#启动
./filebeat modules enable nginx
#禁用
./filebeat modules disable nginx
我们到下面的目录,就能看到module的配置了
# 进入到module目录
cd modules.d/
#查看文件
vim nginx.yml
得到的文件内容如下所示
# Module: nginx # Docs: https://www.elastic.co/guide/en/beats/filebeat/7.9/filebeat-module-nginx.html - module: nginx # Access logs access: enabled: true # 添加日志文件 var.paths: ["/usr/local/nginx1/logs/access.log*"] # Set custom paths for the log files. If left empty, # Filebeat will choose the paths depending on your OS. #var.paths: # Error logs error: enabled: true var.paths: ["/usr/local/nginx1/logs/error.log*"]
我们需要修改刚刚的nginx-log.yml文件,然后添加到我们的module
filebeat.inputs:
setup.template.settings:
index.number_of_shards: 1
output.elasticsearch:
hosts: ["192.168.40.150:9200","192.168.40.137:9200","192.168.40.138:9200"]
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
我们启动我们的filebeat
./filebeat -e -c nginx-log.yml
如果启动的时候发现出错
#只要执行下面两条命令就可以,新版本的ES不会报错
ERROR fileset/factory.go:142 Error loading pipeline: Error loading pipeline for
fileset nginx/access: This module requires the following Elasticsearch plugins:
ingest-user-agent, ingest-geoip. You can install them by running the following
commands on all the Elasticsearch nodes:
sudo bin/elasticsearch-plugin install ingest-user-agent
sudo bin/elasticsearch-plugin install ingest-geoip
#将elasticsearch.yaml文件进行修改成以下内容
#node.roles: [master,data,ingest]
500 Internal Server Error: {\"error\":{\"root_cause\":[{\"type\":\"illegal_state_exception\",\"reason\":\"There are no ingest nodes in this cluster, unable to forward request to an ingest node.\"}],\"type\":\"illegal_state_exception\",\"reason\":\"There are no ingest nodes in this cluster, unable to forward request to an ingest node.\"},\"status\":500}","service.name":"filebeat","ecs.version":"1.6.0"}
启动成功后,能看到日志记录已经成功刷新进去了
我们可以测试一下,刷新nginx页面,或者向错误日志中,插入数据
echo "err" >> error.log
能够看到,刚刚的记录已经成功插入了
关于module的其它使用,可以参考文档:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。