查询 Application Load Balancer 日志 - Amazon Athena
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

查询 Application Load Balancer 日志

Application Load Balancer 是 Elastic Load Balancing 的负载均衡选项,它允许使用容器在微服务部署中实现流量分配。通过查询 Application Load Balancer 日志,您可以查看进出 Elastic Load Balancing 实例和后端应用程序的流量来源、延迟和传输字节。有关更多信息,请参阅《User Guide for Application Load Balancers》中的 Access logs for your Application Load BalancerConnection logs for your Application Load Balancer

先决条件

为 ALB 访问日志创建表

  1. 在 Athena 控制台中将以下 CREATE TABLE 语句复制并粘贴到查询编辑器中。有关 Amazon 控制台入门的更多信息,请参阅 开始使用。将 LOCATION 子句中的路径替换为 Amazon S3 访问日志文件夹位置。有关访问日志文件位置的更多信息,请参阅《User Guide for Application Load Balancers》中的 Access log files。有关每个日志文件字段的信息,请参阅 Access log entries

    注意

    以下 CREATE TABLE 语句包含最近添加的 classificationclassification_reasontraceability_id 列。要为不包含这些条目的应用程序负载均衡器访问日志创建表,请从 CREATE TABLE 语句中删除相应的列,并相应地修改正则表达式。

    CREATE EXTERNAL TABLE IF NOT EXISTS alb_access_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, traceability_id 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://DOC-EXAMPLE-BUCKET/access-log-folder-path/'
  2. 在 Athena 控制台中运行查询。查询完成后,Athena 将注册 alb_access_logs 表,使其中的数据可以供您发出查询。

使用分区投影功能在 Athena 中为 ALB 访问日志创建表

由于 ALB 访问日志具有一个可以预先指定其分区方案的已知结构,所以您可以使用 Athena 分区投影功能,以此来减少查询运行时间并自动管理分区。当添加新数据时,分区投影会自动添加新分区。这样就不必使用 ALTER TABLE ADD PARTITION 手动添加分区了。

从指定日期开始到当前日期为止,以下示例 CREATE TABLE 语句会自动在 ALB 访问日志上为单个 Amazon 区域使用分区投影。该语句以上一部分中的示例为基础,但添加了 PARTITIONED BYTBLPROPERTIES 子句以启用分区投影。在 LOCATIONstorage.location.template 子句中,将占位符替换为标识 ALB 访问日志在 Amazon S3 存储桶中位置的值。有关访问日志文件位置的更多信息,请参阅《User Guide for Application Load Balancers》中的 Access log files。对于 projection.day.range,将 2022/01/01 替换为要使用的开始日期。成功运行查询后,您可以查询表。您无需运行 ALTER TABLE ADD PARTITION 来加载分区。有关每个日志文件字段的信息,请参阅 Access log entries

CREATE EXTERNAL TABLE IF NOT EXISTS alb_access_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, traceability_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]*) ([-.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://DOC-EXAMPLE-BUCKET/AWSLogs/<ACCOUNT-NUMBER>/elasticloadbalancing/<REGION>/' 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://DOC-EXAMPLE-BUCKET/AWSLogs/<ACCOUNT-NUMBER>/elasticloadbalancing/<REGION>/${day}" )

更多有关分区投影的信息,请参阅 使用 Amazon Athena 分区投影

ALB 访问日志的示例查询

以下查询计算按客户端 IP 地址分组的负载均衡器接收到的 HTTP GET 请求的数目:

SELECT COUNT(request_verb) AS count, request_verb, client_ip FROM alb_logs GROUP BY request_verb, client_ip LIMIT 100;

另一个查询显示 Safari 浏览器用户访问的 URL:

SELECT request_url FROM alb_logs WHERE user_agent LIKE '%Safari%' LIMIT 10;

以下查询显示了 ELB 状态代码值大于或等于 500 的记录。

SELECT * FROM alb_logs WHERE elb_status_code >= 500

以下示例演示了如何根据 datetime 解析日志:

SELECT client_ip, sum(received_bytes) FROM alb_logs WHERE parse_datetime(time,'yyyy-MM-dd''T''HH:mm:ss.SSSSSS''Z') BETWEEN parse_datetime('2018-05-30-12:00:00','yyyy-MM-dd-HH:mm:ss') AND parse_datetime('2018-05-31-00:00:00','yyyy-MM-dd-HH:mm:ss') GROUP BY client_ip;

以下查询会查询在指定日期内对所有 ALB 日志使用分区投影的表。

SELECT * FROM alb_logs WHERE day = '2022/02/12'

为 ALB 连接日志创建表

  1. 在 Athena 控制台中将以下 CREATE TABLE 语句复制并粘贴到查询编辑器中。有关 Amazon 控制台入门的更多信息,请参阅 开始使用。将 LOCATION 子句中的路径替换为 Amazon S3 连接日志文件夹位置。有关连接日志文件位置的更多信息,请参阅《User Guide for Application Load Balancers》中的 Connection log files。有关每个日志文件字段的信息,请参阅 Connection log entries

    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, traceability_id 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://DOC-EXAMPLE-BUCKET/connection-log-folder-path/'
  2. 在 Athena 控制台中运行查询。查询完成后,Athena 将注册 alb_connection_logs 表,使其中的数据可以供您发出查询。

使用分区投影功能在 Athena 中为 ALB 连接日志创建表

由于 ALB 日志具有一个可以预先指定其分区方案的已知结构,您可以使用 Athena 分区投影功能,以此来减少查询运行时间并自动管理分区。当添加新数据时,分区投影会自动添加新分区。这样就不必使用 ALTER TABLE ADD PARTITION 手动添加分区了。

从指定日期开始到当前日期为止,以下示例 CREATE TABLE 语句会自动在 ALB 连接日志上为单个 Amazon 区域使用分区投影。该语句以上一部分中的示例为基础,但添加了 PARTITIONED BYTBLPROPERTIES 子句以启用分区投影。在 LOCATIONstorage.location.template 子句中,将占位符替换为标识 ALB 连接日志在 Amazon S3 存储桶中位置的值。有关连接日志文件位置的更多信息,请参阅《User Guide for Application Load Balancers》中的 Connection log files。对于 projection.day.range,将 2023/01/01 替换为要使用的开始日期。成功运行查询后,您可以查询表。您无需运行 ALTER TABLE ADD PARTITION 来加载分区。有关每个日志文件字段的信息,请参阅 Connection log entries

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, traceability_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://DOC-EXAMPLE-BUCKET/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://DOC-EXAMPLE-BUCKET/AWSLogs/<ACCOUNT-NUMBER>/elasticloadbalancing/<REGION>/${day}" )

更多有关分区投影的信息,请参阅 使用 Amazon Athena 分区投影

ALB 连接日志的示例查询

以下查询对 tls_verify_status 值不为 'Success' 的情况进行计数,结果按客户端 IP 地址分组:

SELECT DISTINCT client_ip, count() AS count FROM alb_connection_logs WHERE tls_verify_status != 'Success' GROUP BY client_ip ORDER BY count() DESC;

以下查询搜索指定时间范围内 tls_handshake_latency 值超过 2 秒的情况:

SELECT * FROM alb_connection_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);

其他 资源