一、安装Filebeat
在目标系统(如CentOS、Debian)上安装Filebeat,可通过包管理器(如yum)或官方下载包安装。例如,CentOS系统使用以下命令安装:
sudo yum install epel-release -y
sudo yum install filebeat -y
Debian系统可使用apt安装:
sudo apt update && sudo apt install filebeat -y
安装完成后,进入Filebeat配置目录(通常为/etc/filebeat/)准备后续配置。
二、配置Filebeat监控系统性能
Filebeat监控系统性能的核心是启用系统模块(预定义了CPU、内存、文件系统、进程等性能指标的采集规则)。具体步骤如下:
filebeat.yml配置文件,添加以下内容以启用system模块:filebeat.modules:
- module: system
enabled: true
# 设置采集周期(默认10s,可根据需求调整)
period: 10s
system模块包含多个子模块(如system.cpu、system.memory、system.filesystem、system.processes),分别对应不同性能指标的采集。output.elasticsearch:
hosts: ["localhost:9200"] # Elasticsearch地址
index: "filebeat-system-performance-%{+yyyy.MM.dd}" # 索引名称(按日期分割)
若需通过Logstash转发,可将output.elasticsearch替换为output.logstash并配置Logstash地址。三、启动与验证Filebeat
systemctl命令启动Filebeat并设置开机自启:sudo systemctl start filebeat
sudo systemctl enable filebeat
sudo systemctl status filebeat
若状态显示为active (running),则表示服务已启动。/var/log/filebeat/filebeat):tail -f /var/log/filebeat/filebeat
filebeat-*索引模式,在Discover页面搜索event.module: system,即可查看系统性能数据(如CPU使用率、内存占用等)。四、高级配置与优化
processors或paths参数调整。例如,仅监控/var/log/messages文件:filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/messages
filestream输入类型:Filebeat 7.0及以上版本推荐使用filestream(替代老旧的log输入),提升文件读取效率。示例如下:filebeat.inputs:
- type: filestream
enabled: true
paths:
- /var/log/*.log
bulk_max_size(默认50),提高向Elasticsearch发送数据的效率。示例如下:output.elasticsearch:
hosts: ["localhost:9200"]
bulk_max_size: 200 # 每批发送200个事件
queue.type: persisted启用持久化队列(避免数据丢失),并调整queue.max_bytes(默认1GB)控制队列大小。示例如下:queue:
type: persisted
max_bytes: 2gb
curl http://localhost:8080/stats
若需可视化,可将这些指标导入Prometheus+Grafana,创建仪表盘监控Filebeat运行状态。五、查看与分析监控数据
Discover页面查看原始性能数据,或使用Visualize工具创建折线图(如CPU使用率趋势)、仪表盘(如系统资源占用概览)。Watcher功能,设置基于性能指标的告警(如CPU使用率超过80%时发送邮件)。示例如下:PUT _watcher/watch/system_cpu_alert
{
"trigger": {
"schedule": {
"interval": "1m"
}
},
"input": {
"search": {
"request": {
"indices": ["filebeat-system-performance-*"],
"body": {
"query": {
"bool": {
"must": [
{"term": {"event.module": "system"}},
{"term": {"metricset.name": "cpu"}},
{"range": {"system.cpu.user.pct": {"gt": 80}}}
]
}
}
}
}
}
},
"actions": {
"email_alert": {
"email": {
"to": "admin@example.com",
"subject": "System CPU Usage Alert",
"body": "CPU usage exceeds 80% for 1 minute."
}
}
}
}