1. 程式人生 > 其它 >ELK-filebeat7.12.0收集系統日誌

ELK-filebeat7.12.0收集系統日誌

Filebeat

filebeat與logstash作用是一樣的
E/L/K都是java程式寫的
filebeat是golang語言寫的,比較輕量

Filebeat模組很好的入門,它是輕量級單用途的日誌收集工具,用於在沒有安裝java的伺服器上專門收集日誌,可以將日誌轉發到logstash、elasticsearch或redis等場景中進行下一步處理。

圖解:

需要依賴包:

filebeat-7.12.0-x86_64.rpm

安裝

[root@localhost ~]# rpm -ivh filebeat-7.12.0-x86_64.rpm

[root@localhost ~]# cp /etc/filebeat/filebeat.yml /etc/filebeat/filebeat.yml.bak

修改配置引數,對接到logstash:5044

注意:service是用來區分標識多日誌到Logstash

[root@localhost ~]# grep -v "#" /etc/filebeat/filebeat.yml |grep -v "^$"
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/messages
fields:
service: 18_160_system_log
output.logstash:
hosts: ["10.197.10.198:5044"]
enabled: true

[root@localhost ~]# systemctl restart filebeat

[root@localhost ~]# systemctl enable filebeat

或者

[root@localhost ~]# /usr/share/filebeat/bin/filebeat -c /etc/filebeat/filebeat.yml -d "publish" &

# 注意使用root使用者登入, 準確的說是注意使用者對檔案所擁有的許可權
# -e 讓日誌列印到控制檯, -c 重新指定啟動的配置檔案, -d 指定除錯選擇者器
./filebeat -e -c filebeat.yml -d "publish"

修改logstash下的配置檔案:

[root@localhost ~]# more /etc/logstash/conf.d/beat.conf
input {
beats {
port => 5044
}
file {
path => "/var/log/messages" #日誌路徑
type => "10_198_system_log" #事件的唯一型別
start_position => "beginning" #第一次收集日誌的位置
stat_interval => "3" #日誌收集的間隔時間
}
}


#filter {
# #只對nginx的json日誌做json解析,系統message為其他格式,無需處理
# if [filetype] == "log_nginxjson"{
# json {
# source => "message"
# remove_field => ["beat","offset","tags","prospector"] #移除欄位,不需要採集
# }
# date {
# match => ["timestamp", "dd/MMM/yyyy:HH:mm:ss Z"] #匹配timestamp欄位
# target => "@timestamp" #將匹配到的資料寫到@timestamp欄位中
# }
# }
#}

#選擇了兩個客戶端的系統日誌做測試,其中一個是直接通過本地的logstash進行收集,另一個是通過filebeat->logstash -> ES中。

#這個[fields][service] 就是用來區分不同的業務日誌

output {
if [fields][service] == "18_160_system_log"{
elasticsearch {
hosts => ["10.197.10.198:9200"]
index => "18-160-msg-%{+YYYY.MM.dd}"
}
}
if [type] == "10_198_system_log"{
elasticsearch {
hosts => ["10.197.10.198:9200"]
index => "10-198-msg-%{+YYYY.MM.dd}"

}
}
}

在通過命令啟動

[root@localhost ~]# /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/beat.conf --config.reload.automatic &

# -f 從指定路徑獲取logstash啟動的yml配置檔案
# --config.reload.automatic 監聽配置檔案 如果有改動自動載入

這時候就可以在kibana-Index Management看到相關日誌資訊,並通過index Patterns展示