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

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

查询经典负载均衡器日志

使用经典负载均衡器日志,分析和了解传入和传出 Elastic Load Balancing 实例和后端应用程序的流量模式。您可以查看流量来源、延迟和已传输的字节。

在分析 Elastic Load Balancing 日志之前,请对其进行配置以保存在目标 Amazon S3 存储桶中。有关更多信息,请参阅为经典负载均衡器启用访问日志

为 Elastic Load Balancing 日志创建表

  1. 将以下 DDL 语句复制并粘贴到 Athena 控制台中。检查 Elastic Load Balancing 日志的语法。您可能需要更新以下查询以包含列和最新版本的记录的 Regex 语法。

    CREATE EXTERNAL TABLE IF NOT EXISTS elb_logs ( timestamp string, elb_name string, request_ip string, request_port int, backend_ip string, backend_port int, request_processing_time double, backend_processing_time double, client_response_time double, elb_response_code string, backend_response_code string, received_bytes bigint, sent_bytes bigint, request_verb string, url string, protocol string, user_agent string, ssl_cipher string, ssl_protocol 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.-]*)$' ) LOCATION 's3://your_log_bucket/prefix/AWSLogs/AWS_account_ID/elasticloadbalancing/';
  2. 修改 LOCATION Amazon S3 存储桶以指定您的 Elastic Load Balancing 日志的目标。

  3. 在 Athena 控制台中运行查询。查询完成后,Athena 将注册 elb_logs 表,使其中的数据可以供查询。有关更多信息,请参阅Elastic Load Balancing 示例查询

Elastic Load Balancing 示例查询

使用类似于以下示例的查询。它列出返回 4XX5XX 错误响应代码的后端应用程序服务器。使用 LIMIT 运算符可限制每次查询的日志数目。

SELECT timestamp, elb_name, backend_ip, backend_response_code FROM elb_logs WHERE backend_response_code LIKE '4%' OR backend_response_code LIKE '5%' LIMIT 100;

使用后续查询对按后端 IP 地址和 Elastic Load Balancing 实例名称分组的所有事务的响应时间进行求和。

SELECT sum(backend_processing_time) AS total_ms, elb_name, backend_ip FROM elb_logs WHERE backend_ip <> '' GROUP BY backend_ip, elb_name LIMIT 100;

有关更多信息,请参阅使用 Athena 分析 S3 中的数据