1. 程式人生 > >Logstash筆記(二) ----input插件

Logstash筆記(二) ----input插件

hello world

在"hello World" 示例中,我們已經見到並介紹了Logstash 的運行流程和配置的基礎語法。 請記住一個原則:Logstash 配置一定要有一個 input 和一個 output。在演示過程中,如果沒有寫明 input,默認就會使用 "hello world" 裏我們已經演示過的 input/stdin ,同理,沒有寫明的 output 就是 output/stdout

如果有什麽問題的話,請查看該文檔http://udn.yyuap.com/doc/logstash-best-practice-cn/input/index.html。以下是input插件的具體解釋:

(1),標準輸入。type和tags是logstash事件中特殊的字段。 type 用來標記事件類型

—— 我們肯定是提前能知道這個事件屬於什麽類型的。而 tags 則是在數據處理過程中,由具體的插件來添加或者刪除的

[[email protected] test]# vim stdin.conf 
input {
    stdin {
        add_field => {"key" => "value"}
        codec => "plain"
        tags => ["add"]
        type => "std-lqb"
    }
}
output {
     stdout {
                        codec => rubydebug
                }
}
[[email protected]
/* */ 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進行修改,添加tags列 [[email protected]
/* */ test]# vim stdin.conf input { stdin { add_field => {"key" => "value2222222222222222222222222222222222222222222 2"} codec => "plain" tags => ["add","xxyy","abc"] type => "std-lqb" } } output { stdout { codec => rubydebug } } [[email protected] 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" } #########根據tags來進行判斷: [[email protected] 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 } } } [[email protected] 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 使用一個名叫 FileWatch 的 Ruby Gem 庫來監聽文件變化。這個庫支持 glob 展開文件路徑,而且會記錄一個叫 .sincedb 的數據庫文件來跟蹤被監聽的日誌文件的當前讀取位置。所以,不要擔心 logstash 會漏過你的數據.

[[email protected] test]# cat  log.conf
input {  
  file {  
   path =>"/usr/local/nginx/logs/access.log"
   type=>"system"  
  start_position =>"beginning"  
}  
}  
  
output {  
        stdout {  
                        codec => rubydebug  
                }   
}

[[email protected] 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"
}

解釋:

有一些比較有用的配置項,可以用來指定 FileWatch 庫的行為:

  • discover_interval

logstash 每隔多久去檢查一次被監聽的 path 下是否有新文件。默認值是 15 秒。

  • exclude

不想被監聽的文件可以排除出去,這裏跟 path 一樣支持 glob 展開。

  • sincedb_path

如果你不想用默認的 $HOME/.sincedb(Windows 平臺上在 C:\Windows\System32\config\systemprofile\.sincedb),可以通過這個配置定義 sincedb 文件到其他位置。

  • sincedb_write_interval

logstash 每隔多久寫一次 sincedb 文件,默認是 15 秒。

  • stat_interval

logstash 每隔多久檢查一次被監聽文件狀態(是否有更新),默認是 1 秒。

  • start_position

logstash 從什麽位置開始讀取文件數據,默認是結束位置,也就是說 logstash 進程會以類似 tail -F 的形式運行。如果你是要導入原有數據,把這個設定改成 "beginning",logstash 進程就從頭開始讀取,有點類似 cat,但是讀到最後一行不會終止,而是繼續變成 tail -F

註意

  1. 通常你要導入原有數據進 Elasticsearch 的話,你還需要 filter/date 插件來修改默認的"@timestamp" 字段值。稍後會學習這方面的知識。

  2. FileWatch 只支持文件的絕對路徑,而且會不自動遞歸目錄。所以有需要的話,請用數組方式都寫明具體哪些文件。

  3. LogStash::Inputs::File 只是在進程運行的註冊階段初始化一個 FileWatch 對象。所以它不能支持類似 fluentd 那樣的 path => "/path/to/%{+yyyy/MM/dd/hh}.log" 寫法。達到相同目的,你只能寫成 path => "/path/to/*/*/*/*.log"

  4. start_position 僅在該文件從未被監聽過的時候起作用。如果 sincedb 文件中已經有這個文件的 inode 記錄了,那麽 logstash 依然會從記錄過的 pos 開始讀取數據。所以重復測試的時候每回需要刪除 sincedb 文件。

  5. 因為 windows 平臺上沒有 inode 的概念,Logstash 某些版本在 windows 平臺上監聽文件不是很靠譜。windows 平臺上,推薦考慮使用 nxlog 作為收集端


(3).TCP輸入。未來你可能會用 Redis 服務器或者其他的消息隊列系統來作為 logstash broker 的角色。不過 Logstash 其實也有自己的 TCP/UDP 插件,在臨時任務的時候,也算能用,尤其是測試環境。

[[email protected] test]# cat tcp.conf 
input {  
 tcp {  
   port =>8888  
   mode=>"server"  
  ssl_enable =>false  
 }  
}  
  
output {  
        stdout {  
                        codec => rubydebug  
                }  
}
[[email protected] 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
}

備註:先關閉8888端口的應用,再開啟,會輸出如下日誌。


本文出自 “清風明月” 博客,請務必保留此出處http://liqingbiao.blog.51cto.com/3044896/1929103

Logstash筆記(二) ----input插件