您好,登錄后才能下訂單哦!
在"hello World" 示例中,我們已經(jīng)見到并介紹了Logstash 的運(yùn)行流程和配置的基礎(chǔ)語法。 請記住一個(gè)原則:Logstash 配置一定要有一個(gè) input 和一個(gè) output。在演示過程中,如果沒有寫明 input,默認(rèn)就會使用 "hello world" 里我們已經(jīng)演示過的 input/stdin ,同理,沒有寫明的 output 就是 output/stdout
如果有什么問題的話,請查看該文檔:http://udn.yyuap.com/doc/logstash-best-practice-cn/input/index.html。以下是input插件的具體解釋:
(1),標(biāo)準(zhǔn)輸入。type和tags是logstash事件中特殊的字段。 type 用來標(biāo)記事件類型 —— 我們肯定是提前能知道這個(gè)事件屬于什么類型的。而 tags 則是在數(shù)據(jù)處理過程中,由具體的插件來添加或者刪除的。
[root@localhost test]# vim stdin.conf input { stdin { add_field => {"key" => "value"} codec => "plain" tags => ["add"] type => "std-lqb" } } output { stdout { codec => rubydebug } } [root@localhost logstash]# /usr/local/logstash/bin/logstash -f test/stdin.conf Settings: Default pipeline workers: 1 Logstash startup completed hello world { "message" => "hello world", "@version" => "1", "@timestamp" => "2017-05-24T08:11:45.852Z", "type" => "std-lqb", "key" => "value", "tags" => [ [0] "add" ], "host" => "localhost.localdomain" } abclqb { "message" => "abclqb", "@version" => "1", "@timestamp" => "2017-05-24T08:13:21.192Z", "type" => "std-lqb", "key" => "value", "tags" => [ [0] "add" ], "host" => "localhost.localdomain" } #####對stdin進(jìn)行修改,添加tags列 [root@localhost test]# vim stdin.conf input { stdin { add_field => {"key" => "value2222222222222222222222222222222222222222222 2"} codec => "plain" tags => ["add","xxyy","abc"] type => "std-lqb" } } output { stdout { codec => rubydebug } } [root@localhost logstash]# /usr/local/logstash/bin/logstash -f test/stdin.conf Settings: Default pipeline workers: 1 Logstash startup completed hello world { "message" => "hello world", "@version" => "1", "@timestamp" => "2017-05-24T09:07:43.228Z", "type" => "std-lqb", "key" => "value22222222222222222222222222222222222222222222", "tags" => [ [0] "add", [1] "xxyy", [2] "abc" ], "host" => "localhost.localdomain" } #########根據(jù)tags來進(jìn)行判斷: [root@localhost test]# vim stdin_2.conf input { stdin { add_field =>{"key11"=>"value22"} codec=>"plain" tags=>["add","xxyy"] type=>"std" } } output { if "tttt" in [tags]{ stdout { codec=>rubydebug{} } } else if "add" in [tags]{ stdout { codec=>json } } } [root@localhost logstash]# /usr/local/logstash/bin/logstash -f test/stdin_2.con f Settings: Default pipeline workers: 1 Logstash startup completed yyxxx {"message":"yyxxx","@version":"1","@timestamp":"2017-05-24T09:32:25.840Z","type":"std","key11":"value22","tags":["add","xxyy"],"host":"localhost.localdomain"} {"message":"","@version":"1","@timestamp":"2017-05-24T09:32:32.480Z","type":"std","key11":"value22","tags":["add","xxyy"],"host":"localhost.localdomain"}xxyy {"message":"xxyy","@version":"1","@timestamp":"2017-05-24T09:32:42.249Z","type":"std","key11":"value22","tags":["add","xxyy"],"host":"localhost.localdomain"}
(2).讀取文件。Logstash 使用一個(gè)名叫 FileWatch 的 Ruby Gem 庫來監(jiān)聽文件變化。這個(gè)庫支持 glob 展開文件路徑,而且會記錄一個(gè)叫 .sincedb 的數(shù)據(jù)庫文件來跟蹤被監(jiān)聽的日志文件的當(dāng)前讀取位置。所以,不要擔(dān)心 logstash 會漏過你的數(shù)據(jù).
[root@localhost test]# cat log.conf input { file { path =>"/usr/local/nginx/logs/access.log" type=>"system" start_position =>"beginning" } } output { stdout { codec => rubydebug } } [root@localhost logstash]# /usr/local/logstash/bin/logstash -f test/log.conf Settings: Default pipeline workers: 1 Logstash startup completed { "message" => "192.168.181.231 - - [24/May/2017:15:04:29 +0800] \"GET / HTTP/1.1\" 502 537 \"-\" \"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36\" \"-\"", "@version" => "1", "@timestamp" => "2017-05-24T09:39:16.600Z", "path" => "/usr/local/nginx/logs/access.log", "host" => "localhost.localdomain", "type" => "system" } { "message" => "192.168.181.231 - - [24/May/2017:15:04:32 +0800] \"GET / HTTP/1.1\" 502 537 \"-\" \"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36\" \"-\"", "@version" => "1", "@timestamp" => "2017-05-24T09:39:16.614Z", "path" => "/usr/local/nginx/logs/access.log", "host" => "localhost.localdomain", "type" => "system" }
解釋:
有一些比較有用的配置項(xiàng),可以用來指定 FileWatch 庫的行為:
discover_interval
logstash 每隔多久去檢查一次被監(jiān)聽的 path
下是否有新文件。默認(rèn)值是 15 秒。
exclude
不想被監(jiān)聽的文件可以排除出去,這里跟 path
一樣支持 glob 展開。
sincedb_path
如果你不想用默認(rèn)的 $HOME/.sincedb
(Windows 平臺上在 C:\Windows\System32\config\systemprofile\.sincedb
),可以通過這個(gè)配置定義 sincedb 文件到其他位置。
sincedb_write_interval
logstash 每隔多久寫一次 sincedb 文件,默認(rèn)是 15 秒。
stat_interval
logstash 每隔多久檢查一次被監(jiān)聽文件狀態(tài)(是否有更新),默認(rèn)是 1 秒。
start_position
logstash 從什么位置開始讀取文件數(shù)據(jù),默認(rèn)是結(jié)束位置,也就是說 logstash 進(jìn)程會以類似 tail -F
的形式運(yùn)行。如果你是要導(dǎo)入原有數(shù)據(jù),把這個(gè)設(shè)定改成 "beginning",logstash 進(jìn)程就從頭開始讀取,有點(diǎn)類似 cat
,但是讀到最后一行不會終止,而是繼續(xù)變成 tail -F
。
通常你要導(dǎo)入原有數(shù)據(jù)進(jìn) Elasticsearch 的話,你還需要 filter/date 插件來修改默認(rèn)的"@timestamp" 字段值。稍后會學(xué)習(xí)這方面的知識。
FileWatch 只支持文件的絕對路徑,而且會不自動遞歸目錄。所以有需要的話,請用數(shù)組方式都寫明具體哪些文件。
LogStash::Inputs::File 只是在進(jìn)程運(yùn)行的注冊階段初始化一個(gè) FileWatch 對象。所以它不能支持類似 fluentd 那樣的 path => "/path/to/%{+yyyy/MM/dd/hh}.log"
寫法。達(dá)到相同目的,你只能寫成 path => "/path/to/*/*/*/*.log"
。
start_position
僅在該文件從未被監(jiān)聽過的時(shí)候起作用。如果 sincedb 文件中已經(jīng)有這個(gè)文件的 inode 記錄了,那么 logstash 依然會從記錄過的 pos 開始讀取數(shù)據(jù)。所以重復(fù)測試的時(shí)候每回需要?jiǎng)h除 sincedb 文件。
因?yàn)?windows 平臺上沒有 inode 的概念,Logstash 某些版本在 windows 平臺上監(jiān)聽文件不是很靠譜。windows 平臺上,推薦考慮使用 nxlog 作為收集端
(3).TCP輸入。未來你可能會用 Redis 服務(wù)器或者其他的消息隊(duì)列系統(tǒng)來作為 logstash broker 的角色。不過 Logstash 其實(shí)也有自己的 TCP/UDP 插件,在臨時(shí)任務(wù)的時(shí)候,也算能用,尤其是測試環(huán)境。
[root@localhost test]# cat tcp.conf input { tcp { port =>8888 mode=>"server" ssl_enable =>false } } output { stdout { codec => rubydebug } } [root@localhost logstash]# /usr/local/logstash/bin/logstash -f test/tcp.conf Settings: Default pipeline workers: 1 Logstash startup completed { "message" => "GET /jenkins/ HTTP/1.1\r", "@version" => "1", "@timestamp" => "2017-05-24T10:09:53.980Z", "host" => "192.168.181.231", "port" => 59426 } { "message" => "Host: 192.168.180.9:8888\r", "@version" => "1", "@timestamp" => "2017-05-24T10:09:54.175Z", "host" => "192.168.181.231", "port" => 59426 } { "message" => "Connection: keep-alive\r", "@version" => "1", "@timestamp" => "2017-05-24T10:09:54.180Z", "host" => "192.168.181.231", "port" => 59426 }
備注:先關(guān)閉8888端口的應(yīng)用,再開啟,會輸出如下日志。
(4)編碼插件Codec:
Codec 是 logstash 從 1.3.0 版開始新引入的概念(Codec 來自 Coder/decoder 兩個(gè)單詞的首字母縮寫)。在此之前,logstash 只支持純文本形式輸入,然后以過濾器處理它。但現(xiàn)在,我們可以在輸入 期處理不同類型的數(shù)據(jù),這全是因?yàn)橛辛?nbsp;codec 設(shè)置。我們在第一個(gè)“Hello world”列子中已經(jīng)用過Codec編碼了,rubydebug就是一種Codec雖然它一般只會在stdout插件中,作為配置測試或者調(diào)試的工具。
(4.1)采用JSON編碼,直接輸入預(yù)定義好的 JSON 數(shù)據(jù),這樣就可以省略掉 filter/grok 配置!
配置實(shí)例以nginx為例,具體步驟如下:
a,編輯配置nginx配置文件nginx.conf。把原先的配置文件注釋掉,換成json的格式,然后重啟下你的nginx
[root@localhost test]# vim /usr/local/nginx/conf/nginx.conf user ftp; worker_processes 2; worker_rlimit_nofile 65535; events { use epoll; worker_connections 1024; } http { include mime.types; default_type application/octet-stream; include proxy.conf; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; # log_format json '{"@timestamp":"$time_iso8601",' '"@version":"1",' '"host":"$server_addr",' '"client":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"domain":"$host",' '"url":"$uri",' '"status":"$status"}'; access_log logs/nginx_access.log json; # access_log logs/access.log main; ####################注意:在$request_time和$body_bytes_sent 變量兩頭沒有雙引號"" ,這兩個(gè)數(shù)據(jù)在JSON 里應(yīng)該是數(shù)值類型。
b,編輯下你的logstash配置文件json.conf
[root@localhost test]# vim json.conf input { file { path => "/usr/local/nginx/logs/nginx_access.log" type => "nginx" start_position => "beginning" add_field => { "key"=>"value"} codec => "json" } } output { stdout{ codec => rubydebug{ } } }
c,logstash加載啟動測試:
[root@localhost logstash]# /usr/local/logstash/bin/logstash -f test/json.conf Settings: Default pipeline workers: 1 Logstash startup completed { "@timestamp" => "2017-05-25T03:26:19.000Z", "@version" => "1", "host" => "192.168.180.9", "client" => "192.168.181.231", "size" => 8250, "responsetime" => 0.157, "domain" => "192.168.180.9", "url" => "/", "status" => "200", "path" => "/usr/local/nginx/logs/nginx_access.log", "type" => "nginx", "key" => "value" } { "@timestamp" => "2017-05-25T03:26:19.000Z", "@version" => "1", "host" => "192.168.180.9", "client" => "192.168.181.231", "size" => 450, "responsetime" => 0.017, "domain" => "192.168.180.9", "url" => "/sc.do", "status" => "200", "path" => "/usr/local/nginx/logs/nginx_access.log", "type" => "nginx", "key" => "value" } { "@timestamp" => "2017-05-25T03:26:19.000Z", "@version" => "1", "host" => "192.168.180.9", "client" => "192.168.181.231", "size" => 16, "responsetime" => 0.083, "domain" => "192.168.180.9", "url" => "/logger/catch.do", "status" => "200", "path" => "/usr/local/nginx/logs/nginx_access.log", "type" => "nginx", "key" => "value" } { "@timestamp" => "2017-05-25T03:26:19.000Z", "@version" => "1", "host" => "192.168.180.9", "client" => "192.168.181.231", "size" => 41153, "responsetime" => 0.362, "domain" => "192.168.180.9", "url" => "/getPageData.do", "status" => "200", "path" => "/usr/local/nginx/logs/nginx_access.log", "type" => "nginx", "key" => "value" } { "@timestamp" => "2017-05-25T03:26:20.000Z", "@version" => "1", "host" => "192.168.180.9", "client" => "192.168.181.231", "size" => 51042, "responsetime" => 0.565, "domain" => "192.168.180.9", "url" => "/getPageData.do", "status" => "200", "path" => "/usr/local/nginx/logs/nginx_access.log", "type" => "nginx", "key" => "value"
(4.2)合并多行數(shù)據(jù)(Multiline):有些時(shí)候,應(yīng)用程序調(diào)試日志會包含非常豐富的內(nèi)容,為一個(gè)事件打印出很多行內(nèi)容。這種日志通常都很難通過命令行解析的方式做分析。 logstash 正為此準(zhǔn)備好了 codec/multiline 插件。multiline 插件也可以用于其他類似的堆棧式信息,比如 linux 的內(nèi)核日志。
當(dāng)啟動logstash及配置文件時(shí)會讓你輸入一連串的字符,知道輸入[ 時(shí)才終止當(dāng)前輸入,如下:
[
root@localhost test]# vim multiline.conf input { stdin { codec => multiline { pattern => "^\[" negate => true what => "previous" } } } output { stdout { codec => rubydebug{ } } } [root@localhost logstash]# /usr/local/logstash/bin/logstash -f test/multiline.c onf Settings: Default pipeline workers: 1 Logstash startup completed hello hello world how are you abc2345 [ { "@timestamp" => "2017-05-25T03:44:35.604Z", "message" => "[\nhello\nhello world\nhow are you \nabc2345", "@version" => "1", "tags" => [ [0] "multiline" ], "host" => "localhost.localdomain" }
總之,這個(gè)插件的原理很簡單,就是把當(dāng)前行的數(shù)據(jù)添加到前面一行后面,直到新進(jìn)的當(dāng)前行匹配 "[" 正則為止。這個(gè)正則還可以用 grok 表達(dá)式。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。