Simple Log Service Processing Language(SPL)命令と SQL 関数を使用して、大量のログデータをフィルタリングおよびクリーニングし、データ形式を標準化できます。このトピックでは、データのフィルタリングとクリーニングに関する一般的なシナリオと関連操作について説明します。
シナリオ 1:ログのフィルタリング(where 命令)
where 命令を使用してログをフィルタリングします。一般的な変換ルール:
where <bool-expression>
例:
サブシナリオ 1:フィールドの内容に基づいてデータエントリをフィルタリングします。
生のログ
# Log 1 __source__: 192.168.0.1 __tag__:__client_ip__: 192.168.0.2 __tag__:__receive_time__: 1597214851 __topic__: app class: test_case id: 7992 test_string: <function test1 at 0x1027401e0> # Log 2 __source__: 192.168.0.1 __tag__:__client_ip__: 192.168.0.2 __tag__:__receive_time__: 1597214861 __topic__: web class: test_case id: 7992 test_string: <function test1 at 0x1027401e0>
SPL 文
__topic__ フィールドが app であるログを破棄します。
* | where __topic__!='app'
出力
__source__: 192.168.0.1 __tag__:__client_ip__: 192.168.0.2 __tag__:__receive_time__: 1597214861 __topic__: web class: test_case id: 7992 test_string: <function test1 at 0x1027401e0>
サブシナリオ 2:フィールド名に一致する正規表現を使用してデータエントリをフィルタリングします。
生のログ
# Log 1 __source__: 192.168.0.1 __tag__:__client_ip__: 192.168.0.2 __tag__:__receive_time__: 1597214851 __topic__: app class: test_case id: 7992 test_string: <function test1 at 0x1027401e0> server_protocol:test # Log 2 __source__: 192.168.0.1 __tag__:__client_ip__: 192.168.0.2 __tag__:__receive_time__: 1597214861 __topic__: web class: test_case id: 7992 test_string: <function test1 at 0x1027401e0> server_protocol: 14861
SPL 文
server_protocol が数値であるフィールドを保持します。
* | where regexp_like(server_protocol, '\d+')
出力
__source__: 192.168.0.1 __tag__:__client_ip__: 192.168.0.2 __tag__:__receive_time__: 1597214861 __topic__: web class: test_case id: 7992 test_string: <function test1 at 0x1027401e0> server_protocol: 14861
シナリオ 2:ログの欠落フィールドに値を割り当てる(extend および parse-regexp 命令)
extend 命令と parse-regexp 命令を使用してログをフィルタリングします。例:
サブシナリオ 1:フィールドが存在しないか空の場合、フィールドに値を割り当てます。
* | extend <output>=<expression>, ...
入力
name:
SPL 文:name フィールドに値を割り当てます
* | extend name='lily'
出力
name:lily
サブシナリオ 2:正規表現を使用して、テキストフィールドから構造化コンテンツを抽出します。
* | parse-regexp -flags=<flags> <field>, <pattern> as <output>, ...
入力
content: '10.0.0.0 GET /index.html 15824 0.043'
SPL 文
* | parse-regexp content, '(\S+)' as ip | parse-regexp content, '\S+\s+(\w+)' as method
出力
content: '10.0.0.0 GET /index.html 15824 0.043' ip: '10.0.0.0' method: 'GET'
サブシナリオ 3:複数のフィールドに値を割り当てます。
* | extend <output>=<expression> | extend <output1>=<expression> | <output2>=<expression>
入力
__source__: 192.168.0.1 __topic__: __tag__: __receive_time__: id: 7990 test_string: <function test1 at 0x1020401e0>
SPL 文
__topic__、__tag__、および __receive_time__ フィールドに値を割り当てます。
* | extend __topic__='app' | extend __tag__='stu' | extend __receive_time__='1597214851'
出力
__source__: 192.168.0.1 __topic__: app __tag__: stu __receive_time__: 1597214851 id: 7990 test_string: <function test1 at 0x1020401e0>
シナリオ 3:フィールドの削除と名前変更(project-away および project-rename 命令)
project-away 命令と project-rename 命令を使用して、フィールドを削除および名前変更します。
サブシナリオ 1:特定のフィールドを削除します。
* | project-away -wildcard-off <field-pattern>, ...
入力
content:123 age:23 name:twiss
SPL 文
* | project-away age, name
出力
content:123
サブシナリオ 2:特定のフィールドの名前を変更します。
* | project-rename <output>=<field>, ...
入力
content:123 age:23 name:twiss
SPL 文
* | project-rename new_age=age, new_name=name
出力
content:123 new_age:23 new_name:twiss
シナリオ 4:ログパラメータータイプの変換
サブシナリオ 1:concat 関数を呼び出して文字列を連結します。
入力
x: 123 y: 100
SPL 文
* | extend a=cast(x as bigint) + cast(y as bigint)| extend b=concat(x, y)
出力
x: 123 y: 100 a: 223 b: 123100
サブシナリオ 2:文字列または日付時刻値を標準時刻に変換します。
次の例では、to_unixtime 関数を使用して、time1 で表される日付時刻値を UNIX タイムスタンプに変換します。
入力
time1: 2020-09-17 9:00:00
SPL 文
time1 で表される日付時刻値を UNIX タイムスタンプに変換します。
* | extend time1=cast(time1 as TIMESTAMP) | extend new_time=to_unixtime(time1)
出力
time1: 2020-09-17 9:00:00 time2: 1600333200.0
シナリオ 5:ログに存在しないフィールドにデフォルト値を入力する(COALESCE 式)
COALESCE 式を使用して、存在しないフィールドにデフォルト値を入力します。
入力
server_protocol: 100
SPL 文
server_protocol が存在する場合、y に server_protocol の値が割り当てられます。 server_protocol1 が存在しない場合、x に値 200 が割り当てられます。
* | extend x=COALESCE(server_protocol1, '200') | extend y=COALESCE(server_protocol, '200')
出力
server_protocol: 100 x: 200 y: 100
シナリオ 6:ログを評価してフィールドを追加する(where および extend の組み合わせ命令)
例:
入力
status1: 200 status2: 404
SPL 文
* | where status1='200'| extend status1_info='normal' | where status2='404'| extend status2_info='error'
出力
status1: 200 status2: 404 status1_info: normal status2_info: error