注意:

使用 Fluentd 將日誌擷取至 OCI 日誌記錄分析

簡介

使用開源資料收集器軟體 Fluentd 從您的來源收集日誌資料。安裝 OCI 日誌記錄分析輸出 Plugin,將收集的日誌資料遞送至 Oracle Cloud 日誌記錄分析。

注意:Oracle 建議您使用 Oracle Cloud Management Agent 將日誌資料擷取至 Oracle Cloud Logging Analytics 時的最佳體驗。不過,如果這不是您使用案例的可能選項,則只能使用 OCI Logging Analytics 輸出 Plugin 進行 Fluentd。

本教學課程中,根據在 Oracle Linux 上安裝的 td-agent rpm 套裝程式使用 Fluentd 設定,但其他 Fluentd 發行版本的必要步驟可能類似。

Fluentd 含有可從輸入來源收集日誌資料的元件、轉換日誌,以及將日誌資料遞送到所需的輸出。您可以安裝並設定 Fluentd 的輸出 Plugin,將日誌從各種來源擷取至 Oracle Cloud 日誌記錄分析。

fluentd_plugin_overview.png 圖解說明

目標

將 OCI 日誌記錄分析輸出 Plugin 從版本 1.x 移轉至 2.x

如果您是 OCI Logging Analytics 輸出 Plugin 的新使用者,但尚未下載與安裝,請略過此子區段,然後前往先決條件。如果您已使用 fluent-plugin-oci-logging-analytics-1.0.0.gem 檔案安裝 版本 1.x 外掛程式,請記下您可能需要變更才能移轉至外掛程式 版本 2.x

1.x 2.x
global_metadata oci_la_global_metadata
中繼資料 oci_la_metadata
個體 ID oci_la_entity_id
entityType oci_la_entity_type
logSourceName oci_la_log_source_name
logPath oci_la_log_path
logGroupId oci_la_log_group_id

必要條件

建立 Fluentd 組態檔

若要設定 Fluentd,將日誌資料遞送至 Oracle Cloud Logging Analytics,請編輯 Fluentd 或 td-agent 提供的組態檔,並提供與 Oracle Cloud Logging Analytics 和其他自訂項目有關的資訊。

Fluentd 輸出外掛程式設定將採用下列格式:

<match pattern>
@type oci-logging-analytics
 namespace                   <YOUR_OCI_TENANCY_NAMESPACE>

# Auth config file details
 config_file_location        ~/.oci/config 
 profile_name                DEFAULT

# When there is no credentials for proxy
 http_proxy                  "#{ENV['HTTP_PROXY']}"

# To provide proxy credentials
 proxy_ip                    <IP>
 proxy_port                  <port>
 proxy_username              <user>
 proxy_password              <password>

# Configuration for plugin (oci-logging-analytics) generated logs
 plugin_log_location       "#{ENV['FLUENT_OCI_LOG_LOCATION'] || '/var/log'}" 
 plugin_log_level          "#{ENV['FLUENT_OCI_LOG_LEVEL'] || 'info'}"
 plugin_log_rotation       "#{ENV['FLUENT_OCI_LOG_ROTATION'] || 'daily'}"  **(DEPRECATED)**
 plugin_log_file_size      "#{ENV['FLUENT_OCI_LOG_AGE'] || '1MB'}"
 plugin_log_file_count     "#{ENV['FLUENT_OCI_LOG_AGE'] || '10'}"

# Buffer Configuration
 <buffer>
       @type file
       path                                "#{ENV['FLUENT_OCI_BUFFER_PATH'] || '/var/log'}"
       flush_thread_count                  "#{ENV['FLUENT_OCI_BUFFER_FLUSH_THREAD_COUNT'] || '10'}"
       retry_wait                          "#{ENV['FLUENT_OCI_BUFFER_RETRY_WAIT'] || '2'}"                     #seconds
       retry_max_times                     "#{ENV['FLUENT_OCI_BUFFER_RETRY_MAX_TIMES'] || '10'}"
       retry_exponential_backoff_base      "#{ENV['FLUENT_OCI_BUFFER_RETRY_EXPONENTIAL_BACKOFF_BASE'] || '2'}" #seconds
       retry_forever                       true
       overflow_action                     block
       disable_chunk_backup                true
 </buffer>
	</match>

建議設定次要外掛程式,當輸出外掛程式持續寫入緩衝區區塊時,Fluentd 將用來傾印備份資料,並超過重試的逾時臨界值。此外,如果發生無法復原的錯誤,Fluentd 會立即中止區塊,並將其移至次要或備份目錄。請參閱 Fluentd 文件:次要輸出

輸出外掛程式組態參數

提供適當的值給 Fluentd 組態檔中的下列參數:

組態參數 描述
命名空間 ( 必要參數 ) 要上傳收集之日誌資料的 OCI 租用戶命名空間
config_file_location 包含 OCI 認證詳細資訊的組態檔位置
profile_name 要從組態檔使用的 OCI 組態設定檔名稱
http_proxy 沒有證明資料的代理主機。範例:www.proxy.com:80
proxy_ip 需要證明資料時提供代理主機 IP 詳細資訊。範例:www.proxy.com
proxy_port 需要證明資料時,代理主機連接埠詳細資訊。範例:80
proxy_username 代理主機使用者名稱詳細資訊
proxy_password 需要證明資料時提供代理主機密碼詳細資訊
plugin_log_location 寫入自己日誌之輸出 Plugin 的檔案路徑。確定路徑存在而且可以存取。預設值:工作目錄
plugin_log_level 輸出外掛程式記錄層級:除錯 < INFO < WARN < ERROR < FATAL < UNKNOWN。預設值:INFO
plugin_log_rotation (DEPRECATED) 輸出外掛程式日誌檔循環頻率:每日、每週或每月。預設值:每日
plugin_log_file_size 要旋轉日誌檔的日誌檔大小上限。(1 KB、1MB 等)。預設值:1 MB
plugin_log_file_count 要保留的存檔 / 循環日誌檔數目 (大於 0)。預設值:10。

如果您沒有為 OCI 運算節點指定 config_file_locationprofile_name 參數,則會使用 instance_principal 型認證。

緩衝區組態參數

在上一個區段中編輯的相同組態檔中,修改緩衝區區段並提供下列必要資訊:

必要參數 描述
@type 這裡指定後端使用的外掛程式 。輸入檔案
path 儲存緩衝區檔案的路徑。確定路徑存在而且可以存取。

緩衝區區塊中可以包含下列選擇性參數:

選擇性參數 預設值 描述
flush_thread_count 1 平行清除 / 寫入區塊的執行緒數目。
retry_wait 1s 下次重試清除之前的等待秒數。
retry_max_times 只有當 retry_forever 欄位為 false 時,才需要此選項。
retry_exponential_backoff_base 2 下一個指數倒退係數的等待秒數。
retry_forever false 若為 true ,外掛程式將會忽略 retry_max_times 選項,並永久重試清除。
overflow_action throw_exception 可能的值:throw_exception / block / drop_oldest_chunk。建議值:區塊
disable_chunk_backup false 指定 false 時,備份目錄中的不可復原區塊將會被捨棄。
chunk_limit_size 8MB 每一區塊的大小上限。事件會寫入區塊中,直到區塊大小變成此大小為止。注意:不論指定的值為何,「記錄日誌分析」輸出 Plugin 目前會將值預設為 1MB。
total_limit_size 64GB (適用於檔案) 儲存的緩衝區大小總計達到此臨界值之後,所有附加作業都會因發生錯誤而失敗 (且資料將會遺失)。
flush_interval 60s 把區塊排清到輸出外掛程式的頻率 。

如需參數可能值的詳細資訊,請參閱 Fluentd Documentation:Buffer Plugins

確認內送日誌事件的格式

內送日誌事件必須使用特定格式,以便 Oracle 提供的 Fluentd Plugin 可以處理日誌資料、分區,然後將其傳輸至 Oracle Cloud 日誌記錄分析。

檢視可用於監督 syslogapachekafka 日誌檔的範例組態,網址為:範例輸入組態

來源 / 輸入外掛程式設定

syslog 日誌的來源組態範例:

<source>
  @type tail
  @id in_tail_syslog
  multiline_flush_interval 5s
  path /var/log/messages*
  pos_file /var/log/messages*.log.pos
  read_from_head true
  path_key tailed_path
  tag oci.syslog
  <parse>
    @type json
  </parse>
</source>

下列參數是定義來源區塊的必要參數:

來源區塊中可以包含下列選擇性參數:

如需其他參數的相關資訊,請參閱 Fluentd Documentation:tail

篩選組態

使用這些參數列出必須用來處理日誌的日誌記錄分析資源。

若要確保 Oracle 提供的輸出 Plugin 可處理輸入來源的日誌,請確定輸入日誌事件符合指定的格式,例如設定 record_transformer 篩選 Plugin 以變更格式。

提示:請注意,設定 record_transformer 篩選外掛程式的方式只是內送事件中包含必要參數的其中一種方式。如需其他方法,請參閱 Fluentd 文件

範例篩選組態:

    <filter oci.kafka>
    @type record_transformer
    enable_ruby true
    <record>
        oci_la_metadata KEY_VALUE_PAIRS
        oci_la_entity_id LOGGING_ANALYTICS_ENTITY_OCID              # If same across sources. Else keep this in individual filters
        oci_la_entity_type LOGGING_ANALYTICS_ENTITY_TYPE            # If same across sources. Else keep this in individual filters
        oci_la_log_source_name LOGGING_ANALYTICS_SOURCENAME
        oci_la_log_group_id LOGGING_ANALYTICS_LOGGROUP_OCID
        oci_la_log_path "${record['tailed_path']}"
        message ${record["log"]}                            # Will assign the 'log' key value from json wrapped message to 'message' field
        tag ${tag}
    </record>
    </filter>`

在篩選區塊中提供下列必要資訊:

您可以選擇在篩選區塊中提供下列其他參數:

您可以用來監督下列日誌的範例組態:

安裝輸出 Plugin

請使用 Oracle 提供的 gem 檔案來安裝 OCI Logging Analytics 輸出 Plugin。本節中的步驟適用於根據 Oracle Linux 上安裝的 td-agent rpm 套裝程式 Fluentd 設定。

  1. 執行下列指令來安裝輸出外掛程式:

    gem install fluent-plugin-oci-logging-analytics
    

如需詳細資訊,請參閱 RubyGems 上的 Fluentd 輸出 Plugin 以傳送日誌 / 事件至 OCI 日誌記錄分析https://rubygems.org/gems/fluent-plugin-oci-logging-analytics

  1. Systemd 以 td-agent 使用者啟動 td-agent。讓 td-agent 使用者存取 OCI 檔案和資料夾。若要以服務形式執行 td-agent,請執行 OCI 日誌記錄分析輸出 Plugin 資料夾和 .oci pem 檔案的 chownchgrp 命令,例如 chown td-agent [FILE]

  2. 若要開始收集 Oracle Cloud Logging Analytics 中的日誌,請執行 td-agent:

    TZ=utc /etc/init.d/td-agent start
    

    如果您在日誌收集期間或設定時遇到問題,可以使用日誌檔 /var/log/td-agent/td-agent.log 進行除錯。

    若要隨時停止 td-agent,請執行下列指令:

    TZ=utc /etc/init.d/td-agent stop
    

開始檢視日誌記錄分析中的日誌

前往日誌總管,然後使用 Oracle Cloud Logging Analytics 的視覺化面板來檢視表單中的日誌資料,協助您更加瞭解並分析。您可以根據資料集想要達成的內容,選取最適合您應用程式的視覺化類型。

建立並執行搜尋查詢之後,您可以將日誌搜尋儲存為小工具,以進一步重複使用。

您可以在「儀表板」頁面上新增 Oracle 定義的小工具或您建立的自訂小工具來建立自訂儀表板。

使用 Prometheus 監控 Fluentd

您可以使用 Prometheus 選擇性監控 Fluentd。如需顯示下方單位標準與其他由 Fluentd 發出至 Prometheus 的步驟,請參閱 Fluentd 文件:由 Prometheus 監控。如果您只想要監督核心 Fluentd 和這些測量結果,請略過參考之 Fluentd 文件中的步驟 1:依 Prometheus 篩選 Plugin 計算傳入記錄步驟 2:依 Prometheus 輸出 Plugin 計算傳出記錄

Fluentd 外掛程式會發出下列 Prometheus 格式的衡量指標,提供外掛程式所收集和處理之資料的洞察力:

Metric Name: oci_la_fluentd_output_plugin_records_received 
labels: [:tag,:oci_la_log_group_id,:oci_la_log_source_name,:oci_la_log_set]
Description: Number of records received by the OCI Logging Analytics Fluentd output plugin.
Type : Gauge

Metric Name: oci_la_fluentd_output_plugin_records_valid 
labels: [:tag,:oci_la_log_group_id,:oci_la_log_source_name,:oci_la_log_set]
Description: Number of valid records received by the OCI Logging Analytics Fluentd output plugin.
Type : Gauge 

Metric Name: oci_la_fluentd_output_plugin_records_invalid 
labels: [:tag,:oci_la_log_group_id,:oci_la_log_source_name,:oci_la_log_set,:reason]
Description: Number of invalid records received by the OCI Logging Analytics Fluentd output plugin. 
Type : Gauge

Metric Name: oci_la_fluentd_output_plugin_records_post_error 
labels: [:tag,:oci_la_log_group_id,:oci_la_log_source_name,:oci_la_log_set,:error_code, :reason]
Description: Number of records failed posting to OCI Logging Analytics by the Fluentd output plugin.
Type : Gauge
    
Metric Name: oci_la_fluentd_output_plugin_records_post_success 
labels: [:tag,:oci_la_log_group_id,:oci_la_log_source_name,:oci_la_log_set]
Description: Number of records posted by the OCI Logging Analytics Fluentd output plugin. 
Type : Gauge  

Metric Name: oci_la_fluentd_output_plugin_chunk_time_to_receive
labels: [:tag]
Description: Average time taken by Fluentd to deliver the collected records from Input plugin to OCI Logging Analytics output plugin.
Type : Histogram  

Metric Name: oci_la_fluentd_output_plugin_chunk_time_to_post 
labels: [:oci_la_log_group_id]
Description: Average time taken for posting the received records to OCI Logging Analytics by the Fluentd output plugin.
Type : Histogram

深入瞭解

其他學習資源

探索 docs.oracle.com/learn 上的其他實驗室,或存取 Oracle Learning YouTube 通道上的更多免費學習內容。此外,請造訪 education.oracle.com/learning-explorer 以成為 Oracle Learning Explorer。

如需產品文件,請瀏覽 Oracle Help Center