regex 剖析器允許針對收集的資料使用一些規則運算式。
vRealize Log Insight 代理程式會使用 C++ Boost 程式庫 regex,並採用 Perl 語法。 可以透過指定包含具名擷取群組的規則運算式模式,來定義 regex 剖析器。例如:(?<field_1>\d{4})[-](?<field_2>\d{4})[-](?<field_3>\d{4})[-](?<field_4>\d{4})
群組中所指定的名稱 (例如:
field_1、
field_2、
field_3 及
field_4) 將成為對應擷取的欄位的名稱。名稱具有下列需求:
- 規則運算式模式中指定的名稱必須是 vRealize Log Insight 的有效欄位名稱。
- 名稱可以僅包含英數字元和底線「_」字元。
- 名稱開頭不能為數位字元。
如果提供無效的名稱,則組態會失敗。
Regex 剖析器選項
regex 剖析器的唯一必要選項為 format 選項。
可以在需要其他偵錯資訊時使用 debug 選項。
組態
若要建立 regex 剖析器,請使用 regex 做為 base_parser,並提供 format 選項。
Regex 組態範例
下列範例可用於分析 1234-5678-9123-4567:
[parser|regex_parser]
base_parser=regex
format=(?<tag1>\d{4})[-](?<tag2>\d{4})[-](?<tag3>\d{4})[-](?<tag4>\d{4})
[filelog|some_info]
directory=D:\Logs
include=*.txt
parser=regex_parser
結果會顯示:
tag1=1234 tag2=5678 tag3=9123 tag4=4567
若要使用 regex 剖析器來剖析 Apache 記錄,請為 Apache 記錄提供特定的 regex 格式:
[parser|regex_parser] base_parser=regex format=(?<remote_host>.*) (?<remote_log_name>.*) (?<remote_auth_user>.*) \[(?<log_timestamp>.*)\] "(?<request>.*)" (?<status_code>.*) (?<response_size>.*)
結果會顯示:
127.0.0.1 - admin [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326 remote_host=127.0.0.1 remote_log_name=- remote_auth_user=admin log_timestamp=10/Oct/2000:13:55:36 -0700 request=GET /apache_pb.gif HTTP/1.0 status_code=200 response_size=2326
下列程式碼顯示剖析 Apache 記錄的另一個範例。
[parser|regex_parser] base_parser=regex format=(?<remote_host>.* (?<remote_log_name>.*)) (?<remote_auth_user>.*) \[(?<log_timestamp>.*)\] "(?<request>.* (?<resource>.*) (?<protocol>.*))" (?<status_code>.*) (?<response_size>.*) 127.0.0.1 unknown - [17/Nov/2015:15:17:54 +0400] \"GET /index.php HTTP/1.1\" 200 4868 remote_host=127.0.0.1 unknown remote_log_name=unknown remote_auth_user=- log_timestamp=17/Nov/2015:15:17:54 +0400 request=GET /index.php HTTP/1.1 resource=/index.php protocol=HTTP/1.1 status_code=200 response_size=4868
效能考量事項
與其他剖析器 (如 CLF 剖析器) 相比,regex 剖析器會耗用更多資源。如果您可以使用其他剖析器來剖析記錄,請考慮使用這些剖析器而不使用 regex 剖析器,以獲得更佳的效能。
如果未提供剖析器並且您要使用
regex 剖析器,請盡量定義清晰明確的格式。下列範例顯示提供更佳效能結果的組態。此範例指定具有數位值的欄位。
(?<remote_host>\d+.\d+.\d+.\d+) (?<remote_log_name>.*) (?<remote_auth_user>.*) \[(?<log_timestamp>.*)\] "(?<request>.*)" (?<status_code>\d+) (?<response_size>\d+)