linux 安装filebeat
-----------------------------------------------------------------------------------------------------------------------
基础概念
Filebeat 是一个轻量级的日志收集器,属于 Elastic Stack(以前称为 ELK Stack)的一部分。它负责从文件系统、日志文件或其他输入源收集日志数据,并将这些数据转发到 Elasticsearch 或 Logstash 进行进一步处理和分析。
优势
- 轻量级:Filebeat 占用资源少,适合在各种环境中运行。
- 易于配置:通过简单的 YAML 配置文件即可设置输入、输出和过滤器。
- 高可靠性:支持日志文件的轮转和断点续传,确保数据不会丢失。
- 集成性强:可以轻松集成到 Elastic Stack 中,与其他组件(如 Elasticsearch 和 Logstash)协同工作。
类型
Filebeat 主要有以下几种类型:
- 标准 Filebeat:用于收集日志文件中的数据。
- Metricbeat:用于收集系统和服务的性能指标。
- Packetbeat:用于收集网络流量数据。
应用场景
- 日志集中管理:将分散在各个服务器上的日志集中到一个地方进行管理和分析。
- 实时监控:通过收集和分析日志数据,实现系统的实时监控和告警。
- 安全审计:收集和分析系统日志,用于安全审计和合规性检查。
安装步骤
以下是在 Linux 系统上安装 Filebeat 的步骤:
使用包管理器安装
Debian/Ubuntu:
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
sudo apt-get install apt-transport-https
echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list
sudo apt-get update && sudo apt-get install filebeat
CentOS/RHEL:
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
sudo tee /etc/yum.repos.d/elastic.repo <<EOF
[elastic-7.x]
name=Elastic repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF
sudo yum install filebeat
手动安装
- 下载 Filebeat 安装包:
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.x.x-linux-x86_64.tar.gz
- 解压安装包:
tar -zxvf filebeat-7.x.x-linux-x86_64.tar.gz
- 移动解压后的文件到系统目录:
sudo mv filebeat-7.x.x-linux-x86_64 /usr/local/filebeat
- 创建 Systemd 服务文件:
sudo nano /etc/systemd/system/filebeat.service
在文件中添加以下内容:
[Unit]
Description=Filebeat sends log files to Logstash or Elasticsearch.
Documentation=https://www.elastic.co/guide/en/beats/filebeat/current/index.html
Wants=network-online.target
After=network-online.target
[Service]
User=root
Group=root
ExecStart=/usr/local/filebeat/filebeat -e -c /usr/local/filebeat/filebeat.yml
Restart=always
[Install]
WantedBy=multi-user.target
- 启动并启用 Filebeat 服务:
sudo systemctl daemon-reload
sudo systemctl enable filebeat
sudo systemctl start filebeat
常见问题及解决方法
问题:Filebeat 无法启动
原因: 可能是配置文件错误或权限问题。
解决方法:
- 检查配置文件
/usr/local/filebeat/filebeat.yml
是否正确。 - 确保 Filebeat 进程有足够的权限访问日志文件和配置文件。
sudo systemctl status filebeat
问题:Filebeat 无法连接到 Elasticsearch 或 Logstash
原因: 可能是网络问题或配置错误。
解决方法:
- 检查网络连接是否正常。
- 确保 Elasticsearch 或 Logstash 的地址和端口配置正确。
output.elasticsearch:
hosts: ["http://localhost:9200"]
-----------------------------------------------------------------------------------------------------------------------
filebeat 教程
1. filebeat安装
#rpm安装方式
https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.17.5-x86_64.rpm
rpm -ivh filebeat-7.17.5-x86_64.rpm
systemctl enable filebeat --now
#二进制包安装方式
1.下载
mkdir -p /data/tools/filebeat/ && cd /data/tools/filebeat/
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.17.5-linux-x86_64.tar.gz
tar -xvf filebeat-7.17.5-linux-x86_64.tar.gz
--------------这里第二部和第三步先不操作,案例部分和2,3冲突--------------------------------
2.使用systemctl管理filebeat服务
cat > /usr/lib/systemd/system/filebeat.service <<EOF
[Unit]
Description=es
After=network.target
[Service]
Type=simple
ExecStart=/data/tools/filebeat/filebeat-7.17.5-linux-x86_64/filebeat -c /data/tools/filebeat/filebeat-7.17.5-linux-x86_64/filebeat.yml
User=filebeat
LimitNOFILE=131070
[Install]
WantedBy=multi-user.target
EOF
3.重新加载systemctl,启动filebeat
systemctl daemon-reload
systemctl enable filebeat --now
2. filbeat案例
2.1. input插件之stdin:案例
https://www.elastic.co/guide/en/beats/filebeat/7.17/index.html
-------------------------------------------------------------------------------
1. filbeat的input插件之stdin案例
(1)创建工作目录
cd /data/tools/filebeat/filebeat-7.17.5-linux-x86_64
mkdir config
(2)编写配置文件
[root@elk1]# cat >> config/01-stdin-to-console.yaml <<EOF
# 配置filebeat的输入端
filebeat.inputs:
# 指定输入端的类型为标准输入
- type: stdin
# 指定filebeat的输出端为console
output.console:
# 表示输出的内容以漂亮的格式显示
pretty: true
EOF
(3)启动filebeat的实例
filebeat -e -c config/01-stdin-to-console.yaml
(4) 测试输入输出
输入: 232323231
输出:
{
........
"message": "232323231",
.....
}
2.2. input插件之tcp:案例
]# cat >> config/02-tcp-to-console.yaml <<EOF
# 配置filebeat的输入端
filebeat.inputs:
# 指定输入端的类型为tcp
- type: tcp
# 定义tcp监听的主机和端口
host: 0.0.0.0:8888
# 指定filebeat的输出端为console
output.console:
# 表示输出的内容以漂亮的格式显示
pretty: true
EOF
(3)启动filebeat的实例
./filebeat -e -c config/02-tcp-to-console.yaml
(4) 测试输入输出
输入: echo "1111"|nc 10.0.0.101 8888
输出:
{
......
"message": "1111",
.....
}
2.3. input的通用字段案例:
input的通用字段案例:
filebeat input插件的通用字段(common options):
- enabled:
是否启用该组件,有true和false,默认值为true。当设置为false时,表示该input组件不会被加载执行!
- tags:
给每条数据添加一个tags标签列表。
- fields
给数据添加字段。
- fields_under_root
该值默认值为false,将自定义的字段放在一个"fields"的字段中。若设置为true,则将fields的KEY放在顶级字段中。
- processors:
定义处理器,对源数据进行简单的处理。
参考链接:
https://www.elastic.co/guide/en/beats/filebeat/7.17/defining-processors.html
2.4. 综合案例:
5 综合案例:
[root@elk]# cat >> config/03-input_common_options-to-console.yaml <<EOF
filebeat.inputs:
- type: log
paths:
- /tmp/logtest/*.log
- /tmp/logtest/*/*.json
- /tmp/logtest/**/*.exe
# 是否启用该类型,默认值为true。
enabled: false
- type: tcp
enabled: true
host: "0.0.0.0:8888"
# 给数据打标签,会在顶级字段多出来多个标签
tags: ["2024","new","happy"]
# 给数据添加KEY-VALUE类型的字段,默认是放在"fields"中的
fields:
school: qinghua
class: "5-4"
classroom: "032"
ip: 219.141.136.10
port: 13306
# 若设置为true时,则将fields添加的自定义字段放在顶级字段中,默认值为false。
fields_under_root: true
# 定义处理器,过滤指定的数据
processors:
# 删除消息是以linux开头的事件(event)
- drop_event:
when:
regexp:
message: "^linux"
# 消息包含error内容事件(event)就可以删除自定义字段或者tags。无法删除内置的字段.
- drop_fields:
when:
contains:
message: "error"
fields: ["class","tags"]
ignore_missing: false
# 修改字段的名称
- rename:
fields:
# 源字段
- from: "school"
# 目标字段
to: "学校"
- from: "log"
to: "日志"
# 转换数据,将字段的类型转换对应的数据类型,并存放在指定的字段中,本案例将其放在"tom"字段中
- convert:
fields:
- {from: "ip", to: "tom.ip", type: "ip"}
- {from: "port", to: "tom.port", type: "integer"}
# 指定filebeat的输出端为console
output.console:
# 表示输出的内容以漂亮的格式显示
pretty: true
EOF
2.5. input插件之log(log插件在7.16版本后弃用)
2.5.1. input插件之log:采集常规数据
# filbeat的input插件之log案例(log插件在7.16版本后弃用)
(1)编写配置文件
[root@elk1]# cat >> config/04-log-to-console.yaml <<EOF
filebeat.inputs:
# 指定输入类型是log
- type: log
# 指定文件路径
paths:
# 一个*,只匹配logtest单层目录下面的以log结尾的文件
- /tmp/logtest/*.log
# 两个*,可以递归匹配logtest下面所有层级的以json结尾的文件
- /tmp/logtest/**/*.json
# 指定filebeat的输出端为console
output.console:
# 表示输出的内容以漂亮的格式显示
pretty: true
EOF
(2)启动filebeat实例
[root@elk1]# ./filebeat -e -c config/04-log-to-console.yaml
(3)测试数据
]# tree /tmp/logtest/
/tmp/logtest/
├── 1
│ ├── 1.json
│ └── 1.log
├── 1.json
└── 1.log
/tmp/logtest/*.log 收集/tmp/logtest/1.log, 不收集/tmp/logtest/1/1.log
/tmp/logtest/**/*.json 收集/tmp/logtest/1.json /tmp/logtest/1/1.json
2.5.2. input插件之log:采集json数据,指定数据采集,排除指定数据采集
6 包含指定数据采集,排除指定数据采集及json格式数据采集案例
[root@elk]# cat config/05-log-to-console.yaml
filebeat.inputs:
- type: log
paths:
- /tmp/logtest/*
# 排除以log结尾的文件
exclude_files: ['\.log$']
# 只采集包含指定信息的数据
# include_lines: ['linux']
# 只要包含特定的数据就不采集该事件(event)
# exclude_lines: ['^linux']
# 将message字段的json数据格式进行解析,并将解析的结果放在顶级字段中
json.keys_under_root: true
# 如果解析json格式失败,则会将错误信息添加为一个"error"字段输出
json.add_error_key: true
# 指定filebeat的输出端为console
output.console:
# 表示输出的内容以漂亮的格式显示
pretty: true
2.5.3. input插件之log:采集nginx原生格式日志
7 使用filebeat采集nginx日志
(1)搭建nginx环境
1.1 添加yum源
cat > /etc/yum.repos.d/nginx.repo <<'EOF'
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
1.2 安装nginx
yum -y install nginx
systemctl start nginx
(2)使用filebeat采集nginx日志
[root@elk1 filebeat-7.17.5-linux-x86_64]# cat >> config/06-log_nginx-to-console.yaml <<EOF
filebeat.inputs:
- type: log
paths:
- /var/log/nginx/access.log*
output.console:
# 表示输出的内容以漂亮的格式显示
pretty: true
EOF
[root@elk1 filebeat-7.17.5-linux-x86_64]# ./filebeat -e -c config/06-log_nginx-to-console.yaml
2.5.4. input插件之log:采集nginx的json格式日志
7 使用filebeat采集nginx的json格式日志
(1)修改nginx的配置文件
# vim /etc/nginx/nginx.conf
...
# log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
# access_log /var/log/nginx/access.log main;
log_format nginx_json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"SendBytes":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"uri":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"tcp_xff":"$proxy_protocol_addr",'
'"http_user_agent":"$http_user_agent",'
'"status":"$status"}';
access_log /var/log/nginx/access.log nginx_json;
(2)热加载nginx
systemctl reload nginx
> /var/log/nginx/access.log
(3)测试访问nginx
curl http://10.0.0.101/
(4)filebeat采集nginx的json格式日志
[root@elk1 filebeat-7.17.5-linux-x86_64]# cat > config/07-log_nginx_json-to-console.yaml <<EOF
filebeat.inputs:
- type: log
paths:
- /var/log/nginx/access.log*
json.keys_under_root: true
json.add_error_key: true
output.console:
# 表示输出的内容以漂亮的格式显示
pretty: true
EOF
(5)启动filebeat实例
[root@elk1 filebeat-7.17.5-linux-x86_64]# ./filebeat -e -c config/07-log_nginx_json-to-console.yaml
2.5.5. input插件之log:采集tomcat访问日志json格式
8 使用filebeat采集tomcat访问日志:
(1)安装tomcat
1.1 下载tomcat软件包
mkdir -p /data/tools/tomcat/ && cd /data/tools/tomcat/
wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.91/bin/apache-tomcat-9.0.91.tar.gz
1.2 解压软件包
tar xf apache-tomcat-9.0.91.tar.gz
(2)修改tomcat的配置文件
cd /data/tools/tomcat/apache-tomcat-9.0.91/conf
cp server.xml{,.bak}
vim server.xml
...(切换到行尾修改,大概是在133-149之间)
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="tomcat_access_log" suffix=".txt"
pattern="{"clientip":"%h","ClientUser":"%l","authenticated":"%u","AccessTime":"%t","request":"%r","status":"%s","SendBytes":"%b","Query?string":"%q","partner":"%{Referer}i","http_user_agent":"%{User-Agent}i"}"/>
(3)配置环境变量并启动tomcat服务
[root@elk1]# cat > /etc/profile.d/tomcat.sh <<'EOF'
#!/bin/bash
export TOMCAT_HOME=/data/tools/tomcat/apache-tomcat-9.0.91/
export PATH=$PATH:$TOMCAT_HOME/bin
EOF
[root@elk]# source /etc/profile.d/tomcat.sh
[root@elk]# catalina.sh start
(4)使用filebeat采集tomcat日志
[root@elk1 filebeat-7.17.5-linux-x86_64]# cat > config/08-log_tomcat-to-console.yaml <<EOF
filebeat.inputs:
- type: log
paths:
- /data/tools/tomcat/apache-tomcat-9.0.91/logs/tomcat_access_log*.txt
json.keys_under_root: true
json.add_error_key: true
output.console:
# 表示输出的内容以漂亮的格式显示
pretty: true
EOF
[root@elk103.oldboyedu.com filebeat-7.17.5-linux-x86_64]#
(5)启动filebeat实例
[root@elk1 filebeat-7.17.5-linux-x86_64]# ./filebeat -e -c config/08-log_tomcat-to-console.yaml
2.5.6. input插件之log: 采集-tomcat的启动日志中错误日志,多行匹配案例
https://www.elastic.co/guide/en/beats/filebeat/7.17/multiline-examples.html
9 采集tomcat的错误日志多行匹配案例
[root@elk1 filebeat-7.17.5-linux-x86_64]# cat > config/09-log-tomcat_error-to-es.yaml <<EOF
filebeat.inputs:
- type: log
paths:
- /data/tools/tomcat/apache-tomcat-9.0.91/logs/catalina*
multiline.type: pattern
multiline.pattern: '^\d{2}'
multiline.negate: true
multiline.match: after
# 指定输出端为ES集群
output.elasticsearch:
hosts: ["http://10.0.0.101:9200","http://10.0.0.102:9200","http://10.0.0.103:9200"]
EOF
#参数解释:
negate英语翻译是取反
multiline.negate: false|true, flase表匹配,true表示不匹配
multiline.match:before|after
用法说明:
1.当multiline.negate为false时:
multiline.match为after 表示 【匹配的行】 会被放到 上一个【不匹配的行】 【后】,形成一行
multiline.match为before 表示 【匹配的行】 会被放到 下一个【不匹配的行】 【前】,形成一行
2.当multiline.negate为true时:
multiline.match为after 表示 【不匹配的行】 会被放到 上一个【匹配的行】 【后】,形成一行
multiline.match为before 表示 【不匹配的行】 会被放到 下一个【匹配的行】 【前】,形成一行
#启动filebeat
[root@elk1 filebeat-7.17.5-linux-x86_64]# ./filebeat -e -c config/09-log-tomcat_error-to-es.yaml
2.6. input插件之filestream(替代log插件)
2.6.1. input插件之filestream: output到console
11 filebeat的input类型之filestream实战案例:
[root@elk1 filebeat-7.17.5-linux-x86_64]# ]# cat config/11-filestream-to-console.yaml
filebeat.inputs:
# 指定类型为filestream,在7.16版本中已经弃用log类型
- type: filestream
enabled: false
paths:
- /tmp/logtest/1.log
parsers:
#配置解析多行模式
- multiline:
type: pattern
pattern: '^\['
negate: true
match: after
- type: filestream
enabled: true
paths:
- /tmp/logtest/student.log
# 配置解析
parsers:
# 配置json格式解析
- ndjson:
# 将错误消息记录到error字段中
add_error_key: true
# 如果解析的json格式字段和filebeat内置的顶级字段冲突,则覆盖,默认是不覆盖的。
overwrite_keys: true
# 将message解析的字段放入一个自定义的字段下。若不指定该字段,则默认解析的键值对会在顶级字段.
target: "student"
output.console:
pretty: true
EOF
[root@elk1 filebeat-7.17.5-linux-x86_64]# ./filebeat -e -c config/11-filestream-to-console.yaml
2.6.2. input插件之filestream,output到logstash
#input插件之filestream-收集ningx日志,output的logstash插件发送到logstash
[root@elk1filebeat-7.17.5-linux-x86_64]# cat > config/15-filestream-nginx-to-logstash.yaml <<EOF
filebeat.inputs:
# 指定类型为filestream,在7.16版本中已经弃用log类型
- type: filestream
enabled: true
paths:
- /var/log/nginx/access.log*
# 配置解析
parsers:
# 配置json格式解析
- ndjson:
# 将错误消息记录到error字段中
add_error_key: true
# 如果解析的json格式字段和filebeat内置的顶级字段冲突,则覆盖,默认是不覆盖的。
overwrite_keys: true
# 将数据输出到logstash中
output.logstash:
# 指定logstash的主机和端口
hosts: ["10.0.0.101:8888"]
EOF
[root@elk103.oldboyedu.com filebeat-7.17.5-linux-x86_64]#
[root@elk103.oldboyedu.com filebeat-7.17.5-linux-x86_64]# ./filebeat -e -c config/15-filestream-nginx-to-logstash.yaml
2.7. input插件之container: 采集docker日志
10 使用filebeat采集docker日志
(1)安装docker
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
yum makecache fast
yum -y install docker-ce docker-compose
(2)配置docker的镜像加速,启动服务
[root@elk1]# cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://2k3sfxf3.mirror.aliyuncs.com"]
}
EOF
[root@elk1 ~]# systemctl enable --now docker
(3)下载nginx镜像
docker run -dp 88:80 --name mynginx --restart always nginx:alpine
docker run -dp 89:8080 --name mytomcat --restart always tomcat:jre8-alpine
(4)使用filebeat采集容器日志
方式一:(此方法从[7.2.0]版本开始已弃用)
[root@elk1 filebeat-7.17.5-linux-x86_64]# cat >config/10-docker-to-console.yaml <<EOF
filebeat.inputs:
# 指定输入类型为docker类型
- type: docker
# 指定容器的ID
containers.ids:
- '*'
output.console:
pretty: true
EOF
方式二:(推荐使用)
[root@elk1 filebeat-7.17.5-linux-x86_64]# cat >config/10-docker-to-console.yaml <<EOF
filebeat.inputs:
- type: container
paths:
- '/var/lib/docker/containers/*/*.log'
output.console:
pretty: true
EOF
(5)启动filebeat实例
[root@elk1 filebeat-7.17.5-linux-x86_64]# ./filebeat -e -c config/10-docker-to-console.yaml
2.8. output插件之file-将数据写入到本地文件案例
12 将数据写入到本地文件案例
[root@elk1 filebeat-7.17.5-linux-x86_64]# cat > config/12-stdin-to-file.yaml <<EOF
filebeat.inputs:
- type: stdin
# 指定输出的类型为本地文件
output.file:
# 指定文件存储的路径
path: "/tmp/tom/"
# 指定文件的名称
filename: stdin.log
EOF
[root@elk1 filebeat-7.17.5-linux-x86_64]# ./filebeat -e -c config/12-stdin-to-file.yaml
2.9. output插件之elasticsearch:写入数据到ES集群
13 写入数据到ES集群
[root@elk1 filebeat-7.17.5-linux-x86_64]# cat > config/13-filestream-to-es.yaml <<EOF
filebeat.inputs:
- type: filestream
enabled: true
paths:
- /tmp/logtest/shopping.json
parsers:
- ndjson:
add_error_key: true
overwrite_keys: true
output.elasticsearch:
# 指定ES集群地址
hosts:
- "http://10.0.0.101:9200"
- "http://10.0.0.102:9200"
- "http://10.0.0.103:9200"
# 指定索引
index: "shopping-%{+yyyy.MM.dd}"
#连接es的用户名密码
username: "filebeat-user"
password: "123456"
# 禁用索引声明管理周期,若不禁用则自动忽略自定义索引名称
setup.ilm.enabled: false
# 设置索引模板的名称
setup.template.name: "shopping"
# 指定索引模板的匹配模式
setup.template.pattern: "shopping-*"
# 是否覆盖原有的索引模板
setup.template.overwrite: true
# 设置索引模板
setup.template.settings:
# 指定分片数量为8
index.number_of_shards: 8
# 指定副本数量为0
index.number_of_replicas: 0
EOF
[root@elk1 filebeat-7.17.5-linux-x86_64]# ./filebeat -e -c config/13-filestream-to-es.yaml
2.10. output插件之elasticsearch:将多个数据源写入到ES集群不同索引
14 将多个数据源写入到ES集群不同索引
[root@elk1 filebeat-7.17.5-linux-x86_64]# cat > config/14-filestream-indices-to-es.yaml <<EOF
filebeat.inputs:
- type: filestream
enabled: true
tags: "docker"
paths:
- /tmp/logtest/docker.json
parsers:
- ndjson:
add_error_key: true
overwrite_keys: true
- type: filestream
enabled: true
tags: "linux"
paths:
- /tmp/logtest/linux.log
parsers:
#配置解析多行模式
- multiline:
type: pattern
pattern: '^\['
negate: true
match: after
output.elasticsearch:
hosts:
- "http://10.0.0.101:9200"
- "http://10.0.0.102:9200"
- "http://10.0.0.103:9200"
#连接es的用户名密码
username: "filebeat-user"
password: "123456"
indices:
- index: "tom-docker-%{+yyyy.MM.dd}"
when.contains:
tags: "docker"
- index: "tom-linux-%{+yyyy.MM.dd}"
when.contains:
tags: "linux"
setup.ilm.enabled: false
setup.template.name: "tom"
setup.template.pattern: "tom-*"
setup.template.overwrite: true
setup.template.settings:
index.number_of_shards: 3
index.number_of_replicas: 0
EOF
[root@elk1 filebeat-7.17.5-linux-x86_64]# filebeat -e -c config/14-filestream-indices-to-es.yaml
2.11. output插件之kafka
filebeat将数据写入到Kafka实战:
[root@elk101 filebeat-7.17.5-linux-x86_64]# cat > config/17-stdin-to-kafka.yaml <<EOF
filebeat.inputs:
- type: stdin
# 将数据输出到kafka
output.kafka:
# 指定kafka主机列表
hosts:
- 10.0.0.101:9092
- 10.0.0.102:9092
- 10.0.0.103:9092
# 指定kafka的topic
topic: "topic-1"
EOF
[root@elk101 filebeat-7.17.5-linux-x86_64]# ./filebeat -e -c config/17-stdin-to-kafka.yaml
3. config.inputs插件:包含多个子配置文件
filebeat.config.inputs:
enabled: true
#指定子配置文件的路径
path: configs/*.yaml
#是否实时重载,配置文件变化时,不需要重启filebeat
reload.enabled: true
#实时重载扫描间隔时间
reload.period: 10s
]# cat filebeat.yml
filebeat.config.inputs:
enabled: true
#指定子配置文件的路径
path: configs/*.yaml
#是否实时重载,配置文件变化时,不需要重启filebeat
reload.enabled: true
#实时重载扫描间隔时间
reload.period: 10s
[root@elk1 filebeat-7.17.5-linux-x86_64]# tree config/
config/
├── 01-stdin-to-console.yaml
├── 02-tcp-to-console.yaml
├── 04-log-to-console.yaml
-----------------------------------------------------------------------------------------------------------------------
Filebeat 是 Elastic Stack 中的一个轻量级日志数据收集工具,用于将日志文件中的数据发送到 Elasticsearch 或 Logstash 进行进一步处理和分析。以下是 Filebeat 的配置详解,帮助你快速上手和定制 Filebeat。
1. Filebeat 配置文件结构
Filebeat 的配置文件通常是 filebeat.yml
,位于 Filebeat 安装目录下。配置文件采用 YAML 格式,主要包含以下部分:
-
全局配置:设置 Filebeat 的全局行为。
-
输入配置:定义日志文件的来源。
-
输出配置:定义日志数据的发送目标(如 Elasticsearch、Logstash 等)。
-
处理器配置:定义对日志数据的处理规则。
-
模块配置:启用和配置 Filebeat 的预定义模块(如 Nginx、MySQL 等)。
2. 全局配置
全局配置用于设置 Filebeat 的基本行为。
# filebeat.yml
# Filebeat 名称(可选)
name: "my-filebeat-instance"
# 日志输出级别(debug, info, warning, error)
logging.level: info
# 日志输出路径(默认在控制台输出)
logging.to_files: true
logging.files:
path: /var/log/filebeat
name: filebeat.log
keepfiles: 7
permissions: 0644
3. 输入配置
输入配置用于定义 Filebeat 从哪些文件或日志源收集数据。
基本输入配置
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
- /var/log/nginx/*.log
-
type
:输入类型,通常为log
(文件日志)。 -
enabled
:是否启用该输入。 -
paths
:日志文件的路径(支持通配符)。
高级输入配置
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/application.log
fields:
app: myapp
env: production
fields_under_root: true
multiline.pattern: '^\['
multiline.negate: true
multiline.match: after
-
fields
:添加自定义字段。 -
fields_under_root
:是否将自定义字段添加到根级别。 -
multiline
:处理多行日志(如 Java 堆栈跟踪)。
4. 输出配置
输出配置用于定义 Filebeat 将数据发送到哪里。
输出到 Elasticsearch
output.elasticsearch:
hosts: ["http://localhost:9200"]
username: "elastic"
password: "your_password"
index: "filebeat-%{[agent.version]}-%{+yyyy.MM.dd}"
-
hosts
:Elasticsearch 的地址。 -
username
和password
:Elasticsearch 的认证信息。 -
index
:指定索引名称(支持动态变量)。
输出到 Logstash
output.logstash:
hosts: ["localhost:5044"]
-
hosts
:Logstash 的地址。
输出到 Kafka
output.kafka:
hosts: ["kafka1:9092", "kafka2:9092"]
topic: "filebeat-logs"
required_acks: 1
-
hosts
:Kafka 的地址。 -
topic
:Kafka 的主题。
输出到控制台(调试用)
output.console:
pretty: true
5. 处理器配置
处理器用于在发送数据之前对日志进行处理。
添加字段
processors:
- add_fields:
target: ""
fields:
env: "production"
删除字段
processors:
- drop_fields:
fields: ["beat.version", "input.type"]
重命名字段
processors:
- rename:
fields:
- from: "message"
to: "log_message"
6. 模块配置
Filebeat 提供了许多预定义的模块(如 Nginx、MySQL、Apache 等),用于快速收集和分析特定应用的日志。
启用模块
filebeat modules enable nginx
配置模块
filebeat.modules:
- module: nginx
access:
enabled: true
var.paths: ["/var/log/nginx/access.log*"]
error:
enabled: true
var.paths: ["/var/log/nginx/error.log*"]
7. 完整配置示例
以下是一个完整的 Filebeat 配置示例:
# filebeat.yml
# 全局配置
name: "my-filebeat-instance"
logging.level: info
logging.to_files: true
logging.files:
path: /var/log/filebeat
name: filebeat.log
keepfiles: 7
permissions: 0644
# 输入配置
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/application.log
fields:
app: myapp
env: production
fields_under_root: true
multiline.pattern: '^\['
multiline.negate: true
multiline.match: after
# 输出配置
output.elasticsearch:
hosts: ["http://localhost:9200"]
username: "elastic"
password: "your_password"
index: "filebeat-%{[agent.version]}-%{+yyyy.MM.dd}"
# 处理器配置
processors:
- add_fields:
target: ""
fields:
env: "production"
- drop_fields:
fields: ["beat.version", "input.type"]
# 模块配置
filebeat.modules:
- module: nginx
access:
enabled: true
var.paths: ["/var/log/nginx/access.log*"]
error:
enabled: true
var.paths: ["/var/log/nginx/error.log*"]
8. 启动 Filebeat
-
测试配置文件是否正确:
filebeat test config
-
启动 Filebeat:
filebeat -e
-
-e
:将日志输出到控制台(用于调试)。
-
9. 常见问题
-
权限问题:确保 Filebeat 有权限读取日志文件。
-
日志格式问题:使用
multiline
配置处理多行日志。 -
输出目标不可达:检查 Elasticsearch、Logstash 或 Kafka 是否正常运行。
-----------------------------------------------------------------------------------------------------------------------
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
2022-02-25 【JMeter】---入门
2022-02-25 【TCP协议】---协议详解
2022-02-25 【HTTP协议】---HTTP协议详解
2022-02-25 MySQL为Null导致的四大坑
2022-02-25 高并发-总结列表
2022-02-25 缓存使用需要考虑的一些细节
2022-02-25 这12种场景Spring事务会失效!