2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ALBログ分析

Athenaだけでやるとこのような手順になる。

Glueを用いてやれば、テーブルを作る部分が楽にできた。

このAWS公式の記事を元に、Grok custom classifierを作成する方針で進め、1時間くらいでできた。Glueすごい。

手順

1. Glue Crawler でスキーマ生成 with Classifier

1-1. Classifierを作る。

ALB ログは自由テキストなので Grok カスタム classifier を 1 つ用意すると精度が高い

らしい。

ので作る。

Grok pattern はこのようなものにした。

%{DATA:type}\s+%{TIMESTAMP_ISO8601:time}\s+%{DATA:elb}\s+%{DATA:client}\s+%{DATA:target}\s+%{BASE10NUM:req_proc_time}\s+%{DATA:target_proc_time}\s+%{BASE10NUM:resp_proc_time}\s+%{BASE10NUM:elb_status:int}\s+%{DATA:target_status}\s+%{BASE10NUM:recv_bytes:int}\s+%{BASE10NUM:sent_bytes:int}\s+"%{DATA:request}"\s+"%{DATA:user_agent}"\s+%{DATA:ssl_cipher}\s+%{DATA:ssl_proto}\s+%{DATA:target_group_arn}\s+"%{DATA:trace_id}"\s+"%{DATA:domain_name}"\s+"%{DATA:chosen_cert_arn}"\s+%{DATA:rule_priority}\s+%{TIMESTAMP_ISO8601:req_create_time}\s+"%{DATA:actions}"\s+"%{DATA:redirect_url}"\s+"%{DATA:error_reason}"\s+"%{DATA:target_list}"\s+"%{DATA:target_status_list}"\s+"%{DATA:classification}"\s+"%{DATA:classification_reason}"

1-2. Glue Crawler を作成

① Glue 左ペイン [Crawlers] → [Add crawler]
② Crawler name : alb-access-log-crawler

スクリーンショット 2025-05-26 12.13.55.png

③ Choose a data source
Data store: S3
Crawl data in: Specified path in my account
Include path:
s3:///AWSLogs//elasticloadbalancing//
Repeat crawls: Crawl new folders only(ログ日付ディレクトリ追加だけ検知)
Custom Classifiers 手順1-1で作成したcustom classifier を指定

スクリーンショット 2025-05-26 12.15.02.png

スクリーンショット 2025-05-26 12.15.52.png

スクリーンショット 2025-05-26 12.16.08.png

④ Choose a Cross-account → No(ログは一つのアカウントのS3に集約されている場合を前提。)
⑤ Add another data store? → No
⑥ IAM role で「AWSGlueServiceRole-ALBLogs」を選択

スクリーンショット 2025-05-26 12.17.10.png

スクリーンショット 2025-05-26 12.17.23.png

スクリーンショット 2025-05-26 12.17.32.png

⑦ Specify the output
データベース: alb_logs_db
Table prefix: alb_(任意)
⑧ Schedule
On demand でも良いが、Daily at 02:00 JST など設定すると自動取り込み可
⑨ Review and create → [Finish]

スクリーンショット 2025-05-26 12.17.38.png

スクリーンショット 2025-05-26 12.17.51.png

スクリーンショット 2025-05-26 12.19.10.png

1-3. Crawler を実行 & 結果確認

① 作成直後の alb-access-log-crawler を選び [Run crawler]

② Databases > alb_logs_db を開くと
alb_access_logs テーブルが生成
Columns: type, time, elb, client_ip … user_agent など
Partitions: year string, month string, day string が自動追加

スクリーンショット 2025-05-26 12.19.31.png

スクリーンショット 2025-05-26 12.28.51.png

2. できたテーブルにquery editorからSQLを流す。

ブラウザでFirefoxの利用率を測定するクエリ。

SELECT
  mozilla_hits,
  total_hits,
  ROUND(100.0 * mozilla_hits / total_hits, 2) AS pct_mozilla
FROM (
  SELECT
    COUNT_IF(regexp_like(user_agent, 'Mozilla')) AS mozilla_hits,
    COUNT(*)                                     AS total_hits
  FROM "xxxx_y_alb_logs_db"."test_for_firefox_7074"
) t;

できた。

スクリーンショット 2025-05-26 13.33.20.png

以上。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?