Graylog: 入門指南
如何開始使用 Graylog
👋 歡迎來到 Stackhero 文件!
Stackhero 提供即用型的 Graylog cloud 解決方案,帶來多項好處,包括:
- 包含無限和專用的 SMTP 電郵伺服器。
- 只需一鍵即可輕鬆更新。
- 使用 HTTPS 保護的可自訂域名(例如,https://logs.your-company.com)。
- 由專用私有 VM提供的最佳性能和強大安全性。
節省時間並簡化您的生活:只需 5 分鐘即可嘗試 Stackhero 的 Graylog cloud hosting 解決方案!
在 Graylog 中創建第一個輸入
輸入是 Graylog 接收日誌的端點。您可以使用 TCP 或 UDP 發送日誌。此外,Graylog 還可以從 API、Kafka 隊列、RabbitMQ 伺服器和其他方法收集日誌條目。
在此示例中,我們將創建一個原始 UDP 輸入。首先,打開 Graylog 介面並導航到「System」然後「Inputs」。選擇「Raw/Plaintext UDP」並點擊「Launch new input」。使用以下值配置您的輸入並驗證表單:
- Node: 選擇您的節點
- Title: RAW UDP
- Port: 5555
接下來,在您的電腦上打開終端並向您的 Graylog 伺服器發送 UDP 訊息。記得將 XXXXXX 替換為您的服務域名:
- 在 macOS 上:
echo "Hello Graylog from UDP" | nc -u -w1 -c <XXXXXX>.stackhero-network.com 5555 - 在 Linux 上:
echo "Hello Graylog from UDP" | nc -u -w1 <XXXXXX>.stackhero-network.com 5555
發送訊息後,返回 Graylog 並點擊「Search」。您應該會看到您的訊息 🎉
恭喜,您已經成功向 Graylog 發送了第一條訊息!隨時創建其他輸入和儀表板以滿足您的需求。如需進一步指導,您可以參考 Graylog 的官方文檔。
Graylog 代碼示例
我們的 Git 存儲庫 中提供了多個代碼示例。此存儲庫提供實用的實現和額外的自定義,以幫助您充分利用 Graylog。
使用 TLS 加密將 rsyslog 日誌發送到 Graylog
如果您有 rsyslog 客戶端並希望安全地將日誌發送到 Graylog,請按照以下步驟操作:
不要在 Graylog 的輸入上啟用任何 TLS 選項。TLS 將由您的實例上的反向代理直接管理,因此 Graylog 不會處理它。
-
前往 Stackhero 儀表板中的 Graylog 服務配置,並為 Syslog TCP 埠 514 啟用「TLS encryption」。
-
按照以下說明更新您的 rsyslog 配置。將
<XXXXXX>.stackhero-network.com替換為您的實例主機名:# 定義 TLS CA 證書 global( DefaultNetstreamDriver="gtls" DefaultNetstreamDriverCAFile="/etc/ssl/certs/ca-certificates.crt" ) # 將所有日誌發送到遠程伺服器 # 為此操作創建了一個磁碟隊列。如果遠程主機 # 掉線,訊息將被緩存到磁碟,並在其重新可用時發送 # 參見 https://www.rsyslog.com/doc/v8-stable/configuration/actions.html # 和 https://www.rsyslog.com/doc/v8-stable/configuration/modules/omfwd.html *.* action( type="omfwd" target="<XXXXXX>.stackhero-network.com" port="514" protocol="tcp" KeepAlive="on" KeepAlive.Interval="30" StreamDriver="gtls" StreamDriverMode="1" StreamDriverAuthMode="x509/name" ResendLastMSGOnReconnect="on" queue.filename="fwdRule1" # 唯一名稱前綴,用於緩存文件 queue.type="LinkedList" queue.maxDiskSpace="256m" queue.saveOnShutdown="on" action.resumeRetryCount="-1" action.resumeInterval="30" ) -
重啟您的 rsyslog 服務,並使用以下命令發送日誌以驗證配置:
logger This is a test
這樣就完成了設置。您現在正在使用 TLS 加密安全地將日誌發送到 Graylog!
處理錯誤「failed to parse field [XXXX] of type [YYYY]」
您可能會遇到如下錯誤:
org.opensearch.index.mapper.MapperParsingException: failed to parse field [time] of type [long] in document with id 'xxxx'
您可以在 Stackhero 儀表板提供的日誌中或在 Graylog 管理面板的 System > Overview > Indexer failures 下查看此錯誤。
此錯誤表示發送的日誌中 time 字段的值與預期類型不匹配(在此情況下,應為「long」類型的數值)。Graylog 利用 OpenSearch 的動態映射功能。當日誌首次發送時,OpenSearch 會嘗試猜測字段類型。例如,如果日誌包含 time 字段,且數值為 1234,OpenSearch 將其定義為數值字段。如果另一條日誌將 time 字段設為 "abcd",即字符串,OpenSearch 將拒絕它,因為它預期為數值。
請記住,字段名稱 time 僅用於說明。它可以是任何字段名稱和類型。
要解決此問題,您需要重新定義 OpenSearch 預期的類型。更多信息,請參考 Graylog 官方文檔。
更新 OpenSearch 映射
在繼續之前,請在 Stackhero 儀表板中啟用 OpenSearch 訪問。導航到您的 Graylog 服務,然後點擊「Configure」按鈕以激活 OpenSearch 訪問。
請小心這些更改,因為不正確的配置可能會阻止您的 OpenSearch 集群並可能導致數據丟失。如果您不確定,請不要繼續。
-
定義您的新映射。在此示例中,我們將
time字段重新定義為字符串類型。您可以在 OpenSearch 字段數據類型文檔中找到可用類型。 -
將以下內容保存到名為
graylog-custom-mapping.json的文件中:{ "template": "graylog_*", "mappings": { "message": { "properties": { "time": { "type": "string", "index": "not_analyzed" } } } } } -
使用以下 curl 命令發送此文件(將
<XXXXXX>.stackhero-network.com替換為您的實例域名):curl -u 'admin' -X PUT -d @'graylog-custom-mapping.json' -H 'Content-Type: application/json' 'https://<XXXXXX>.stackhero-network.com/opensearch/_template/graylog-custom-mapping?pretty'您應該收到如下回應:
{ "acknowledged": true } -
最後,使用此命令驗證映射是否已更新(根據需要替換域名):
curl -u 'admin' -X GET 'https://<XXXXXX>.stackhero-network.com/opensearch/graylog_deflector/_mapping?pretty'
處理錯誤「Unable to write audit log entry」
如果您看到如下錯誤:
Unable to write audit log entry because there is no valid license
或
Not running cleanup for auditlog entries in MongoDB because there is no valid license
這是因為 Graylog Enterprise 已啟用但沒有有效的許可。如果您有許可,可以在 Graylog 介面中輸入。如果沒有許可,只需在 Stackhero 儀表板中禁用 Graylog Enterprise。