Beats
簡介
Beats集合了多種單一用途的數(shù)據(jù)采集器冲茸,主要有以下幾種類型
- filebeat:采集日志文件
- metricbeat:采集系統(tǒng)和軟件指標(biāo)
- packetbeat:采集網(wǎng)絡(luò)數(shù)據(jù)
如果數(shù)據(jù)不需要經(jīng)過處理屯阀,可以直接傳給Elasticsearch中
如果數(shù)據(jù)需要經(jīng)過處理缅帘,可以傳給logstash處理,然后再發(fā)送給Elasticsearch
Filebeat:輕量級的日志采集器
為什么要用Filebeat
當(dāng)服務(wù)器很多時难衰,ssh登錄查看會比較麻煩钦无。啟動 Filebeat 后,打開 Logs UI盖袭,直接在 Kibana 中觀看對您的文件進行 tail 操作的過程
Filebeat工作流程
- 指定數(shù)據(jù)源
- 匹配到日志后失暂,使用Harvester組件讀取日志
- 日志傳遞到spooler,然后再傳到elasticsearch或logstash中
下載安裝
選擇對應(yīng)系統(tǒng)的安裝包苍凛,下載地址:https://www.elastic.co/cn/downloads/beats/filebeat
下載后趣席,傳到服務(wù)器上
# 解壓
tar xf filebeat-7.13.0-linux-x86_64.tar.gz
# 移動并重命名
mv filebeat-7.13.0-linux-x86_64 /usr/local/filebeat
創(chuàng)建從控制臺讀取數(shù)據(jù)的配置文件
cd /usr/local/filebeat
vim mybeat.yml
# 添加以如下內(nèi)容
filebeat.inputs:
- type: stdin
enabled: true
output.console:
pretty: true
enable: true
啟動
./filebeat -e -c mybeat.yml
輸出hello,可以控制臺看到j(luò)son輸出
創(chuàng)建從日志中讀取數(shù)據(jù)的配置文件
vim mybeat1.yml
# 輸出以下內(nèi)容
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/logs/message
output.console:
pretty: true
enable: true
# 再次啟動
./filebeat -e -c mybeat1.yml
# 往messages里追加一點內(nèi)容
echo "hello" >> /var/log/messages
可以看到剛剛追加的內(nèi)容
自定義字段
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/logs/message
fields_under_root: true # 自定義字段作為根節(jié)點顯示
# 自定義字段
fields:
myfields: test
output.console:
pretty: true
enable: true
輸出到Elasticsearch中
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*.log
fields:
myfields: test
fields_under_root: true
output.elasticsearch:
hosts: ["192.168.0.14:9200"]
自定義索引名稱
output.elasticsearch:
hosts: ["192.168.0.14"]
index: "message-%{+yyyy-MM}"
setup.ilm.enabled: false
setup.template.enabled: false
setup.template.name: "index"
setup.template.pattern: "index-*"
安裝chrome插件Elasticsearch Head查看結(jié)果
還可以按日志類型定義索引名稱
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
# 自定義標(biāo)簽
tags: "access"
- type: log
enabled: true
paths:
- /var/log/nginx/error.log
tags: "error"
output.elasticsearch:
hosts: ["192.168.0.14:9200"]
indices:
- index: "nginx-access-%{+yyyy.MM}"
when.contains:
tags: "access"
- index: "nginx-error-%{+yyyy.MM}"
when.contains:
tags: "error"
setup.ilm.enabled: false
setup.template.name: "index"
setup.template.pattern: "index-*"
setup.template.enabled: false
filebeat的組件
harvester
- 讀取單個文件內(nèi)容
- 每個文件啟動一個harvester,并且harvester負(fù)責(zé)打開和關(guān)閉這些文件
- 每個文件啟動一個harvester
- 在harvester正在讀取文件內(nèi)容的時候醇蝴,文件被刪除或者重命名了,那么Filebeat就會續(xù)讀這個文件想罕,這就會造成一個問題悠栓,就是只要負(fù)責(zé)這個文件的harvester沒用關(guān)閉,那么磁盤空間就不會被釋放按价,默認(rèn)情況下惭适,F(xiàn)ilebeat保存問價你打開直到close_inactive到達
prospector
- 管理harvester,找到所有讀取的文件來源
- 如果輸入類型為日志楼镐,則為每個文件啟動一個harvester
- filebeat支持的prospector:stdin和log
module
前面的日志采集都是手動配置癞志,在Filebeat中,有大量的Module框产,可以簡化配置凄杯,并且做了簡單的處理
# 查看module列表
./filebeat modules list
# 啟用module
./filebeat modules enable nginx
# 禁用module
./filebeat modules disable nginx
修改module配置
vim modules.d/nginx.yml
- module: nginx
# Access logs
access:
enabled: true
# 添加日志文件
var.paths: ["/var/log/nginx/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: ["/var/log/nginx/error.log*"]
配置filebeat,添加module
filebeat.inputs:
output.elasticsearch:
hosts: ["192.168.0.20:9200"]
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
測試
可以看到剛寫入的數(shù)據(jù)己經(jīng)采集到了
更多玩法秉宿,可參考官方文檔:https://www.elastic.co/guide/en/beats/filebeat/current/index.html
Metricbeat
Metricbeat的組成
- module:收集的對象戒突,如mysql,redis,操作系統(tǒng)等
- metricset:收集指標(biāo)的集合:如CPU描睦,memory膊存,network等
使用Metricbeat的module收集Nginx數(shù)據(jù)
# 啟用nginx module
./metricbeat modules enable nginx
# 配置nginx的status
location /status {
stub_status on;
access_log off;
}
# reload
systemctl reload nginx
vim modules.d/nginx.yml
html
- module: nginx
#metricsets:
# - stubstatus
period: 10s
# Nginx hosts
hosts: ["http://127.0.0.1"]
# status的uri
server_status_path: "status"
./metricbeat -e
測試