注意: Logstash須要Java 8或更高版本.可使用
http://www.oracle.com/technetwork/java/javase/downloads/index.html [oracle官方版本] 或者使用開源版本OpenJDK: http://openjdk.java.net/.html
使用下面的命令,檢查你的JDK版本java
java -version
在安裝了java的系統上,這個命令將有以下相似的輸出:linux
java version "1.8.0_65" Java(TM) SE Runtime Environment (build 1.8.0_65-b17) Java HotSpot(TM) 64-Bit Server VM (build 25.65-b01, mixed mode)
從https://www.elastic.co/downloads/logstash 下載適合你本身的主機環境的Logstash安裝文件.
解壓文件,不要將安裝logstash到一個含有冒號(:)的目錄下面。git
在支持包管理器的linux系統上,可使用包管理器來安裝。web
咱們也有適用於APT和YUM的發行版倉庫。注意咱們只提供二進制包,但沒有源代碼軟件包,包
做爲Logstash構建的一部分被建立。shell
咱們將Logstash package repositories不一樣版本分放到不一樣的urls,避免在主要或次要版本升級中發生意外。對於全部的2.3.x版本發佈使用2.3版本號,在2.2.x使用2.2等。數據庫
咱們使用PGP密鑰
https://pgp.mit.edu/pks/lookup?op=vindex&search=0xD27D666CD88E42B4[D88E42B4 ]
Elastic的簽名密鑰,指紋centos
4609 5ACC 8548 582C 1A26 99A9 D27D 666C D88E 42B4
簽署咱們的全部包。在https://pgp.mit.edu 提供 。安全
下載並安裝公有簽名密鑰網絡
wget -qO - https://packages.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
把倉庫的定義加入 /etc/apt/sources.list
文件:
echo "deb https://packages.elastic.co/logstash/2.3/debian stable main" | sudo tee -a /etc/apt/sources.list
注意
Use the
echo
method described above to add the Logstash repository. Do not
useadd-apt-repository
as it will add adeb-src
entry as well, but we do not
provide a source package. If you have added thedeb-src
entry, you will see an
error like the following:
Unable to find expected entry ‘main/source/Sources’ in Release file (Wrong sources.list entry or malformed file)
Just delete the
deb-src
entry from the/etc/apt/sources.list
file and the
installation should work as expected.
運行 sudo apt-get update
使repository可用. 而後運行安裝命令:
sudo apt-get update && sudo apt-get install logstash
下載並安裝公有簽名密鑰:
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
以下內容寫到一個文件以.repo
爲後綴名,放到/etc/yum.repos.d/
下,如:logstash.repo
[logstash2.3] name=Logstash repository for2.3.x packages baseurl=https://packages.elastic.co/logstash2.3/centos gpgcheck=1 gpgkey=https://packages.elastic.co/GPG-KEY-elasticsearch enabled=1
添加完倉庫文件,能夠安裝:
yum install logstash
運行以下的基礎Logstash pipeline,測試Logstash的安裝
cd logstash-2.3.0 bin/logstash -e 'input { stdin { } } output { stdout {} }'
能夠從命令行使用-e 參數來直接指定配置。在命令行中指定的配置可讓您快速測試,無需在迭代過程當中修改文件。這個管道輸入是標準輸入,並移動該輸入到輸出,以結構化格式標準輸出。
一旦"Logstash startup completed"顯示,經過命令行提示,輸入hello world,查看響應結果:
hello world 2013-11-21T01:22:14.405+0000 0.0.0.0 hello world
Logstash將時間戳和ip地址信息加入到消息中。使用Ctrl+D的shell命令能夠從Logstash環境退出。
LogStash管道一般有一個或多個輸入,過濾,輸出插件。本節中的場景經過Logstash配置文件來指定這些插件,並討論了每一個插件在作什麼。
Logstash配置文件定義了Logstash 管道,當你使用-f <path/to/file>啓動一個Logstash實例,其實使用了一個配置文件定義了管道實例。
一個Logstash管道有兩個必備元素,輸入和輸出,一個可選元素,過濾器。input插件從一個源攝取數據,filter插件按照你指定的方式修改數據,output插件寫出數據到一個目標數據庫。
下面是一個管道配置的模板:
# #是註釋,使用註釋描述配置 input { } # 該部分被註釋,表示filter是可選的 # filter { # # } output { }
這個模板是不具有功能的,由於輸入和輸出部分沒有定義的任何有效的選項。
將模板粘貼到你的Logstash根目錄,命名爲first-pipeline.conf。
這個例子建立一個Logstash管道,使用Apache web logs 做爲輸入,解析這些日誌到特定的命名字段,而且輸出這些解析數據到ElashticSearch集羣
你能夠下載樣例數據,這裏
啓動Logstash管道,使用file input插件配置Logstash實例。
修改first-pipeline.conf 添加以下內容:
input { file { path => "/path/to/logstash-tutorial.log" start_position => beginning ignore_older => 0 } }
用實際的logstash-tutorial.log日誌文件目錄替換 /path/to/。
gork 過濾器插件是Logstash的幾個默承認用插件之一。關於如何管理Logstash 插件的更多細節請參考:reference documentation
grok 過濾器插件在輸入日誌中查找指定的模式,配置文件中須要指定哪些模式的識別是你的案例所感興趣的。一個表明性的web server 日誌文件行信息,以下:
83.149.9.216 - - [04/Jan/2015:05:13:42 +0000] "GET /presentations/logstash-monitorama-2013/images/kibana-search.png HTTP/1.1" 200 203023 "http://semicomplete.com/presentations/logstash-monitorama-2013/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36"
行首的IP地址信息很容易被識別,括號中的時間戳也是如此。在此教程中,使用%{COMBINEDAPACHELOG} grok
模式,它的行信息結構有以下的一些字段。
Information | Field Name |
---|---|
IP Address | clientip |
User ID | ident |
User Authentication | auth |
timestamp | timestamp |
HTTP Verb | verb |
Request body | request |
HTTP Version | httpversion |
HTTP Status Code | response |
Bytes served | bytes |
Referrer URL | referrer |
User agent | agent |
編寫 first-pipeline.conf添加以下的內容:
filter { grok { match => { "message" => "%{COMBINEDAPACHELOG}"} } }
過濾器處理以後,一個行信息是以下的JSON的結構:
{ "clientip" : "83.149.9.216", "ident" : , "auth" : , "timestamp" : "04/Jan/2015:05:13:42 +0000", "verb" : "GET", "request" : "/presentations/logstash-monitorama-2013/images/kibana-search.png", "httpversion" : "HTTP/1.1", "response" : "200", "bytes" : "203023", "referrer" : "http://semicomplete.com/presentations/logstash-monitorama-2013/", "agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36" }
Now that the web logs are broken down into specific fields, the Logstash pipeline can index the data into an Elasticsearch cluster. Edit the first-pipeline.conf file to add the following text after the input section:
如今,網絡日誌被分解成具體的字段, 使用Logstash管道能夠將數據索引到Elasticsearch集羣。編輯first-pipeline.conf 文件,在input 段以後添加如下文字:
output { elasticsearch { } }
用這樣的配置, Logstash使用http協議鏈接到Elasticsearch 。上面的例子假設Logstash和Elasticsearch運行在同一實例中。您可使用主機配置像hosts => "es-machine:9092"
指定遠程Elasticsearch實例。
除了解析日誌數據,filter插件能夠從現有數據基礎上補充信息。例如: geoip插件查找IP地址,從而導出地址地理位置信息,並將該位置信息發送給日誌。
使用geoip filter插件配置您的Logstash,如:經過添加如下行到first-pipeline.conf文件的filter部分:
geoip { source => "clientip" }
插件geoip的配置須要的數據已經被定義爲獨立的字段中的數據。確保在配置文件中,geoip 配置段在grok配置段以後。
須要指定包含IP地址的字段的名稱。在本例中,字段名稱是clientip(grok插件字段) 。
此時此刻,你的first-pipeline.conf文件有input,filter和output段,正確的配置看起來是這樣的:
input { file { path => "/Users/palecur/logstash-1.5.2/logstash-tutorial-dataset" start_position => beginning } } filter { grok { match => { "message" => "%{COMBINEDAPACHELOG}"} } geoip { source => "clientip" } } output { elasticsearch {} stdout {} }
測試配置語法的正確性,使用下面的命令:
bin/logstash -f first-pipeline.conf --configtest
使用–configtest 選項解析你的配置文件並報告錯誤.當配置文件經過檢查以後,用以下命令啓動Logstash:
bin/logstash -f first-pipeline.conf
基於gork filter插件的字段索引,在Elasticsearch運行一個測試查詢,
curl -XGET 'localhost:9200/logstash-$DATE/_search?q=response=200'
使用當前日期替換$DATE(YYYY.MM.DD格式).由於咱們的例子只有一條200 HTTP response,因此查詢命中一條結果:
{"took":2, "timed_out":false, "_shards":{"total":5, "successful":5, "failed":0}, "hits":{"total":1, "max_score":1.5351382, "hits":[{"_index":"logstash-2015.07.30", "_type":"logs", "_id":"AU7gqOky1um3U6ZomFaF", "_score":1.5351382, "_source":{"message":"83.149.9.216 - - [04/Jan/2015:05:13:45 +0000] \"GET /presentations/logstash-monitorama-2013/images/frontend-response-codes.png HTTP/1.1\" 200 52878 \"http://semicomplete.com/presentations/logstash-monitorama-2013/\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36\"", "@version":"1", "@timestamp":"2015-07-30T20:30:41.265Z", "host":"localhost", "path":"/path/to/logstash-tutorial-dataset", "clientip":"83.149.9.216", "ident":"-", "auth":"-", "timestamp":"04/Jan/2015:05:13:45 +0000", "verb":"GET", "request":"/presentations/logstash-monitorama-2013/images/frontend-response-codes.png", "httpversion":"1.1", "response":"200", "bytes":"52878", "referrer":"\"http://semicomplete.com/presentations/logstash-monitorama-2013/\"", "agent":"\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36\"" } }] } }
嘗試一個基於ip地址的地理信息查詢:
curl -XGET 'localhost:9200/logstash-$DATE/_search?q=geoip.city_name=Buffalo'
使用當前日期替換$DATE(YYYY.MM.DD格式).
只有一條log記錄是來自於城市Buffalo,因此查詢的結果是:
{"took":3, "timed_out":false, "_shards":{ "total":5, "successful":5, "failed":0}, "hits":{"total":1, "max_score":1.03399, "hits":[{"_index":"logstash-2015.07.31", "_type":"logs", "_id":"AU7mK3CVSiMeBsJ0b_EP", "_score":1.03399, "_source":{ "message":"108.174.55.234 - - [04/Jan/2015:05:27:45 +0000] \"GET /?flav=rss20 HTTP/1.1\" 200 29941 \"-\" \"-\"", "@version":"1", "@timestamp":"2015-07-31T22:11:22.347Z", "host":"localhost", "path":"/path/to/logstash-tutorial-dataset", "clientip":"108.174.55.234", "ident":"-", "auth":"-", "timestamp":"04/Jan/2015:05:27:45 +0000", "verb":"GET", "request":"/?flav=rss20", "httpversion":"1.1", "response":"200", "bytes":"29941", "referrer":"\"-\"", "agent":"\"-\"", "geoip":{ "ip":"108.174.55.234", "country_code2":"US", "country_code3":"USA", "country_name":"United States", "continent_code":"NA", "region_name":"NY", "city_name":"Buffalo", "postal_code":"14221", "latitude":42.9864, "longitude":-78.7279, "dma_code":514, "area_code":716, "timezone":"America/New_York", "real_region_name":"New York", "location":[-78.7279,42.9864] } } }] } }
你須要管理的信息一般來自於不一樣的源,你的案例也可能將數據送到多個目標。Logstash 管道須要使用多個輸出或者輸出插件來知足這些需求。
這個例子建立一個Logstash 管道來從Twitter feed 和Filebeat client獲取輸入信息,而後將這些信息送到Elasticsearch集羣,同時寫入一個文件.
要添加一個Twitter feed,你須要一系列的條件:
訪問 https://dev.twitter.com/apps 註冊一個Twitter 帳號 而且生成你的 consumer key 和 secret, 還有你的 OAuth token 和 secret.
把以下的內容追加到first-pipeline.conf文件的input段:
twitter { consumer_key => consumer_secret => keywords => oauth_token => oauth_token_secret => }
filebeat 客戶端是一個輕量級的資源友好的文件服務端日誌收集工具,而且把這些日誌送到Logstash實例來處理。filebeat 客戶端使用安全Beats協議與Logstash實例通訊。lumberjack協議被設計成可靠的低延遲的.Filebeat 使用託管源數據的計算機的計算資源,而且Beats input插件減小了 Logstash實例的資源需求.
注意
一個典型的用法是,Filebeat運行在,與Logstash實例分開的一個單獨的計算機上.爲了教學方便,咱們將Logstash 和 Filebeat運行在同一個計算機上.
默認的Logstash配置包含Beats input插件,它被設計成資源友好的.安裝Filebeat在數據源所在的機器,從Filebeat產品頁下載合適的包.
建立一個和這個例子相似的配置文件:
filebeat: prospectors: - paths: - "/path/to/sample-log" fields: type: syslog output: logstash: hosts: ["localhost:5043"] tls: certificate: /path/to/ssl-certificate.crt certificate_key: /path/to/ssl-certificate.key certificate_authorities: /path/to/ssl-certificate.crt timeout: 15
以filebeat.yml爲名,保存配置文件
配置你的Logstash實例,加入Filebeat input插件.將以下內容添加到first-pipeline.conf文件的input段中.
beats { port => "5043" ssl => true ssl_certificate => "/path/to/ssl-cert" ssl_key => "/path/to/ssl-key" }
能夠經過 file output插件配置讓Logstash管道將數據直接寫入一個文件.
將以下內容添加到first-pipeline.conf文件的output段,完成Logstash實例配置:
file { path => /path/to/target/file }
輸出數據到多個 Elasticsearch節點,能夠減輕某個給定節點的資源需求.當某一個節點掛掉的時候,提供了數據記錄輸入Elasticsearch的更多選擇.
修改你的first-pipeline.conf文件的output段,這樣就可讓你的Logstash實例將數據寫入到多個Elasticsearch節點.
output { elasticsearch { hosts => ["IP Address 1:port1", "IP Address 2:port2", "IP Address 3"] } }
host參數要使用三個非master的Elasticsearch集羣節點ip.當host參數列出多個ip地址時,Logstash負載均衡請求到列表中的地址.另外Elasticsearch的默認端口是9200,在上面的配置中能夠省略.
此時,你的 first-pipeline.conf 文件應該是下面的這樣:
input { twitter { consumer_key => consumer_secret => keywords => oauth_token => oauth_token_secret => } beats { port => "5043" ssl => true ssl_certificate => "/path/to/ssl-cert" ssl_key => "/path/to/ssl-key" } } output { elasticsearch { hosts => ["IP Address 1:port1", "IP Address 2:port2", "IP Address 3"] } file { path => /path/to/target/file } }
Logstash從你配置的Twitter feed消費數據, 從 Filebeat獲取數據, 而且將這些數據索引到Elasticsearch的三個節點,而且同時將數據寫入一個文件.
在數據源節點上,使用以下命令啓動Filebeat:
sudo ./filebeat -e -c filebeat.yml -d "publish"
Filebeat嘗試鏈接5403端口,知道你的Logstash的Beats 插件生效,這個端口都沒有響應,因此如今能夠將任何的鏈接異常看做是正常的.
校驗你的配置,使用以下命令:
bin/logstash -f first-pipeline.conf --configtest
使用–configtest 選項解析你的配置文件並報告錯誤.當配置文件經過檢查以後,用以下命令啓動Logstash:
bin/logstash -f first-pipeline.conf
使用grep命令在你的目標文件中查找,驗證Mozilla信息的存在.
grep Mozilla /path/to/target/file
在你的Elasticsearch集羣中運行Elasticsearch查詢,驗證一樣的信息也存在!
curl -XGET 'localhost:9200/logstash-2015.07.30/_search?q=agent=Mozilla'