如何使用 Amazon Athena 分析我的 Application Load Balancer 存取日誌?

4 分的閱讀內容
0

我想使用 Amazon Athena 分析我的 Application Load Balancer 存取日誌。

我想使用 Amazon Athena 分析我的 Application Load Balancer 存取日誌。

簡短描述

預設情況下,Elastic Load Balancing (ELB) 不會啟用存取紀錄。當您啟用存取紀錄時,請指定一個 Amazon Simple Storage Service (Amazon S3) 儲存貯體。所有 Application Load Balancer 和 Classic Load Balancer 存取日誌都儲存在 Amazon S3 儲存貯體中。若要進行疑難排解或分析負載平衡器的效能,請使用 Athena 分析 Amazon S3 中的存取日誌。

**注意:**雖然您可以使用 Athena 分析 Application Load Balancer 和 Classic Load Balancer 的存取日誌,但本解決方法僅適用於 Application Load Balancer

解決方法

為 Application Load Balancer 日誌建立資料庫和資料表

若要分析 Athena 中的存取日誌,請依照下列步驟建立資料庫和資料表:

1.    開啟 Athena 主控台

2.    在 Query Editor(查詢編輯器) 中,執行下列命令以 create a database, (建立資料庫)。最佳做法是在與 S3 儲存貯體相同的 AWS 區域中建立資料庫。

create database alb_db

3.    在步驟二建立的資料庫中,為 Application Load Balancer 日誌建立 alb_logs 資料表。如需詳細資訊,請參閱為 Application Load Balancer 日誌建立資料表

**注意:**若要實現更好的查詢效能,您可以建立具備分區投影的資料表。在分區投影中,Athena 會根據組態計算分區值和位置,而不是從儲存庫 (例如 AWS Glue Data Catalog) 讀取。如需詳細資訊,請參閱使用 Amazon Athena 進行分區投影

CREATE EXTERNAL TABLE IF NOT EXISTS alb_logs (
            type string,
            time string,
            elb string,
            client_ip string,
            client_port int,
            target_ip string,
            target_port int,
            request_processing_time double,
            target_processing_time double,
            response_processing_time double,
            elb_status_code int,
            target_status_code string,
            received_bytes bigint,
            sent_bytes bigint,
            request_verb string,
            request_url string,
            request_proto string,
            user_agent string,
            ssl_cipher string,
            ssl_protocol string,
            target_group_arn string,
            trace_id string,
            domain_name string,
            chosen_cert_arn string,
            matched_rule_priority string,
            request_creation_time string,
            actions_executed string,
            redirect_url string,
            lambda_error_reason string,
            target_port_list string,
            target_status_code_list string,
            classification string,
            classification_reason string
            )
            PARTITIONED BY
            (
             day STRING
            )
            ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.RegexSerDe'
            WITH SERDEPROPERTIES (
            'serialization.format' = '1',
            'input.regex' =
            '([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*):([0-9]*) ([^ ]*)[:-]([0-9]*) ([-.0-9]*) ([-.0-9]*) ([-.0-9]*) (|[-0-9]*) (-|[-0-9]*) ([-0-9]*) ([-0-9]*) \"([^ ]*) (.*) (- |[^ ]*)\" \"([^\"]*)\" ([A-Z0-9-_]+) ([A-Za-z0-9.-]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^\"]*)\" ([-.0-9]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^ ]*)\" \"([^\s]+?)\" \"([^\s]+)\" \"([^ ]*)\" \"([^ ]*)\"')
            LOCATION 's3://your-alb-logs-directory/AWSLogs/1111222233334444/elasticloadbalancing//'
            TBLPROPERTIES
            (
             "projection.enabled" = "true",
             "projection.day.type" = "date",
             "projection.day.range" = "2022/01/01,NOW",
             "projection.day.format" = "yyyy/MM/dd",
             "projection.day.interval" = "1",
             "projection.day.interval.unit" = "DAYS",
             "storage.location.template" = "s3://your-alb-logs-directory/AWSLogs/1111222233334444/elasticloadbalancing//${day}"
            )

**注意:**請根據您的使用案例替換資料表名稱和 S3 位置。

或者,使用下列查詢來建立具備分區的資料表:

CREATE EXTERNAL TABLE IF NOT EXISTS alb_logs_partitioned (
            type string,
            time string,
            elb string,
            client_ip string,
            client_port int,
            target_ip string,
            target_port int,
            request_processing_time double,
            target_processing_time double,
            response_processing_time double,
            elb_status_code string,
            target_status_code string,
            received_bytes bigint,
            sent_bytes bigint,
            request_verb string,
            request_url string,
            request_proto string,
            user_agent string,
            ssl_cipher string,
            ssl_protocol string,
            target_group_arn string,
            trace_id string,
            domain_name string,
            chosen_cert_arn string,
            matched_rule_priority string,
            request_creation_time string,
            actions_executed string,
            redirect_url string,
            lambda_error_reason string,
            target_port_list string,
            target_status_code_list string,
            classification string,
            classification_reason string
            )
            PARTITIONED BY(day string)
            ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.RegexSerDe'
            WITH SERDEPROPERTIES (
            'serialization.format' = '1',
            'input.regex' =
            '([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*):([0-9]*) ([^ ]*)[:-]([0-9]*) ([-.0-9]*) ([-.0-9]*) ([-.0-9]*) (|[-0-9]*) (-|[-0-9]*) ([-0-9]*) ([-0-9]*) \"([^ ]*) ([^ ]*) (- |[^ ]*)\" \"([^\"]*)\" ([A-Z0-9-]+) ([A-Za-z0-9.-]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^\"]*)\" ([-.0-9]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^ ]*)\" \"([^\s]+?)\" \"([^\s]+)\" \"([^ ]*)\" \"([^ ]*)\"')
            LOCATION 's3://my_log_bucket/AWSLogs/1111222233334444/elasticloadbalancing/us-east-1/'

然後,使用 ALTER TABLE ADD PARTITION 命令加載分區:

ALTER TABLE alb_logs_partitioned ADD
  PARTITION (day = '2022/05/21')
  LOCATION's3://my_log_bucket/AWSLogs/1111222233334444/elasticloadbalancing/us-east-1/2022/05/21/'

**注意:**在 Application Load Balancer 日誌中使用 AWS Glue 爬蟲程式並不是最佳做法。

4.    在導覽窗格的 表格 下,從功能表中選擇 Preview table (預覽資料表)。您可以在 Results (結果) 視窗檢視 Application Load Balancer 存取日誌中的資料。

5.    使用 Query editor (查詢編輯器) 在資料表上執行 SQL 陳述式。您可以儲存查詢、檢視先前的查詢或下載 .csv 檔案格式的查詢結果。

查詢範例

在下列範例中,請務必修改資料表名稱、資料欄值和其他變數以符合您的查詢:

依時間順序查看前 100 個存取日誌項目

使用此查詢進行分析和疑難排解:

SELECT *  
FROM alb_logs  
ORDER by time ASC  
LIMIT 100

列出所有已存取 Application Load Balancer 的用戶端 IP 地址,以及其存取 Application Load Balancer 的次數

使用此查詢進行分析和疑難排解:

SELECT distinct client_ip, count() as count from alb_logs  
GROUP by client_ip  
ORDER by count() DESC;

列出請求或回應對中透過 Application Load Balancer 傳遞的平均資料量 (以 KB 為單位)

使用此查詢進行分析和疑難排解:

SELECT (avg(sent_bytes)/1000.0 + avg(received_bytes)/1000.0)  
as prewarm_kilobytes from alb_logs;

列出 Application Load Balancer 路由流量的所有目標,以及依百分比分佈將請求路由至每個目標的次數

使用此查詢來識別潛在的目標流量不平衡:

SELECT target_ip, (Count(target_ip)* 100.0 / (Select Count(*) From alb_logs))  
as backend_traffic_percentage  
FROM alb_logs  
GROUP by target_ip<  
ORDER By count() DESC;

列出用戶端傳送請求至 Application Load Balancer ,然後在閒置逾時耗用時間前關閉連線的次數 (HTTP 460 錯誤)

使用此查詢對 HTTP 460 錯誤進行疑難排解:

SELECT * from alb_logs where elb_status_code = '460';

列出因接聽程式規則將請求轉發至空的目標群組而無法路由用戶端請求的時間 (HTTP 503 錯誤)

使用此查詢對 HTTP 503 錯誤進行疑難排解:

SELECT * from alb_logs where elb_status_code = '503';

根據每個用戶端造訪指定 URL 的次數,以遞減順序列出用戶端

使用此查詢分析流量模式:

SELECT client_ip, elb, request_url, count(*) as count from alb_logs  
GROUP by client_ip, elb, request_url  
ORDER by count DESC;

以遞減順序列出 Firefox 使用者最常存取的 10 個網址

使用此查詢分析流量分佈和模式:

SELECT request_url, user_agent, count(*) as count  
FROM alb_logs  
WHERE user_agent LIKE '%Firefox%'  
GROUP by request_url, user_agent  
ORDER by count(*) DESC  
LIMIT 10;

根據每個用戶端傳送至 Application Load Balancer 的請求中包含的資料量 (MB),以遞減順序列出用戶端

使用此查詢分析流量分佈和模式:

SELECT client_ip, sum(received_bytes/1000000.0) as client_datareceived_megabytes  
FROM alb_logs  
GROUP by client_ip  
ORDER by client_datareceived_megabytes DESC;

列出在指定日期範圍內每次目標處理時間超過 5 秒的時間

使用此查詢對指定時間範圍內的延遲進行疑難排解:

SELECT * from alb_logs  
WHERE (parse_datetime(time,'yyyy-MM-dd''T''HH:mm:ss.SSSSSS''Z')  
    BETWEEN parse_datetime('2018-08-08-00:00:00','yyyy-MM-dd-HH:mm:ss')  
    AND parse_datetime('2018-08-08-02:00:00','yyyy-MM-dd-HH:mm:ss'))  
AND (target_processing_time >= 5.0);

計算負載平衡器 (依用戶端 IP 地址分組) 接收的 HTTP GET 請求數量

使用此查詢分析傳入流量分佈:

SELECT COUNT(request_verb)   
AS count, request_verb, client_ip   
FROM alb_logs_partitioned   
WHERE day = '2022/05/21'   
GROUP by request_verb, client_ip;
AWS 官方
AWS 官方已更新 1 年前