Filebeat 收集 CentOS 系统日志 是一个非常常见的场景,通常包括 系统日志(/var/log/messages、/var/log/secure) 和 系统服务日志。下面我按 “从零到可用” 的方式给你一个完整、实战型的说明。
常见日志文件:
| 日志类型 | 路径 |
|---|---|
| 系统通用日志 | /var/log/messages |
| 认证与安全日志 | /var/log/secure |
| 计划任务日志 | /var/log/cron |
| 启动日志 | /var/log/dmesg |
| 自定义服务日志 | /var/log/xxx/*.log |
✅ CentOS 6 使用 rsyslog
✅ CentOS 7/8 使用 rsyslog + systemd-journal
# 导入 Elastic GPG key
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
# 安装 Filebeat(示例为 8.x)
cat > /etc/yum.repos.d/filebeat.repo <<EOF
[filebeat]
name=Elasticsearch repository for 8.x packages
baseurl=https://artifacts.elastic.co/packages/8.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF
yum install -y filebeat
vim /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/messages
- /var/log/secure
- /var/log/cron
fields:
log_type: system
fields_under_root: false
processors:
- add_host_metadata:
when.not.contains.tags: forwarded
output.elasticsearch:
hosts: ["http://elasticsearch:9200"]
index: "centos-system-logs"
setup.ilm.enabled: false
setup.template.name: centos-system
setup.template.pattern: centos-system-*
✅ 适合 Elasticsearch + Kibana
output.logstash:
hosts: ["logstash:5044"]
Logstash 示例:
input {
beats {
port => 5044
}
}
filter {
if [fields][log_type] == "system" {
grok {
match => { "message" => "%{SYSLOGLINE}" }
}
}
}
output {
elasticsearch {
hosts => ["localhost:9200"]
index => "centos-system-logs-%{+YYYY.MM.dd}"
}
}
filebeat modules enable system
编辑模块配置:
vim /etc/filebeat/modules.d/system.yml
示例:
- module: system
syslog:
enabled: true
var.paths: ["/var/log/messages"]
auth:
enabled: true
var.paths: ["/var/log/secure"]
systemctl enable filebeat
systemctl start filebeat
systemctl status filebeat
journalctl -u filebeat -f
curl http://localhost:9200/_cat/indices
centos-system-logs*
✅ 检查:
ls -l /var/log/messages
✅ 确认权限:
usermod -aG root filebeat
或:
chmod 644 /var/log/messages
setenforce 0
# 或
getenforce
Filebeat 通过在 CentOS 上配置 log input 或 system module,读取 /var/log 下的系统日志,再输出到 Elasticsearch 或 Logstash,实现集中化日志收集。
如果你愿意,我可以:
你现在用的是 裸机、Docker 还是 Kubernetes?