跳至內容

如何使用 Athena 查詢 Application Load Balancer 連線日誌?

2 分的閱讀內容
0

我想使用 Amazon Athena 查詢 Application Load Balancer 連線日誌。

解決方法

若要查詢Application Load Balancer 連線日誌,請為 Athena 建立資料庫和資料表。請完成下列步驟:

  1. 開啟 Athena console (Athena 主控台)。

  2. 若要建立資料庫,請在查詢編輯器中執行下列命令:

    CREATE DATABASE alb_db

    **注意:**最佳實務是在與您的 Amazon Simple Storage Service (Amazon S3) 儲存貯體相同的 AWS 區域中建立資料庫。

  3. 建立資料庫後,請使用下列查詢建立資料表:

    CREATE EXTERNAL TABLE IF NOT EXISTS alb_connection_logs (  
             time string,  
             client_ip string,  
             client_port int,  
             listener_port int,  
             tls_protocol string,  
             tls_cipher string,  
             tls_handshake_latency double,  
             leaf_client_cert_subject string,  
             leaf_client_cert_validity string,  
             leaf_client_cert_serial_number string,  
             tls_verify_status string,  
             conn_trace_id 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]*) ([A-Za-z0-9.-]*) ([^ ]*) ([-.0-9]*) \"([^\"]*)\" ([^ ]*) ([^ ]*) ([^ ]*) ?([^ ]*)?( .*)?'  
                )  
                LOCATION 's3://S3-LOCATION/AWSLogs/ACCOUNT-NUMBER/elasticloadbalancing/REGION/'  
                TBLPROPERTIES  
                (  
                 "projection.enabled" = "true",  
                 "projection.day.type" = "date",  
                 "projection.day.range" = "2023/01/01,NOW",  
                 "projection.day.format" = "yyyy/MM/dd",  
                 "projection.day.interval" = "1",  
                 "projection.day.interval.unit" = "DAYS",  
                 "storage.location.template" = "s3://S3-LOCATION/AWSLogs/ACCOUNT-NUMBER/elasticloadbalancing/REGION/${day}"  
                )

    **注意:**將 S3-LOCATIONACCOUNT-NUMBERREGION 替換為您的 S3 儲存貯體值。

  4. 若要查詢 Application Load Balancer 連線日誌,請使用 SQL 陳述式。

範例查詢

查看最近 100 比日誌

SELECT *FROM alb_conn_logs  
ORDER by time desc  
LIMIT 100

統計 tls_verify_status 不為「成功」的次數,並依 client_IP 分組

SELECT distinct client_ip, count() as count from alb_conn_logs WHERE tls_verify_status != 'Success'  
GROUP by client_ip  
ORDER by count() DESC;

查看特定時間範圍內,所有 tls_handshake_latency 超過兩秒的情況

SELECT * FROM alb_conn_logs WHERE (parse_datetime(time,'yyyy-MM-dd''T''HH:mm:ss.SSSSSS''Z')  
BETWEEN parse_datetime('2024-01-01-00:00:00','yyyy-MM-dd-HH:mm:ss')  
AND parse_datetime('2024-03-20-00:00:00','yyyy-MM-dd-HH:mm:ss'))  
AND (tls_handshake_latency >= 2.0);

檢查具有確切憑證有效期的連線,並依leaf_client_cert_serial_number 分組

SELECT DISTINCT leaf_client_cert_serial_number, count() AS count FROM alb_conn_logs  
WHERE tls_verify_status = 'Success' AND leaf_client_cert_validity like '%NotAfter=2025-01-01%'  
GROUP BY leaf_client_cert_serial_number  
ORDER BY count() DESC;

相關資訊

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

查詢 Application Load Balancer 日誌

AWS 官方已更新 1 年前