Beats配置详解
大约 4 分钟ELK日志收集技术Beats配置管理
Beats配置详解
Beats配置文件结构
Beats的主要配置文件位于安装目录下的/etc/
或config/
目录中,主要包括:
- filebeat.yml:Filebeat主配置文件
- metricbeat.yml:Metricbeat主配置文件
- packetbeat.yml:Packetbeat主配置文件
- winlogbeat.yml:Winlogbeat主配置文件
- auditbeat.yml:Auditbeat主配置文件
- heartbeat.yml:Heartbeat主配置文件
通用配置选项
1. 基本配置
节点配置:
# 节点名称
name: "filebeat-node-1"
# 标签
tags: ["service-X", "web-tier"]
# 自定义字段
fields:
env: production
datacenter: dc1
# 字段添加到根级别
fields_under_root: false
输出配置:
# 输出到Elasticsearch
output.elasticsearch:
hosts: ["localhost:9200"]
username: "filebeat_internal"
password: "password"
index: "filebeat-%{[agent.version]}-%{+yyyy.MM.dd}"
# 输出到Logstash
output.logstash:
hosts: ["localhost:5044"]
ssl.certificate_authorities: ["/etc/filebeat/certs/logstash-ca.crt"]
ssl.certificate: "/etc/filebeat/certs/filebeat.crt"
ssl.key: "/etc/filebeat/certs/filebeat.key"
进程器配置:
# 全局处理器
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
- add_docker_metadata: ~
- add_kubernetes_metadata: ~
2. 网络配置
网络设置:
# 网络超时
max_procs: 1
# 网络超时设置
queue.mem.events: 4096
queue.mem.flush.min_events: 2048
queue.mem.flush.timeout: 1s
# 网络缓冲区
output.elasticsearch.bulk_max_size: 1024
output.elasticsearch.flush_interval: 1s
3. 日志配置
# 日志级别
logging.level: info
logging.to_files: true
logging.files:
path: /var/log/filebeat
name: filebeat
keepfiles: 7
permissions: 0644
Filebeat配置详解
1. 输入配置
日志文件输入:
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
- /var/log/nginx/*.log
- /var/log/apache/*.log
fields:
log_type: nginx_access
fields_under_root: true
encoding: utf-8
ignore_older: 72h
scan_frequency: 10s
harvester_buffer_size: 16384
max_bytes: 10485760
multiline.pattern: '^\['
multiline.negate: false
multiline.match: after
标准输入:
- type: stdin
enabled: true
fields:
input_type: stdin
UDP输入:
- type: udp
enabled: true
host: "localhost:8080"
fields:
input_type: udp
2. 模块配置
启用模块:
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
reload.period: 10s
Nginx模块配置:
- module: nginx
access:
enabled: true
var.paths: ["/var/log/nginx/access.log*"]
error:
enabled: true
var.paths: ["/var/log/nginx/error.log*"]
3. 多行日志处理
filebeat.inputs:
- type: log
multiline.pattern: '^[[:space:]]+(at|\.{3})|^[[:space:]]+|^$'
multiline.negate: false
multiline.match: after
multiline.timeout: 5s
multiline.max_lines: 500
multiline.flush_pattern: '^[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}'
Metricbeat配置详解
1. 模块配置
系统模块:
metricbeat.modules:
- module: system
metricsets:
- cpu
- memory
- network
- diskio
- filesystem
- process
enabled: true
period: 10s
processes: ['.*']
process.include_top_n:
by_cpu: 5
by_memory: 5
Docker模块:
- module: docker
metricsets:
- container
- cpu
- diskio
- healthcheck
- info
- memory
- network
hosts: ["unix:///var/run/docker.sock"]
enabled: true
period: 10s
Nginx模块:
- module: nginx
metricsets: ["stubstatus"]
enabled: true
period: 10s
hosts: ["http://127.0.0.1/nginx_status"]
2. 自定义指标收集
metricbeat.modules:
- module: prometheus
metricsets: ["collector"]
enabled: true
period: 10s
hosts: ["localhost:9090"]
metrics_path: /metrics
namespace: prometheus
Packetbeat配置详解
1. 网络接口配置
packetbeat.interfaces:
device: any
type: af_packet
snaplen: 1514
buffer_size_mb: 100
with_vlans: true
bpf_filter: "port 80 or port 443"
2. 协议配置
HTTP协议:
packetbeat.protocols:
- type: http
ports: [80, 8080, 8000, 5000, 8002]
send_request: true
send_response: true
hide_keywords: ["password", "pass", "authorization"]
include_body_for: ["text/html"]
MySQL协议:
- type: mysql
ports: [3306]
max_rows: 10
max_row_length: 1024
Redis协议:
- type: redis
ports: [6379]
Winlogbeat配置详解
1. 事件日志配置
winlogbeat.event_logs:
- name: Application
level: error
ignore_older: 72h
- name: Security
level: information
event_id: 4624, 4625, 4634
- name: System
level: warning
providers:
- Microsoft-Windows-Security-Auditing
2. 自定义事件日志
- name: Microsoft-Windows-PowerShell/Operational
event_id: 4103, 4104
tags: ["powershell"]
fields:
log_type: powershell
Auditbeat配置详解
1. 审计模块配置
审计守护进程:
auditbeat.modules:
- module: auditd
enabled: true
audit_rules: |
# 监控文件访问
-w /etc/passwd -p wa -k identity
-w /etc/group -p wa -k identity
-w /etc/shadow -p wa -k identity
# 监控系统调用
-a always,exit -F arch=b64 -S execve -k exec
# 监控网络连接
-a always,exit -F arch=b64 -S connect -k network
文件完整性:
- module: file_integrity
enabled: true
paths:
- /bin
- /usr/bin
- /sbin
- /usr/sbin
- /etc
scan_at_start: true
scan_rate_per_sec: 50 MiB
max_file_size: 100 MiB
hash_types: [sha1]
安全配置
1. SSL/TLS配置
output.elasticsearch:
hosts: ["https://localhost:9200"]
ssl.enabled: true
ssl.verification_mode: full
ssl.certificate_authorities: ["/etc/filebeat/certs/elastic-ca.pem"]
ssl.certificate: "/etc/filebeat/certs/filebeat.crt"
ssl.key: "/etc/filebeat/certs/filebeat.key"
2. 认证配置
output.elasticsearch:
username: "filebeat_internal"
password: "${FILEBEAT_PASSWORD}"
3. API密钥认证
output.elasticsearch:
api_key: "KnR6yE424rJTToNJAkYn:VQ2BTjy_JTAd_NbzRqGFmw"
性能优化配置
1. 批量处理
output.elasticsearch:
bulk_max_size: 2048
flush_interval: 1s
worker: 2
2. 队列配置
queue.mem:
events: 8192
flush.min_events: 4096
flush.timeout: 5s
3. 资源限制
# 限制CPU使用
max_procs: 2
# 内存优化
logging.metrics.enabled: false
监控与调试
1. 监控配置
# 启用监控
monitoring.enabled: true
monitoring.elasticsearch:
hosts: ["localhost:9200"]
username: "beats_system"
password: "password"
2. 调试配置
# 调试日志
logging.level: debug
logging.selectors: ["*"]
# 输出到控制台
output.console:
pretty: true
3. HTTP监控
# 启用HTTP监控
http.enabled: true
http.host: localhost
http.port: 5066
环境变量配置
1. 使用环境变量
output.elasticsearch:
hosts: ["${ELASTICSEARCH_HOST:localhost:9200}"]
username: "${ELASTICSEARCH_USERNAME:}"
password: "${ELASTICSEARCH_PASSWORD:}"
2. 条件配置
filebeat.inputs:
- type: log
enabled: ${ENABLE_LOG_INPUT:true}
paths:
- ${LOG_PATH:/var/log/*.log}
常见配置问题
1. 文件权限问题
# 问题:无法读取日志文件
# 解决:检查文件权限
sudo chown root:filebeat /etc/filebeat/filebeat.yml
sudo chmod 644 /etc/filebeat/filebeat.yml
2. 网络连接问题
# 问题:无法连接到Elasticsearch
# 解决:检查网络配置
output.elasticsearch:
hosts: ["http://elasticsearch-host:9200"]
timeout: 90
3. 内存不足问题
# 问题:内存使用过高
# 解决:调整资源限制
max_procs: 1
queue.mem.events: 2048
4. 配置语法问题
# 验证配置文件
./filebeat test config
# 测试输出连接
./filebeat test output
最佳实践
1. 配置组织
# 按功能模块组织配置
filebeat.config.inputs:
enabled: true
path: ${path.config}/inputs.d/*.yml
reload.enabled: true
reload.period: 10s
2. 环境差异化
# 根据环境加载不同配置
filebeat.inputs:
- type: log
paths:
- /var/log/${ENVIRONMENT}/*.log
3. 安全配置
# 启用安全功能
output.elasticsearch:
ssl.enabled: true
username: "filebeat_internal"
password: "${FILEBEAT_PASSWORD}"
4. 性能优化
# 合理设置批量大小
output.elasticsearch:
bulk_max_size: 1024
flush_interval: 2s
总结
Beats的配置管理涉及输入配置、输出配置、处理器配置、安全配置等多个方面。通过合理配置各项参数,可以确保Beats的高效运行和数据收集的准确性。在实际应用中,需要根据具体的数据源类型和业务需求进行相应的配置调整和优化,同时注意安全性和性能配置,确保系统的可靠性和高效性。