logstash簡介
- 是一個數據採集、加工處理以及傳輸的工具
- 特點
- 所有類型的數據集中處理
- 不同模式和格式數據的正常化
- 自定義日誌格式的迅速擴展
- 爲自定義數據源輕鬆添加插件
安裝logstash
- 主機配置要求最低2核CPU2G內存
[root@logstash ~] rsync -av 192.168.1.252:/etc/hosts /etc/ #同步主機名解析
[root@logstash ~] yum -y install java-1.8.0-openjdk logstash #需要jdk運行環境
- 沒有默認的配置文件,需要手寫,安裝路徑在
/opt/logstash/
[root@logstash ~] vim /etc/logstash/logstash.conf
input{
stdin{}
}
filter{}
output{
stdout{}
}
#寫入驗證配置,作用類似echo,輸入什麼輸出什麼
[root@logstash ~] /opt/logstash/bin/logstash -f /etc/logstash/logstash.conf #啓動程序,-f 指定配置文件路徑及文件名
Settings: Default pipeline workers: 2
Pipeline main started #表示啓動成功
test #輸入test
2020-02-24T04:02:22.790Z logstash test #輸出test,則服務正常
logstash使用
1)類型
布爾值類型:ssl_enable => true
字節類型:bytes => "1MiB"
字符串類型:name => "xkops"
數值類型:port => 22
數組:match => ["datetime","UNIX"]
哈希:options => {k => "v",k2 => "v2"}
編碼解碼:codec => "json"
路徑:file_path => "/tmp/filename"
註釋:#
2)條件判斷
等於 ==
不等於 !=
小於 <
大於 >
小於等於 <=
大於等於 >=
匹配正則 =~
不匹配正則 !~
包含 in
不包含 not in
與 and
或 or
非與 nand
非或 xor
複合表達式 ()
取反覆合 !()
3)插件
1. codec類插件
- 常用的插件:plain、json、json _lines、rubydebug、multiline等
] vim /etc/logstash/logstash.conf
input{
stdin{ codec => "json" }
}
filter{}
output{
stdout{ codec => "rubydebug" }
}
] /opt/logstash/bin/logstash -f /etc/logstash/logstash.conf
2. input file插件
] vim /etc/logstash/logstash.conf
input{
file{
start_position => "beginning"
sincedb_path => "/var/lib/logstash/sincedb-access"
path => [ "/tmp/a.log", "/tmp/b.log" ]
type => 'filelog'
}
sincedb_path
記錄讀取位置文件的路徑和文件名start_position
配置第一次讀取文件從什麼地方開始,值爲[“beginning”,“end”],默認爲"end"。配置爲beginning時,第一次從文件開頭進行讀取,並將已讀取的位置記錄到sincedb_path指定的文件中,下一次讀取時會從這個文件查看記錄的位置並讀取未讀取的部分,保證記錄完整性type
相當於給日誌打標籤path
指定要讀取的日誌文件
3.filter grok插件
- 解析各種非結構化的日誌數據插件
- grok使用正則表達式把飛結構化的數據結構化
- 在分組匹配,正則表達式需要根據具體數據結構編寫
- 雖然編寫困難,但適用性極廣
- 幾乎可以應用於各類數據
filter{
grok{
match => { "message" => "%{IP:client_ip}"}
}
}
- grok正則分組匹配
-
匹配ip時間戳和請求方法
-
使用正則宏,正則宏查詢文件
/opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-patterns-core-2.0.5/patterns/grok-patterns
-
filter {
grok {
match => { "message" => "%{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes} %{NUMBER:duration}" }
}
}
或者:
filter {
grok {
match => { "message" => "%{COMBINEDAPACHELOG}"} #正則宏名
}
}
4.output ES插件
- 主要作用爲將數據寫入es集羣
output{
if [type] == "apache_log"{ #if的作用是將type爲這個VALUE的日誌數據寫入,其他的不寫入,達到多種服務的日誌區分的目的
elasticsearch{
hosts => ["192.168.1.51:9200", "192.168.1.52:9200", "192.168.1.53:9200"] #es集羣主機和端口
index => "logstash-apache" #索引名
flush_size => 2000 #數據大小達到2000字節時才寫入一次
idle_flush_time => 10 #數據寫入空閒10s時,寫入一次數據
}}
}
5.input filebeats插件
- 這個插件主要用來接收beats類軟件發送過來的數據,由於logstash以來JAVA環境,而且佔用資源非常大,所以使用更輕量的filebeats替代,將logstash獨立出來處理日誌
beats{
port => 5044
}
例:
input {
beats {
port => 5044
}
}
filter{
if [type] == "http_log"{
grok {
match => { "message" => "%{COMBINEDAPACHELOG}" }
}}
}
output{
#stdout{ codec => "rubydebug" }
if [type] == "http_log"{
elasticsearch {
index => "weblog"
hosts => ["192.168.1.51:9200", "192.168.1.52:9200", "192.168.1.53:9200"]
flush_size => 2000
idle_flush_time => 10
}}
}
web服務器安裝filebeat
] yum -y install filebeat
] vim /etc/filebeat/filebeat.yml
15 - /var/log/access.log #數組格式,可以寫多個文件
72 document_type: http_log #設置發送的日誌的type,作用是再logstash服務器匹配對應的規則
183 #elasticsearch: #註釋掉elasticsearch相關配置
188 #hosts: ["localhost:9200"] #註釋掉elasticsearch相關配置
278 logstash: #打開註釋
280 hosts: ["192.168.1.57:5044"] #打開註釋,指定logstash服務器ip和監聽的端口
] systemctl start filebeat
] systemctl enable filebeat
如果kibana使用時報錯:Field data loading is forbidden on [xxxx]
,則不要將索引名稱設置爲logstash-
開頭即可,參考