LogStash安裝
1. LogStash插件介紹
Logstash是一個具有實時管道的開源資料收集引擎。可以動态地統一不同來源的資料,并将資料歸到不同目的地。也是一個管理事件和日志工具。你可以用它來收集日志,分析它們,并将它們儲存起來以供以後使用。
Logstash 通常都是和 Kibana 以及 Elasticsearch 一起使用。
2. logStash安裝
#注意版本和 elasticsearch,kibana 必須保持一緻,es,kibana都是6.2.4版本
wget https://artifacts.elastic.co/downloads/logstash/logstash-6.2.4.tar.gz
#解壓
tar -zxvf logstash-6.2.4.tar.gz
快速啟動:
#啟動 基本的 intput output
#stdin stdout 标準輸入、标準輸出插件
./logstash -e 'input{ stdin{} } output{ stdout{} }'
# codec插件,指定輸出格式為json
./logstash -e 'input{ stdin{} } output{ stdout{ codec => json } }'
# 日志内容寫入elasticsearch
./logstash -e 'input{ stdin{} } output{ elasticsearch{hosts => ["192.168.66.66:9200"]} }'
#日志内容寫入elasticsearch,同時标準輸出
#注意elasticsearch插件的文法格式:hosts 對應數組
./logstash -e 'input{ stdin{} } output{ elasticsearch{hosts => ["192.168.66.66:9200"]} stdout{} }'
3. logStash 配置檔案方式啟動
https://www.elastic.co/guide/en/logstash/current/configuration-file-structure.html

要配置Logstash,您需要建立一個配置檔案來指定您想要使用的插件以及每個插件的設定。您可以引用配置中的事件字段,并在事件滿足某些條件時使用條件來處理事件。當您運作logstash時,您使用-f來指定配置檔案。
讓我們逐漸建立一個簡單的配置檔案并使用它運作Logstash。建立一個名為logstash-simple.conf的檔案,并将其儲存在與Logstash相同的目錄中。
input { stdin { } }
output {
elasticsearch { hosts => ["localhost:9200"] }
stdout { codec => rubydebug }
}
然後,運作logstash并使用-f标志指定配置檔案。
bin/logstash -f logstash-simple.conf
果不其然!Logstash讀取指定的配置檔案并輸出到Elasticsearch和stdout。請注意,如果您在标準輸出中看到一條消息,它是“Elasticsearch Unreachable”,您需要確定Elasticsearch已安裝并在端口9200上可通路。
4. logStash 配置檔案文法、結構
https://www.elastic.co/guide/en/logstash/current/configuration-file-structure.html
對于要添加到事件處理管道中的每個插件類型,Logstash配置檔案都有一個單獨的部分。例如:
# This is a comment. You should use comments to describe
# parts of your configuration.
input {
...
}
filter {
...
}
output {
...
}
每個部分包含一個或多個插件的配置選項。如果指定多個篩選器,則按照它們在配置檔案中出現的順序應用它們。
插件的配置由插件名稱和插件的設定塊組成。例如,這個輸入部配置設定置了兩個檔案輸入:
input {
file {
path => "/var/log/messages"
type => "syslog"
}
file {
path => "/var/log/apache/access.log"
type => "apache"
}
}
在本例中,為每個檔案輸入配置了兩個設定:path和type。
您可以根據插件類型配置不同的設定。有關每個插件的資訊,請參閱輸入插件、輸出插件、過濾插件和編解碼器插件。
5. logStash插件
5.1. input插件
https://www.elastic.co/guide/en/logstash/current/input-plugins.html
輸入插件允許Logstash讀取特定的事件源。
輸入比較常見的幾個插件:stdin、file、http、tcp
5.2. output插件
https://www.elastic.co/guide/en/logstash/current/output-plugins.html
輸出插件将事件資料發送到特定的目的地。輸出是事件管道中的最後一個階段。
把日志内容輸出到elasticsearch插件:
5.3. codec插件
https://www.elastic.co/guide/en/logstash/current/codec-plugins.html
編解碼器插件改變事件的資料表示。編解碼器本質上是流過濾器,可以作為輸入或輸出的一部分進行操作。
Codec(Code Decode)Plugin作用于input 和 output plugin,負責将資料在原始與Logstash之間轉換,常見的codec有:
- plain 讀取原始内容
- dots 将内容簡化為點進行輸出
- rubydebug 将内容按照ruby格式輸出,友善調試
- line 處理帶有換行符的内容
- json 處理json格式的内容
- multiline 處理多行資料的内容
還有有很多其他插件
官方文檔:https://www.elastic.co/guide/en/logstash/current/index.html
![]()
LogStash 安裝LogStash安裝
6. Demo1:file日志收集
#建立新的配置檔案
mv logstash.conf file.conf
#詳細配置如下
input {
file{
path => "/var/log/messages" #收集messages檔案日志
type => "system"
start_position => "beginning" #記錄上次收集的位置
}
}
output {
elasticsearch {
hosts => ["192.168.66.66:9200"] #寫入elasticsearch的位址
index => "system-%{+YYYY.MM.dd}" #定義索引的名稱
}
stdout { codec => rubydebug }
}
#啟動logstash,配置檔案名字叫什麼無所謂
bin/logstash -f config/file.conf
收集效果如下所示:
7. Demo2:Java日志收集
#在原來file檔案的基礎上進行編輯
input {
file{
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
#加一個file檔案收集日志插件,收集elasticsearch日志、es就是java語言開發的。
file{
path => "/home/es/elasticsearch-6.2.4/logs/elasticsearch.log"
type => "es-info"
start_position => "beginning"
}
}
output {
if [type] == "system"{
elasticsearch {
hosts => ["192.168.66.66:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
#判斷,導入到不同的索引庫,否則會放入同一個索引庫中
if [type] == "es-info"{
elasticsearch {
hosts => ["192.168.66.66:9200"]
index => "es-info-%{+YYYY.MM.dd}"
}
}
stdout { codec => rubydebug }
}
導入效果:
問題:目前導入日志都是按照行導入的、但是有些日志多行是一句話,如果分開的話,就不太容檢視日志的完整的意思了。
解決方案: 可以使用codec來進行解決
#在原來file檔案的基礎上進行編輯
input {
file{
path => "/var/log/messages"
type => "system"
start_position => "beginning"
}
#加一個file檔案收集日志插件,收集elasticsearch日志、es就是java語言開發的。
file{
path => "/home/es/elasticsearch-6.2.4/logs/elasticsearch.log"
type => "es-info"
start_position => "beginning"
#使用正規表達式,合并多行日志
codec => multiline {
pattern => "^\[" #發現中括号,就合并日志
negate => true
what => "previous"
}
}
}
output {
if [type] == "system"{
elasticsearch {
hosts => ["192.168.66.66:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
#判斷,導入到不同的索引庫,否則會放入同一個索引庫中
if [type] == "es-info"{
elasticsearch {
hosts => ["192.168.66.66:9200"]
index => "es-info-%{+YYYY.MM.dd}"
}
}
stdout { codec => rubydebug }
}
使用codec插件處理多行資訊。把多行日志合并為一行,導入到es