AWS Support
用户指南 (API 版本 2013-04-15)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

使用 Trusted Advisor 即 Web 服务

借助 AWS Support 服务,您可以编写与 AWS Trusted Advisor 交互的应用程序。此主题演示如何获取 Trusted Advisor 检查的列表、刷新其中一个检查,然后获取检查返回的详细结果。这些任务用 Java 进行演示。有关针对其他语言的支持的信息,请参阅用于 Amazon Web Services 的工具

获取可用 Trusted Advisor 检查的列表

以下 Java 代码段创建一个 AWS Support 客户端实例,您可使用该客户端来调用所有 Trusted Advisor 操作。接下来,这段代码通过调用 DescribeTrustedAdvisorChecks 操作,获取 Trusted Advisor 检查的列表及其相应的 CheckId 值。您可以使用此信息来构建用户界面,让用户通过此界面选择他们想运行或刷新的检查。

private static AWSSupport createClient() { return AWSSupportClientBuilder.defaultClient(); } // Get the List of Available Trusted Advisor Checks public static void getTAChecks() { // Possible language parameters: "en" (English), "ja" (Japanese), "fr" (French), "zh" (Chinese) DescribeTrustedAdvisorChecksRequest request = new DescribeTrustedAdvisorChecksRequest().withLanguage("en"); DescribeTrustedAdvisorChecksResult result = createClient().describeTrustedAdvisorChecks(request); for (TrustedAdvisorCheckDescription description : result.getChecks()) { // Do something with check description. System.out.println(description.getId()); System.out.println(description.getName()); } }

刷新可用 Trusted Advisor 检查的列表

以下 Java 代码段创建一个 AWS Support 客户端实例,您可使用该客户端来刷新 Trusted Advisor 数据。

// Refresh a Trusted Advisor Check // Note: Some checks are refreshed automatically, and they cannot be refreshed by using this operation. // Specifying the check ID of a check that is automatically refreshed causes an InvalidParameterValue error. public static void refreshTACheck(final String checkId) { RefreshTrustedAdvisorCheckRequest request = new RefreshTrustedAdvisorCheckRequest().withCheckId(checkId); RefreshTrustedAdvisorCheckResult result = createClient().refreshTrustedAdvisorCheck(request); System.out.println("CheckId: " + result.getStatus().getCheckId()); System.out.println("Milliseconds until refreshable: " + result.getStatus().getMillisUntilNextRefreshable()); System.out.println("Refresh Status: " + result.getStatus().getStatus()); }

轮询 Trusted Advisor 检查以了解状态变化

在提交运行 Trusted Advisor 检查以生成最新状态数据的请求之后,请使用 DescribeTrustedAdvisorCheckRefreshStatuses 操作请求检查运行进度以及新数据做好检查准备的时间。

以下 Java 代码段使用 CheckId 变量中的相应值获取在以下部分中请求的检查的状态。此外,此段代码还演示了 Trusted Advisor 服务的其他几种用途:

  1. 您可以通过遍历 DescribeTrustedAdvisorCheckRefreshStatusesResult 实例中包含的对象来调用 getMillisUntilNextRefreshable。您可以使用返回的值来测试是否希望代码继续刷新检查。

  2. 如果 timeUntilRefreshable 等于零,您可以请求刷新检查。

  3. 您可以使用返回的状态继续轮询状态变化,代码段将轮询间隔设置为建议的 10 秒。如果状态为 enqueuedin_progress,循环将返回并再次请求状态。如果调用返回 successful,则循环终止。

  4. 最后,代码返回一个 DescribeTrustedAdvisorCheckResultResult 数据类型的实例,您可使用该实例遍历检查所生成的信息。

注意:请先使用单个刷新请求,然后再轮询请求的状态。

// Retrieves TA refresh statuses. Multiple checkId's can be submitted. public static List<TrustedAdvisorCheckRefreshStatus> getTARefreshStatus(final String... checkIds) { DescribeTrustedAdvisorCheckRefreshStatusesRequest request = new DescribeTrustedAdvisorCheckRefreshStatusesRequest().withCheckIds(checkIds); DescribeTrustedAdvisorCheckRefreshStatusesResult result = createClient().describeTrustedAdvisorCheckRefreshStatuses(request); return result.getStatuses(); } // Retrieves a TA check status, and checks to see if it has finished processing. public static boolean isTACheckStatusInTerminalState(final String checkId) { // Since we only submitted one checkId to getTARefreshStatus, just retrieve the only element in the list. TrustedAdvisorCheckRefreshStatus status = getTARefreshStatus(checkId).get(0); // Valid statuses are: // 1. "none", the check has never been refreshed before. // 2. "enqueued", the check is waiting to be processed. // 3. "processing", the check is in the midst of being processed. // 4. "success", the check has succeeded and finished processing - refresh data is available. // 5. "abandoned", the check has failed to process. return status.getStatus().equals("abandoned") || status.getStatus().equals("success"); } // Enqueues a Trusted Advisor check refresh. Periodically polls the check refresh status for completion. public static TrustedAdvisorCheckResult getFreshTACheckResult(final String checkId) throws InterruptedException { refreshTACheck(checkId); while(!isTACheckStatusInTerminalState(checkId)) { Thread.sleep(10000); } return getTACheckResult(checkId); } // Retrieves fresh TA check data whenever possible. // Note: Some checks are refreshed automatically, and they cannot be refreshed by using this operation. This method // is only functional for checks that can be refreshed using the RefreshTrustedAdvisorCheck operation. public static void pollForTACheckResultChanges(final String checkId) throws InterruptedException { String checkResultStatus = null; do { TrustedAdvisorCheckResult result = getFreshTACheckResult(checkId); if (checkResultStatus != null && !checkResultStatus.equals(result.getStatus())) { break; } checkResultStatus = result.getStatus(); // The rule refresh has completed, but due to throttling rules the checks may not be refreshed again // for a short period of time. // Since we only submitted one checkId to getTARefreshStatus, just retrieve the only element in the list. TrustedAdvisorCheckRefreshStatus refreshStatus = getTARefreshStatus(checkId).get(0); Thread.sleep(refreshStatus.getMillisUntilNextRefreshable()); } while(true); // Signal that a TA check has changed check result status here. }

请求 Trusted Advisor 检查结果

选择所需的详细结果检查之后,使用 DescribeTrustedAdvisorCheckResult 操作来提交请求。

以下 Java 代码段使用 result 变量引用的 DescribeTrustedAdvisorChecksResult 实例(在之前的代码段中获得)。您提交运行请求之后,该代码段并未通过用户界面以交互方式定义检查,而是通过在每个 result.getChecks().get(0) 调用中指定索引值 0 来提交运行列表中第一个检查的请求。接下来,此段代码定义一个 DescribeTrustedAdvisorCheckResultRequest 实例,并将该实例传递给名为 checkResultDescribeTrustedAdvisorCheckResultResult 实例。您可以使用此数据类型的成员结构查看检查结果。

// Request a Trusted Advisor Check Result public static TrustedAdvisorCheckResult getTACheckResult(final String checkId) { DescribeTrustedAdvisorCheckResultRequest request = new DescribeTrustedAdvisorCheckResultRequest() // Possible language parameters: "en" (English), "ja" (Japanese), "fr" (French), "zh" (Chinese) .withLanguage("en") .withCheckId(checkId); DescribeTrustedAdvisorCheckResultResult requestResult = createClient().describeTrustedAdvisorCheckResult(request); return requestResult.getResult(); }

注意:请求 Trusted Advisor 检查结果不会生成更新的结果数据。

输出 Trusted Advisor 检查的详细信息

以下 Java 代码段遍历前一节返回的 DescribeTrustedAdvisorCheckResultResult 实例,以获取 Trusted Advisor 检查所标记的资源的列表。

// Print ResourceIds for flagged resources. for (TrustedAdvisorResourceDetail flaggedResource : result1.getResult().getFlaggedResources()) { System.out.println( "The resource for this ResourceID has been flagged: " + flaggedResource.getResourceId()); }