

AWS Application Discovery Service 不再向新客户开放。或者 AWS Transform ，使用提供类似功能的。有关更多信息，请参阅 App [AWS lication Discovery Service 可用性变更](https://docs.aws.amazon.com/application-discovery/latest/userguide/application-discovery-service-availability-change.html)。

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

# 探索亚马逊 Athena 中的数据
<a name="explore-data"></a>

Amazon Athena 中的数据探索允许您在一个地方分析 Discovery Agent 从发现的所有本地服务器中收集的数据。从 Migration Hub 控制台（或使用 StartContinousExport API）启用 Amazon Athena 中的数据探索并开启代理数据收集功能后，代理收集的数据将定期自动存储在您的 S3 存储桶中。有关更多信息，请参阅 [探索亚马逊 Athena 中的数据](#explore-data)。

Amazon Athena 中的数据探索允许您在一个地方分析发现代理从发现的所有本地服务器收集的数据。从 Migration Hub 控制台（或使用 StartContinousExport API）启用 Amazon Athena 中的数据探索并开启代理数据收集功能后，代理收集的数据将定期自动存储在您的 S3 存储桶中。

然后，您可以访问 Amazon Athena 运行预定义的查询，以分析每台服务器的时间序列系统性能、每台服务器上运行的进程类型以及不同服务器之间的网络依赖关系。此外，您还可以使用 Amazon Athena 编写自己的自定义查询，上传其他现有数据源，例如配置管理数据库 (CMDB) 导出，并将发现的服务器与实际业务应用程序关联起来。您还可以将 Athena 数据库与 Amazon Quick 集成，以可视化查询输出并执行其他分析。

本节中的主题描述了在 Athena 中使用数据来评估和规划将本地环境迁移到的方式。 AWS

# 在 Amazon Athena 中开启数据探索
<a name="ce-prep-agents"></a>

使用 Migration Hub 控制台或从 API 调用开启持续导出，即可在 Amazon Athena 中进行数据探索。 AWS CLI您必须先开启数据探索功能，然后才能在 Amazon Athena 中查看并开始探索已发现的数据。

开启 “连续导出” 后，您的账户将自动使用服务相关角色`AWSServiceRoleForApplicationDiscoveryServiceContinuousExport`。有关此服务相关角色的更多信息，请参阅[Application Discovery Service 的服务相关角色权限](service-linked-role-permissions.md)。

以下说明说明如何使用控制台和 Amazon Athena 开启数据探索功能。 AWS CLI

------
#### [ Turn on with the console ]

在 Migration Hub 控制台的 “数据收集**器**” 页面上选择 “开始数据收集”，或者在 Migration Hub 控制台的数据收集器页面上单击 “在 Amazon Athena 中进行数据探索” 时，隐式开启持续导出，即可启用 Amazon Athena 中的数据探索。

**通过控制台在 Amazon Athena 中开启数据探索**

1. 在导航窗格中，选择 **Data Collectors (数据收集器)**。

1. 选择 **Agents (代理)** 选项卡。

1. 选择 “**开始数据收集**”，或者如果您已经开启了数据收集，请单击 “**Amazon Athena 中的数据探索**” 开关。

1. 在上一步生成的对话框中，单击同意关联成本的复选框并选择 **Continue (继续)** 或 **Enable (启用)**。

**注意**  
您的代理现在以 “持续导出” 模式运行，这将使您能够在 Amazon Athena 中查看和处理发现的数据。首次启用此功能时，您的数据最多可能需要 30 分钟才能显示在 Amazon Athena 中。

------
#### [ Enable with the AWS CLI ]

Amazon Athena 中的数据探索功能是通过来自 API 调用明确开启的 “持续导出”。 AWS CLI为此， AWS CLI 必须先在您的环境中安装。

**要在 Amazon Athena 中安装 AWS CLI 并开启数据探索**

1. 安装 AWS CLI 适用于您的操作系统（Linux、macOS 或 Windows）的。有关说明，请参阅[《AWS Command Line Interface 用户指南》](https://docs.aws.amazon.com/cli/latest/userguide/)。

1. 打开命令提示符 (Windows) 或终端 (Linux 或 macOS)。

   1. 键入 `aws configure` 并按下 Enter。

   1. 输入您的 AWS 访问密钥 ID 和 AWS 私有访问密钥。

   1. 对于默认区域名称，输入 `us-west-2`。

   1. 对于默认输出格式，输入 `text`。

1. 键入以下命令：

   ```
   aws discovery start-continuous-export
   ```

**注意**  
您的代理现在以 “持续导出” 模式运行，这将使您能够在 Amazon Athena 中查看和处理发现的数据。首次启用此功能时，您的数据最多可能需要 30 分钟才能显示在 Amazon Athena 中。

------

# 直接在 Amazon Athena 中浏览数据
<a name="explore-direct-in-ate"></a>

在 Amazon Athena 中开启数据探索后，您可以直接在 Athena 中查询数据，开始探索和处理代理发现的详细当前数据。您可以使用这些数据生成电子表格，运行成本分析，将查询移植到可视化程序以便以示意图方式呈现网络依赖关系等等。

以下说明说明了如何直接在 Athena 控制台中浏览代理数据。如果您在 Athena 中没有任何数据，或者尚未在 Amazon Athena 中启用数据探索，则系统会显示一个对话框提示您在 Amazon Athena 中启用数据探索，如中所述。[在 Amazon Athena 中开启数据探索开启数据探索](ce-prep-agents.md)

**直接在 Athena 中浏览代理发现的数据**

1. 在 AWS Migration Hub 控制台中，选择导航窗格中的**服务器**。

1. 要打开亚马逊 Athena 控制台，**请选择 “在亚马逊 Athena 中浏览数据”**。

1. 在 **Query Editor (查询编辑器)** 页面上，在 **Database (数据库)** 下的导航窗格中，确保选择了 **application\$1discovery\$1service\$1database**。
**注意**  
在 **Tables (表)** 下，以下各表表示按代理分组的数据集。  
**os\$1info\$1agent**
**network\$1interface\$1agent**
**sys\$1performance\$1agent**
**processes\$1agent**
**inbound\$1connection\$1agent**
**outbound\$1connection\$1agent**
**id\$1mapping\$1agent**

1. 通过在 Athena 查询编辑器中编写和运行 SQL 查询，在 Amazon Athena 控制台中查询数据。例如，您可以使用以下查询来查看发现的所有服务器 IP 地址。

   ```
   SELECT * FROM network_interface_agent;
   ```

   有关更多示例查询，请参阅[在 Amazon Athena 中使用预定义查询使用预定义的查询](predefined-queries.md)。

# 可视化亚马逊 Athena 数据
<a name="port-query-to-visualization"></a>

为了可视化您的数据，可以将查询移植到可视化程序（例如Amazon Quick）或其他开源可视化工具，例如Cytoscape、yEd或Gelphi。使用这些工具来渲染网络图、摘要图表和其他图形表示。使用此方法时，您可以通过可视化程序连接到 Athena，这样它就可以访问您收集的数据作为生成可视化的来源。

**使用 Quick 可视化您的 Amazon Athena 数据**

1. [快速登录 Amazon](https://aws.amazon.com/quicksight/)。

1. 选择 **Connect to another data source or upload a file (连接到其他数据源或上传文件)**。

1. 选择 **Athena。**将显示 “**新建 Athena 数据**源” 对话框。

1. 在 **Data source name (数据源名称)** 字段中输入名称。

1. 选择**创建数据来源**。

1. 在 “选择**您的gents-servers-os表” 对话框中选择 **A** 表**，**然后选择 “选择”**。

1. 在 **Finish dataset creation (完成数据集创建)** 对话框中，依次选择 **Import to SPICE for quicker analytics (导入到 SPICE 以进行快速分析)** 和 **Visualize (可视化)**。

   将呈现您的可视化结果。

# 在 Amazon Athena 中使用预定义查询
<a name="predefined-queries"></a>

本节包含一组预定义的查询，这些查询可执行典型的使用案例，如 TCO 分析和网络可视化。您可以按原样使用这些查询，或根据自己的需求对其进行修改。

**使用预定义的查询**

1. 在 AWS Migration Hub 控制台中，选择导航窗格中的**服务器**。

1. 要打开亚马逊 Athena 控制台，**请选择 “在亚马逊 Athena 中浏览数据”**。

1. 在 **Query Editor (查询编辑器)** 页面上，在 **Database (数据库)** 下的导航窗格中，确保选择了 **application\$1discovery\$1service\$1database**。

1. 在查询编辑器中选择加号 (**\$1**) 以创建新查询的选项卡。

1. 从 [预定义查询](#pq-query-examples)中复制其中一个查询。

1. 将查询粘贴到刚创建的新查询选项卡的查询窗格中。

1. 选择 **Run Query (运行查询)**。

## 预定义查询
<a name="pq-query-examples"></a>

选择标题以查看有关此查询的信息。

### 获取服务器的 IP 地址和主机名
<a name="pq-helper-function"></a>

该视图帮助程序函数检索给定服务器的 IP 地址和主机名。您可以在其他查询中使用此视图。有关如何创建视图的信息，请参阅 *Amazon Athena 用户指南*中的[创建视图](https://docs.aws.amazon.com/athena/latest/ug/create-view.html)。

```
CREATE OR REPLACE VIEW hostname_ip_helper AS 
SELECT DISTINCT
  "os"."host_name"
, "nic"."agent_id"
, "nic"."ip_address"
FROM
  os_info_agent os
, network_interface_agent nic
WHERE ("os"."agent_id" = "nic"."agent_id");
```

### 识别有或没有代理的服务器
<a name="pq-agents-installed-or-not"></a>

此查询可以帮助您执行数据验证。如果您在网络中的多台服务器上部署了代理，则可以使用此查询来了解在网络中是否存在其他没有部署代理的服务器。在此查询中，我们检查入站和出站网络流量，并且只筛选私有 IP 地址的流量。即以 `192`、`10` 或 `172` 开头的 IP 地址。

```
SELECT DISTINCT "destination_ip" "IP Address" ,
         (CASE
    WHEN (
    (SELECT "count"(*)
    FROM network_interface_agent
    WHERE ("ip_address" = "destination_ip") ) = 0) THEN
        'no'
        WHEN (
        (SELECT "count"(*)
        FROM network_interface_agent
        WHERE ("ip_address" = "destination_ip") ) > 0) THEN
            'yes' END) "agent_running"
    FROM outbound_connection_agent
WHERE ((("destination_ip" LIKE '192.%')
        OR ("destination_ip" LIKE '10.%'))
        OR ("destination_ip" LIKE '172.%'))
UNION
SELECT DISTINCT "source_ip" "IP ADDRESS" ,
         (CASE
    WHEN (
    (SELECT "count"(*)
    FROM network_interface_agent
    WHERE ("ip_address" = "source_ip") ) = 0) THEN
        'no'
        WHEN (
        (SELECT "count"(*)
        FROM network_interface_agent
        WHERE ("ip_address" = "source_ip") ) > 0) THEN
            'yes' END) "agent_running"
    FROM inbound_connection_agent
WHERE ((("source_ip" LIKE '192.%')
        OR ("source_ip" LIKE '10.%'))
        OR ("source_ip" LIKE '172.%'));
```

### 分析带有代理的服务器的系统性能数据
<a name="pq-agents-server-performance"></a>

您可以使用此查询来分析安装了代理的本地服务器的系统性能和使用率模式数据。此查询结合 `system_performance_agent` 表与 `os_info_agent` 表来标识每个服务器的主机名。此查询为其上正在运行代理的所有服务器返回时间序列利用率数据（以 15 分钟为间隔）。

```
SELECT "OS"."os_name" "OS Name" ,
    "OS"."os_version" "OS Version" ,
    "OS"."host_name" "Host Name" ,
     "SP"."agent_id" ,
     "SP"."total_num_cores" "Number of Cores" ,
     "SP"."total_num_cpus" "Number of CPU" ,
     "SP"."total_cpu_usage_pct" "CPU Percentage" ,
     "SP"."total_disk_size_in_gb" "Total Storage (GB)" ,
     "SP"."total_disk_free_size_in_gb" "Free Storage (GB)" ,
     ("SP"."total_disk_size_in_gb" - "SP"."total_disk_free_size_in_gb") "Used Storage" ,
     "SP"."total_ram_in_mb" "Total RAM (MB)" ,
     ("SP"."total_ram_in_mb" - "SP"."free_ram_in_mb") "Used RAM (MB)" ,
     "SP"."free_ram_in_mb" "Free RAM (MB)" ,
     "SP"."total_disk_read_ops_per_sec" "Disk Read IOPS" ,
     "SP"."total_disk_bytes_written_per_sec_in_kbps" "Disk Write IOPS" ,
     "SP"."total_network_bytes_read_per_sec_in_kbps" "Network Reads (kbps)" ,
     "SP"."total_network_bytes_written_per_sec_in_kbps" "Network Write (kbps)"
FROM "sys_performance_agent" "SP" , "OS_INFO_agent" "OS"
WHERE ("SP"."agent_id" = "OS"."agent_id") limit 10;
```

### 根据端口号和进程详细信息跟踪服务器之间的出站通信
<a name="pq-analyze-outbound-connections"></a>

此查询获取每个服务的出站流量的详细信息，以及端口号和进程详细信息。

在运行查询之前，如果尚未执行此操作，则必须创建 `iana_service_ports_import` 表，其中包含从 IANA 下载的 IANA 端口注册表数据库。有关如何创建此表的信息，请参阅 [创建 IANA 港口注册表导入表](#pq-create-iana-import-table)。

创建 `iana_service_ports_import` 表后，创建两个用于跟踪出站流量的视图帮助程序函数。有关如何创建视图的信息，请参阅 *Amazon Athena 用户指南*中的[创建视图](https://docs.aws.amazon.com/athena/latest/ug/create-view.html)。

**创建出站跟踪帮助程序函数**

1. 从 [https://console.aws.amazon.com/athena/](https://console.aws.amazon.com/athena/home) 打开 Athena 控制台。

1. 使用以下列出所有不同出站目标 IP 地址的辅助函数创建`valid_outbound_ips_helper`视图。

   ```
   CREATE OR REPLACE VIEW valid_outbound_ips_helper AS 
   SELECT DISTINCT "destination_ip"
   FROM outbound_connection_agent;
   ```

1. 使用以下帮助程序函数创建 `outbound_query_helper` 视图，该函数确定出站流量的通信频率。

   ```
   CREATE OR REPLACE VIEW outbound_query_helper AS
   SELECT "agent_id" ,
            "source_ip" ,
            "destination_ip" ,
            "destination_port" ,
            "agent_assigned_process_id" ,
            "count"(*) "frequency"
   FROM outbound_connection_agent
   WHERE (("ip_version" = 'IPv4')
           AND ("destination_ip" IN 
       (SELECT *
       FROM valid_outbound_ips_helper )))
   GROUP BY  "agent_id", "source_ip", "destination_ip", "destination_port", "agent_assigned_process_id";
   ```

1. 在创建 `iana_service_ports_import` 表和两个帮助程序函数之后，您可以运行以下查询来获取每个服务的出站流量的详细信息，以及端口号和进程详细信息。

   ```
   SELECT hip1.host_name "Source Host Name",
            outbound_connections_results0.source_ip "Source IP Address",
            hip2.host_name "Destination Host Name",
            outbound_connections_results0.destination_ip "Destination IP Address",
            outbound_connections_results0.frequency "Connection Frequency",
            outbound_connections_results0.destination_port "Destination Communication Port",
            outbound_connections_results0.servicename "Process Service Name",
            outbound_connections_results0.description "Process Service Description"
   FROM 
       (SELECT DISTINCT o.source_ip,
            o.destination_ip,
            o.frequency,
            o.destination_port,
            ianap.servicename,
            ianap.description
       FROM outbound_query_helper o, iana_service_ports_import ianap
       WHERE o.destination_port = TRY_CAST(ianap.portnumber AS integer)) AS outbound_connections_results0 LEFT OUTER
   JOIN hostname_ip_helper hip1
       ON outbound_connections_results0.source_ip = hip1.ip_address LEFT OUTER
   JOIN hostname_ip_helper hip2
       ON outbound_connections_results0.destination_ip = hip2.ip_address
   ```

### 根据端口号和进程详细信息跟踪服务器之间的入站通信
<a name="pq-analyze-inbound-connections"></a>

此查询获取有关每个服务的入站流量的信息，以及端口号和进程详细信息。

在运行此查询之前，如果尚未执行此操作，则必须创建 `iana_service_ports_import` 表，其中包含从 IANA 下载的 IANA 端口注册表数据库。有关如何创建此表的信息，请参阅 [创建 IANA 港口注册表导入表](#pq-create-iana-import-table)。

创建 `iana_service_ports_import` 表后，创建两个用于跟踪入站流量的视图帮助程序函数。有关如何创建视图的信息，请参阅 *Amazon Athena 用户指南*中的[创建视图](https://docs.aws.amazon.com/athena/latest/ug/create-view.html)。

**创建导入跟踪帮助程序函数**

1. 从 [https://console.aws.amazon.com/athena/](https://console.aws.amazon.com/athena/home) 打开 Athena 控制台。

1. 使用以下帮助程序函数创建 `valid_inbound_ips_helper` 视图，该函数列出了所有不同的入站源 IP 地址。

   ```
   CREATE OR REPLACE VIEW valid_inbound_ips_helper AS 
   SELECT DISTINCT "source_ip"
   FROM inbound_connection_agent;
   ```

1. 使用以下帮助程序函数创建 `inbound_query_helper` 视图，该函数确定入站流量的通信频率。

   ```
   CREATE OR REPLACE VIEW inbound_query_helper AS 
   SELECT "agent_id" ,
            "source_ip" ,
            "destination_ip" ,
            "destination_port" ,
            "agent_assigned_process_id" ,
            "count"(*) "frequency"
   FROM inbound_connection_agent
   WHERE (("ip_version" = 'IPv4')
           AND ("source_ip" IN 
       (SELECT *
       FROM valid_inbound_ips_helper )))
   GROUP BY  "agent_id", "source_ip", "destination_ip", "destination_port", "agent_assigned_process_id";
   ```

1. 在创建 `iana_service_ports_import` 表和两个帮助程序函数之后，您可以运行以下查询来获取每个服务的入站流量的详细信息，以及端口号和进程详细信息。

   ```
   SELECT hip1.host_name "Source Host Name",
            inbound_connections_results0.source_ip "Source IP Address",
            hip2.host_name "Destination Host Name",
            inbound_connections_results0.destination_ip "Destination IP Address",
            inbound_connections_results0.frequency "Connection Frequency",
            inbound_connections_results0.destination_port "Destination Communication Port",
            inbound_connections_results0.servicename "Process Service Name",
            inbound_connections_results0.description "Process Service Description"
   FROM 
       (SELECT DISTINCT i.source_ip,
            i.destination_ip,
            i.frequency,
            i.destination_port,
            ianap.servicename,
            ianap.description
       FROM inbound_query_helper i, iana_service_ports_import ianap
       WHERE i.destination_port = TRY_CAST(ianap.portnumber AS integer)) AS inbound_connections_results0 LEFT OUTER
   JOIN hostname_ip_helper hip1
       ON inbound_connections_results0.source_ip = hip1.ip_address LEFT OUTER
   JOIN hostname_ip_helper hip2
       ON inbound_connections_results0.destination_ip = hip2.ip_address
   ```

### 通过端口号识别正在运行的软件
<a name="pq-identify-software"></a>

此查询基于端口号识别正在运行的软件。

在运行此查询之前，如果尚未执行此操作，则必须创建 `iana_service_ports_import` 表，其中包含从 IANA 下载的 IANA 端口注册表数据库。有关如何创建此表的信息，请参阅 [创建 IANA 港口注册表导入表](#pq-create-iana-import-table)。

运行以下查询，以基于端口号识别正在运行的软件。

```
SELECT o.host_name "Host Name",
       ianap.servicename "Service",
       ianap.description "Description",
       con.destination_port,
       con.cnt_dest_port "Destination Port Count"
FROM   (SELECT agent_id,
               destination_ip,
               destination_port,
               Count(destination_port) cnt_dest_port
        FROM   inbound_connection_agent
        GROUP  BY agent_id,
                  destination_ip,
                  destination_port) con,
       (SELECT agent_id,
               host_name,
               Max("timestamp")
        FROM   os_info_agent
        GROUP  BY agent_id,
                  host_name) o,
       iana_service_ports_import ianap
WHERE  ianap.transportprotocol = 'tcp'
       AND con.destination_ip NOT LIKE '172%'
       AND con.destination_port = ianap.portnumber
       AND con.agent_id = o.agent_id
ORDER BY cnt_dest_port DESC;
```

## 创建 IANA 港口注册表导入表
<a name="pq-create-iana-import-table"></a>

某些预定义的查询需要一个名为 `iana_service_ports_import` 的表，该表包含从 Internet 号码分配机构 (IANA) 下载的信息。

**创建 iana\$1service\$1ports\$1import 表**

1. 从 iana.org 上的[服务名称和传输协议端口号注册表下载 I *AN* A 端口注册表](https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml)数据库 **CSV** 文件。

1. 将文件上传到 Amazon S3。有关更多信息，请参阅[如何将文件和文件夹上传至 S3 存储桶？](https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html)

1. 在 Athena 中创建一个名为的新表。`iana_service_ports_import`有关说明，请参阅 *Amazon Athen* [a 用户指南中的创建表](https://docs.aws.amazon.com/athena/latest/ug/getting-started.html#step-2-create-a-table)。在以下示例中，您需要将 `my_bucket_name` 替换为在上一步中将 CSV 文件上传到的 S3 存储桶的名称。

   ```
   CREATE EXTERNAL TABLE IF NOT EXISTS iana_service_ports_import (
            ServiceName STRING,
            PortNumber INT,
            TransportProtocol STRING,
            Description STRING,
            Assignee STRING,
            Contact STRING,
            RegistrationDate STRING,
            ModificationDate STRING,
            Reference STRING,
            ServiceCode STRING,
            UnauthorizedUseReported STRING,
            AssignmentNotes STRING
   )
   ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
   WITH SERDEPROPERTIES (
     'serialization.format' = ',',
     'quoteChar' = '"',
     'field.delim' = ','
   ) LOCATION 's3://my_bucket_name/'
   TBLPROPERTIES ('has_encrypted_data'='false',"skip.header.line.count"="1");
   ```