ELK 安裝Logstash

章節


ELK中,Logstash不是必須安裝的。html

Logstash是一個功能強大的工具,提供了大量的插件,用於解析、加工各類來自數據源的數據。若是Beat採集的數據,須要加工處理後才能使用,就須要將集成Logstash。java

要下載安裝Logstash,打開一個命令行窗口,執行如下命令:linux

Logstash依賴Java 8 或 Java 11,確保已經安裝了Java。shell

[root@qikegu ~]# java --version
openjdk 11.0.3 2019-04-16 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.3+7-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.3+7-LTS, mixed mode, sharing)

deb:app

curl -L -O https://artifacts.elastic.co/downloads/logstash/logstash-7.1.0.deb
sudo dpkg -i logstash-7.1.0.deb

rpm:curl

curl -L -O https://artifacts.elastic.co/downloads/logstash/logstash-7.1.0.rpm
sudo rpm -i logstash-7.1.0.rpm

mac and linux:elasticsearch

curl -L -O https://artifacts.elastic.co/downloads/logstash/logstash-7.1.0.tar.gz
tar -xzvf logstash-7.1.0.tar.gz

win:ide

  1. 從Logstash下載頁面下載Logstash 7.1.0 Windows zip文件。
  2. 將zip文件的內容解壓到一個目錄,例如C:\Program Files

要了解有關安裝、配置和運行Logstash的更多信息,請閱讀官網文檔工具

配置Logstash偵聽Beats輸入

Logstash提供了輸入插件,用於接受數據輸入。在本教程中,你將建立一個Logstash管道配置,用於偵聽Beat輸入,並將接收到的數據發送到Elasticsearch。學習

配置Logstash

建立一個新的Logstash管道配置文件,命名爲demo-metrics-pipeline.conf。若是將Logstash安裝爲deb或rpm包,請在Logstash配置目錄(例如:/etc/logstash/conf.d/)中建立文件。

文件必須包含:

  • 輸入配置,設置beat端口爲5044
  • 輸出配置,配置elasticsearch的相關信息

示例:

input {
  beats {
    port => 5044
  }
}

# The filter part of this file is commented out to indicate that it
# is optional.
# filter {
#
# }

output {
  elasticsearch {
    hosts => "localhost:9200"
    manage_template => false
    index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
  }
}

使用此管道配置啓動Logstash時,Beat事件都將經過Logstash發往elasticsearch,在Logstash中,能夠使用Logstash的強大功能分析加工數據。

啓動 Logstash

啓動Logstash,若是將Logstash安裝爲deb或rpm包,請確保配置文件位於配置目錄中。

deb:

sudo /etc/init.d/logstash start

rpm:

sudo service logstash start

mac and linux:

./bin/logstash -f demo-metrics-pipeline.conf

win:

./bin/logstash -f demo-metrics-pipeline.conf

Logstash開始監聽Beat發送過來的事件。接下來,須要配置Metricbeat,將事件發送到Logstash。

配置Metricbeat將事件發送到Logstash

默認狀況下,Metricbeat將事件發送到Elasticsearch。

要將事件發送到Logstash,需修改配置文件metricbeat.yml。能夠在Metricbeat安裝目錄下找到這個文件,或者/etc/metricbeat(rpm和deb)。

註釋掉output.elasticsearch部分,啓用output.logstash部分:

#-------------------------- Elasticsearch output ------------------------------
#output.elasticsearch:
  # Array of hosts to connect to.
  #hosts: ["localhost:9200"]

...

#----------------------------- Logstash output --------------------------------
output.logstash:
  # The Logstash hosts
  hosts: ["localhost:5044"]

保存文件,從新啓動Metricbeat,使配置更改生效。

定義過濾器,從字段中提取數據

目前,Logstash只是將事件轉發給Elasticsearch,沒有進行處理。接下來,將學習使用過濾器。

Metricbeat收集的系統數據,包括一個名爲cmdline的字段,該字段包含了進程啓動的完整命令行參數。例如:

"cmdline": "/Applications/Firefox.app/Contents/MacOS/plugin-container.app/Contents/MacOS/plugin-container -childID 3
-isForBrowser -boolPrefs 36:1|299:0| -stringPrefs 285:38;{b77ae304-9f53-a248-8bd4-a243dbf2cab1}| -schedulerPrefs
0001,2 -greomni /Applications/Firefox.app/Contents/Resources/omni.ja -appomni
/Applications/Firefox.app/Contents/Resources/browser/omni.ja -appdir
/Applications/Firefox.app/Contents/Resources/browser -profile
/Users/dedemorton/Library/Application Support/Firefox/Profiles/mftvzeod.default-1468353066634
99468 gecko-crash-server-pipe.99468 org.mozilla.machname.1911848630 tab"

你可能只須要命令的路徑,而不是將整個命令行參數發送給Elasticsearch。一種方法是使用Grok過濾器,學習Grok超出了本教程的範圍,可是若是想了解更多,請參閱Grok filter插件文檔

要提取路徑,在前面建立的Logstash配置文件中,在輸入和輸出部分之間,添加如下Grok過濾器:

filter {
  if [system][process] {
    if [system][process][cmdline] {
      grok {
        match => { 
          "[system][process][cmdline]" => "^%{PATH:[system][process][cmdline_path]}"
        }
        remove_field => "[system][process][cmdline]" 
      }
    }
  }
}
  • 使用模式匹配路徑,而後將路徑存儲在名爲cmdline_path的字段中。
  • 刪除原始字段cmdline,所以在Elasticsearch中不爲其創建索引。

完成後,完整的配置文件應該以下所示:

input {
  beats {
    port => 5044
  }
}

filter {
  if [system][process] {
    if [system][process][cmdline] {
      grok {
        match => {
          "[system][process][cmdline]" => "^%{PATH:[system][process][cmdline_path]}"
        }
        remove_field => "[system][process][cmdline]"
      }
    }
  }
}

output {
  elasticsearch {
    hosts => "localhost:9200"
    manage_template => false
    index => "qikegu-%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
  }
}

從新啓動Logstash,使配置生效。該事件如今包含一個名爲cmdline_path的字段,包含命令的路徑:

"cmdline_path": "/Applications/Firefox.app/Contents/MacOS/plugin-container.app/Contents/MacOS/plugin-container"
相關文章
相關標籤/搜索