Search and Process Language (SPL) 命令と SQL 関数を使用して、大量のログデータをフィルタリングおよびクリーニングし、データ形式を標準化できます。このトピックでは、データのフィルタリングとクリーニングの一般的なシナリオと関連する操作について説明します。
シナリオ 1:ログのフィルタリング(where 命令)
where 命令を使用してログをフィルタリングできます。一般的なルールは次のとおりです。
where <bool-expression>以下に例を示します。
サブシナリオ 1: フィールドの内容に基づいてデータをフィルタリングする。
生のログ
#ログ 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> #ログ 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: 正規表現を使用してフィールド名に一致するデータをフィルタリングする。
生のログ
#ログ 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 #ログ 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: 14861SPL 文
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:twissSPL 文
* | project-away age, name出力
content:123
サブシナリオ 2:特定のフィールドの名前を変更します。
| project-rename <output>=<field>, ...入力データ
content:123 age:23 name:twissSPL 文
* | project-rename new_age=age, new_name=name出力
content:123 new_age:23 new_name:twiss
シナリオ 4:ログパラメータータイプの変換
サブシナリオ 1: concat 関数を使用して文字列を連結する。
入力データ
x: 123 y: 100SPL 文
* | extend a=cast(x as bigint) + cast(y as bigint)| extend b=concat(x, y)出力
x: 123 y: 100 a: 223 b: 123100
サブシナリオ 2: 文字列または datetime 値を標準の時間形式に変換する。次の例は、to_unixtime 関数を使用して time1 フィールドの datetime 値を UNIX タイムスタンプに変換する方法を示しています。
生のログ
time1: 2020-09-17 9:00:00変換ルール
time1 フィールドの datetime 値を 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: 100SPL 文
server_protocol フィールドが存在する場合、その値が y に割り当てられます。server_protocol1 フィールドが存在しない場合、200 が x に割り当てられます。
* | 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: 404SPL 文
* | where status1='200'| extend status1_info='normal' | where status2='404'| extend status2_info='error'出力
status1: 200 status2: 404 status1_info: normal status2_info: error