

# 记录和监控 Athena
<a name="security-logging-monitoring"></a>

要检测事件、在发生事件时接收警报并进行响应，请针对 Amazon Athena 使用以下选项：
+ **使用 AWS CloudTrail 监控 Athena** – [AWS CloudTrail](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/) 提供了 Athena 中的用户、角色或 AWS 服务 的操作记录。它捕获来自 Athena 控制台的调用以及以事件方式对 Athena API 操作的代码调用。您可以确定向 Athena 发出的请求、从中发出请求的 IP 地址、发出请求的用户、发出请求的时间以及其他详细信息。有关更多信息，请参阅 [使用 AWS CloudTrail 记录 Amazon Athena API 调用](monitor-with-cloudtrail.md)。

  您还可以使用 Athena 查询 CloudTrail 日志文件，不仅可用于 Athena，也可用于其他 AWS 服务。有关更多信息，请参阅 [查询 AWS CloudTrail日志](cloudtrail-logs.md)。
+ **通过 CloudTrail 和 Amazon Quick 监控 Athena 使用情况**：[Amazon Quick](https://aws.amazon.com/quicksight/) 是一项完全托管式、基于云的商业智能服务，可让您创建组织可以从任何设备访问的交互式控制面板。有关使用 CloudTrail 和 Amazon Quick 监控 Athena 使用情况的解决方案的示例，请参阅 AWS 大数据博客文章 [How Realtor.com monitors Amazon Athena usage with AWS CloudTrail and Quick](https://aws.amazon.com/blogs/big-data/analyzing-amazon-athena-usage-by-teams-within-a-real-estate-company/)。
+ **将 EventBridge 与 Athena 一起使用** - Amazon EventBridge 提供近乎实时的系统事件流，这些系统事件描述 AWS 资源中的更改。EventBridge 注意到在他们发生时显示的操作更改、响应这些操作更改并在必要时采取纠正措施，方式是发送消息以响应环境、激活函数、进行更改并捕获状态信息。尽最大努力发出事件。有关更多信息，请参阅 *Amazon EventBridge 用户指南*中的 [Amazon EventBridge 入门](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-get-started.html)。
+ **使用工作组分隔用户、团队、应用程序或工作负载，并设置查询限制和控制查询成本** – 您可以在 Amazon CloudWatch 中查看与查询相关的指标、通过配置扫描的数据量限制来控制查询成本、创建阈值以及在突破这些阈值时触发操作，例如 Amazon SNS 告警。有关更多信息，请参阅 [使用工作组控制查询访问和成本](workgroups-manage-queries-control-costs.md)。使用资源级别的 IAM 权限来控制对特定工作组的访问。有关更多信息，请参阅[使用 IAM 策略控制工作组访问](workgroups-iam-policy.md)和[使用 CloudWatch 和 EventBridge 监控查询并控制成本](workgroups-control-limits.md)。

**Topics**
+ [使用 AWS CloudTrail 记录 Amazon Athena API 调用](monitor-with-cloudtrail.md)

# 使用 AWS CloudTrail 记录 Amazon Athena API 调用
<a name="monitor-with-cloudtrail"></a>

Athena 已与 AWS CloudTrail 集成，后者是一项提供 Athena 中由用户、角色或 AWS 服务 所采取操作的记录的服务。

CloudTrail 将 Athena 的API 调用作为事件捕获。捕获的调用包括来自 Athena 控制台的调用和对 Athena API 操作的代码调用。如果您创建跟踪，则可以使 CloudTrail 事件持续传送到 Amazon S3 存储桶（包括 Athena 的事件）。如果您不配置跟踪，则仍可在 CloudTrail 控制台中的**事件历史记录**中查看最新事件。

使用 CloudTrail 收集的信息，您可以确定向 Athena 发出了什么请求、发出请求的 IP 地址、何人发出的请求、请求的发出时间以及其他详细信息。

要了解有关 CloudTrail 的更多信息，请参阅《[AWS CloudTrail 用户指南](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/)》。

您可以使用 Athena 从 Athena 本身和其他 AWS 服务 查询 CloudTrail 日志文件。有关更多信息，请参阅 [查询 AWS CloudTrail日志](cloudtrail-logs.md)、[Hive JSON SerDe](hive-json-serde.md)，以及 AWS 大数据博客文章[将 CTAS 语句与 Amazon Athena 结合使用以降低成本并提高性能](https://aws.amazon.com/blogs/big-data/using-ctas-statements-with-amazon-athena-to-reduce-cost-and-improve-performance/)，其中介绍了使用 CloudTrail 提供对 Athena 使用情况的洞察。

## 关于 CloudTrail 中的 Athena 信息
<a name="athena-info-in-cloudtrail"></a>

在您创建 Amazon Web Services 账户时，将在该账户上启用 CloudTrail。当 Athena 中发生活动时，该活动将记录在 CloudTrail 事件中，并与其他 AWS 服务事件一同保存在 **Event history**（事件历史记录）中。您可以在 Amazon Web Services 账户中查看、搜索和下载最新事件。有关更多信息，请参阅[使用 CloudTrail 事件历史记录查看事件](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/view-cloudtrail-events.html)。

要持续记录 Amazon Web Services 账户中的事件（包括 Athena 的事件），请创建跟踪记录。通过*跟踪*，CloudTrail 可将日志文件传送至 Amazon S3 存储桶。默认情况下，在控制台中创建跟踪记录时，此跟踪记录应用于所有 AWS 区域。此跟踪记录在 AWS 分区中记录所有区域中的事件，并将日志文件传送至您指定的 Amazon S3 存储桶。此外，您可以配置其他 AWS 服务，进一步分析在 CloudTrail 日志中收集的事件数据并采取行动。有关更多信息，请参阅下列内容：
+ [创建跟踪记录概述](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-create-and-update-a-trail.html)
+ [CloudTrail 支持的服务和集成](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-aws-service-specific-topics.html#cloudtrail-aws-service-specific-topics-integrations)
+ [为 CloudTrail 配置 Amazon SNS 通知](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/getting_notifications_top_level.html)
+ [从多个区域接收 CloudTrail 日志文件](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/receive-cloudtrail-log-files-from-multiple-regions.html)和[从多个账户接收 CloudTrail 日志文件](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-receive-logs-from-multiple-accounts.html)

所有 Athena 操作均由 CloudTrail 记录下来并记载到 [Amazon Athena API 参考](https://docs.aws.amazon.com/athena/latest/APIReference/)中。例如，对 [StartQueryExecution](https://docs.aws.amazon.com/athena/latest/APIReference/API_StartQueryExecution.html) 和 [GetQueryResults](https://docs.aws.amazon.com/athena/latest/APIReference/API_StartQueryExecution.html) 操作的调用会在 CloudTrail 日志文件中生成条目。

每个事件或日志条目都包含有关生成请求的人员信息。身份信息可帮助您确定以下内容：
+ 请求是使用根用户凭证还是 AWS Identity and Access Management（IAM）用户凭证发出的。
+ 请求是使用角色还是联合用户的临时安全凭证发出的。
+ 请求是否由其他 AWS 服务 发出。

有关更多信息，请参阅 [CloudTrail userIdentity 元素](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-event-reference-user-identity.html)。

## 了解 Athena 日志文件条目
<a name="understanding-ate-log-file-entries"></a>

跟踪记录是一种配置，可用于将事件作为日志文件传送到您指定的 Simple Storage Service（Amazon S3）存储桶。CloudTrail 日志文件包含一个或多个记录条目。一个事件表示来自任何源的一个请求，包括有关所请求的操作、操作的日期和时间、请求参数等方面的信息。CloudTrail 日志文件不是公有 API 调用的有序堆栈跟踪，因此它们不会按任何特定顺序显示。

**注意**  
为防止意外泄露敏感信息，`StartQueryExecution` 和 `CreateNamedQuery` 日志中的 `queryString` 条目的值均为 `***OMITTED***`。这是设计使然。要访问实际查询字符串，可以使用 Athena [GetQueryExecution](https://docs.aws.amazon.com/athena/latest/APIReference/API_GetQueryExecution.html) API 并传入 CloudTrail 日志中的 `responseElements.queryExecutionId` 值。

以下示例展示了 CloudTrail 日志条目：
+  [StartQueryExecution (成功)](#startqueryexecution-successful) 
+  [StartQueryExecution (失败)](#startqueryexecution-failed) 
+  [CreateNamedQuery](#createnamedquery) 

### StartQueryExecution（成功）
<a name="startqueryexecution-successful"></a>

```
{
 "eventVersion":"1.05",
 "userIdentity":{
    "type":"IAMUser",
    "principalId":"EXAMPLE_PRINCIPAL_ID",
    "arn":"arn:aws:iam::123456789012:user/johndoe",
    "accountId":"123456789012",
    "accessKeyId":"EXAMPLE_KEY_ID",
    "userName":"johndoe"
 },
 "eventTime":"2017-05-04T00:23:55Z",
 "eventSource":"athena.amazonaws.com",
 "eventName":"StartQueryExecution",
 "awsRegion":"us-east-1",
 "sourceIPAddress":"77.88.999.69",
 "userAgent":"aws-internal/3",
 "requestParameters":{
    "clientRequestToken":"16bc6e70-f972-4260-b18a-db1b623cb35c",
    "resultConfiguration":{
       "outputLocation":"s3://amzn-s3-demo-bucket/test/"
    },
    "queryString":"***OMITTED***"
 },
 "responseElements":{
    "queryExecutionId":"b621c254-74e0-48e3-9630-78ed857782f9"
 },
 "requestID":"f5039b01-305f-11e7-b146-c3fc56a7dc7a",
 "eventID":"c97cf8c8-6112-467a-8777-53bb38f83fd5",
 "eventType":"AwsApiCall",
 "recipientAccountId":"123456789012"
}
```

### StartQueryExecution（失败）
<a name="startqueryexecution-failed"></a>

```
{
 "eventVersion":"1.05",
 "userIdentity":{
  "type":"IAMUser",
  "principalId":"EXAMPLE_PRINCIPAL_ID",
  "arn":"arn:aws:iam::123456789012:user/johndoe",
  "accountId":"123456789012",
  "accessKeyId":"EXAMPLE_KEY_ID",
  "userName":"johndoe"
  },
 "eventTime":"2017-05-04T00:21:57Z",
 "eventSource":"athena.amazonaws.com",
 "eventName":"StartQueryExecution",
 "awsRegion":"us-east-1",
 "sourceIPAddress":"77.88.999.69",
 "userAgent":"aws-internal/3",
 "errorCode":"InvalidRequestException",
 "errorMessage":"Invalid result configuration. Should specify either output location or result configuration",
 "requestParameters":{
  "clientRequestToken":"ca0e965f-d6d8-4277-8257-814a57f57446",
  "queryString":"***OMITTED***"
  },
 "responseElements":null,
 "requestID":"aefbc057-305f-11e7-9f39-bbc56d5d161e",
 "eventID":"6e1fc69b-d076-477e-8dec-024ee51488c4",
 "eventType":"AwsApiCall",
 "recipientAccountId":"123456789012"
}
```

### CreateNamedQuery
<a name="createnamedquery"></a>

```
{
  "eventVersion":"1.05",
  "userIdentity":{
     "type":"IAMUser",
     "principalId":"EXAMPLE_PRINCIPAL_ID",
     "arn":"arn:aws:iam::123456789012:user/johndoe",
     "accountId":"123456789012",
     "accessKeyId":"EXAMPLE_KEY_ID",
     "userName":"johndoe"
  },
  "eventTime":"2017-05-16T22:00:58Z",
  "eventSource":"athena.amazonaws.com",
  "eventName":"CreateNamedQuery",
  "awsRegion":"us-west-2",
  "sourceIPAddress":"77.88.999.69",
  "userAgent":"aws-cli/1.11.85 Python/2.7.10 Darwin/16.6.0 botocore/1.5.48",
  "requestParameters":{
     "name":"johndoetest",
     "queryString":"***OMITTED***",
     "database":"default",
     "clientRequestToken":"fc1ad880-69ee-4df0-bb0f-1770d9a539b1"
     },
  "responseElements":{
     "namedQueryId":"cdd0fe29-4787-4263-9188-a9c8db29f2d6"
     },
  "requestID":"2487dd96-3a83-11e7-8f67-c9de5ac76512",
  "eventID":"15e3d3b5-6c3b-4c7c-bc0b-36a8dd95227b",
  "eventType":"AwsApiCall",
  "recipientAccountId":"123456789012"
},
```