将 PutDashboard 与 Amazon SDK 或 CLI 配合使用 - Amazon CloudWatch
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

PutDashboard 与 Amazon SDK 或 CLI 配合使用

以下代码示例演示如何使用 PutDashboard

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

.NET
Amazon SDK for .NET
注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

/// <summary> /// Set up a dashboard using a call to the wrapper class. /// </summary> /// <param name="customMetricNamespace">The metric namespace.</param> /// <param name="customMetricName">The metric name.</param> /// <param name="dashboardName">The name of the dashboard.</param> /// <returns>A list of validation messages.</returns> private static async Task<List<DashboardValidationMessage>> SetupDashboard( string customMetricNamespace, string customMetricName, string dashboardName) { // Get the dashboard model from configuration. var newDashboard = new DashboardModel(); _configuration.GetSection("dashboardExampleBody").Bind(newDashboard); // Add a new metric to the dashboard. newDashboard.Widgets.Add(new Widget { Height = 8, Width = 8, Y = 8, X = 0, Type = "metric", Properties = new Properties { Metrics = new List<List<object>> { new() { customMetricNamespace, customMetricName } }, View = "timeSeries", Region = "us-east-1", Stat = "Sum", Period = 86400, YAxis = new YAxis { Left = new Left { Min = 0, Max = 100 } }, Title = "Custom Metric Widget", LiveData = true, Sparkline = true, Trend = true, Stacked = false, SetPeriodToTimeRange = false } }); var newDashboardString = JsonSerializer.Serialize(newDashboard, new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }); var validationMessages = await _cloudWatchWrapper.PutDashboard(dashboardName, newDashboardString); return validationMessages; } /// <summary> /// Wrapper to create or add to a dashboard with metrics. /// </summary> /// <param name="dashboardName">The name for the dashboard.</param> /// <param name="dashboardBody">The metric data in JSON for the dashboard.</param> /// <returns>A list of validation messages for the dashboard.</returns> public async Task<List<DashboardValidationMessage>> PutDashboard(string dashboardName, string dashboardBody) { // Updating a dashboard replaces all contents. // Best practice is to include a text widget indicating this dashboard was created programmatically. var dashboardResponse = await _amazonCloudWatch.PutDashboardAsync( new PutDashboardRequest() { DashboardName = dashboardName, DashboardBody = dashboardBody }); return dashboardResponse.DashboardValidationMessages; }
  • 有关 API 详细信息,请参阅《Amazon SDK for .NET API 参考》中的 PutDashboard

Java
SDK for Java 2.x
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

public static void createDashboardWithMetrics(CloudWatchClient cw, String dashboardName, String fileName) { try { PutDashboardRequest dashboardRequest = PutDashboardRequest.builder() .dashboardName(dashboardName) .dashboardBody(readFileAsString(fileName)) .build(); PutDashboardResponse response = cw.putDashboard(dashboardRequest); System.out.println(dashboardName + " was successfully created."); List<DashboardValidationMessage> messages = response.dashboardValidationMessages(); if (messages.isEmpty()) { System.out.println("There are no messages in the new Dashboard"); } else { for (DashboardValidationMessage message : messages) { System.out.println("Message is: " + message.message()); } } } catch (CloudWatchException | IOException e) { System.err.println(e.getMessage()); System.exit(1); } }
  • 有关 API 详细信息,请参阅《Amazon SDK for Java 2.x API 参考》中的 PutDashboard

Kotlin
适用于 Kotlin 的 SDK
注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

suspend fun createDashboardWithMetrics( dashboardNameVal: String, fileNameVal: String, ) { val dashboardRequest = PutDashboardRequest { dashboardName = dashboardNameVal dashboardBody = readFileAsString(fileNameVal) } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.putDashboard(dashboardRequest) println("$dashboardNameVal was successfully created.") val messages = response.dashboardValidationMessages if (messages != null) { if (messages.isEmpty()) { println("There are no messages in the new Dashboard") } else { for (message in messages) { println("Message is: ${message.message}") } } } } }
  • 有关 API 详细信息,请参阅《Amazon SDK for Kotlin API 参考》中的 PutDashboard

PowerShell
适用于 PowerShell 的工具

示例 1:创建或更新名为“Dashboard1”的控制面板,以包含两个并排的指标小部件。

$dashBody = @" { "widgets":[ { "type":"metric", "x":0, "y":0, "width":12, "height":6, "properties":{ "metrics":[ [ "AWS/EC2", "CPUUtilization", "InstanceId", "i-012345" ] ], "period":300, "stat":"Average", "region":"us-east-1", "title":"EC2 Instance CPU" } }, { "type":"metric", "x":12, "y":0, "width":12, "height":6, "properties":{ "metrics":[ [ "AWS/S3", "BucketSizeBytes", "BucketName", "MyBucketName" ] ], "period":86400, "stat":"Maximum", "region":"us-east-1", "title":"MyBucketName bytes" } } ] } "@ Write-CWDashboard -DashboardName Dashboard1 -DashboardBody $dashBody

示例 2:创建或更新控制面板,将描述控制面板的内容通过管道传输到 cmdlet 中。

$dashBody = @" { ... } "@ $dashBody | Write-CWDashboard -DashboardName Dashboard1
  • 有关 API 详细信息,请参阅《Amazon Tools for PowerShell Cmdlet 参考》中的 PutDashboard

有关 Amazon SDK 开发人员指南和代码示例的完整列表,请参阅 将 CloudWatch 与 Amazon SDK 结合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。