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

将 CLI 与 Amazon CloudWatch 网络监测仪结合使用的示例

本节包括将 Amazon Command Line Interface 与 Amazon CloudWatch 网络监测仪操作结合使用的示例。

在开始之前,请确保您登录以将 Amazon CLI 与相同 Amazon 账户结合使用,该账户具有要监控的 Amazon 虚拟私有云(VPC)、网络负载均衡器、Amazon CloudFront 分配或 Amazon WorkSpaces 目录。Internet Monitor 不支持跨账户访问资源。有关使用 Amazon CLI 的更多信息,请参阅 Amazon CLI 命令参考。有关将 API 操作与 Amazon CloudWatch 网络监测仪结合使用的更多信息,请参阅 Amazon CloudWatch 网络监测仪 API 参考指南

创建监视器

在 Internet Monitor 中创建监视器时,您需要提供一个名称并将资源与监视器相关联,以显示应用程序的互联网流量。您可以指定流量百分比,该百分比定义了您的应用程序流量受到监测的程度。这也决定了受到监测的城市网络(即客户端位置和 ASN,通常是互联网服务提供商或 ISP)的数量。您还可以为要监测的应用程序资源的城市网络最大数量设置限制,以帮助您控制账单成本。有关更多信息,请参阅选择一个城市-网络最大限制

最后,您可以选择是否要将应用程序的所有互联网测量结果发布到 Amazon S3。网络监测仪会将前 500 个(按流量计)城市网络的互联网测量数据自动发布到 CloudWatch Logs,但您可以选择同时将所有测量结果发布到 S3。

要使用 Amazon CLI 创建监测仪,请使用 create-monitor 命令。以下命令将创建一个监测仪,该监测仪可监控 100% 的流量,但将城市网络的最大限制设置为 10,000,添加一个 VPC 资源,并选择将互联网测量结果发布到 Amazon S3。

注意

Internet Monitor 每五分钟将向每个监测仪发送流量的前 500 个城市网络(客户端位置和 ASN,通常是互联网服务提供商或 ISP)的互联网测量数据发布到 CloudWatch Logs。或者,您可以选择将所有受监测城市网络(最多 50 万个城市网络的服务限制)的互联网测量数据发布到 Amazon S3 存储桶。有关更多信息,请参阅在 Amazon CloudWatch 网络监测仪中将互联网测量数据发布到 Amazon S3

aws internetmonitor --create-monitor monitor-name "TestMonitor" \ --traffic-percentage-to-monitor 100 \ --max-city-networks-to-monitor 10000 \ --resources "arn:aws:ec2:us-east-1:111122223333:vpc/vpc-11223344556677889" \ --internet-measurements-log-delivery S3Config="{BucketName=MyS3Bucket,LogDeliveryStatus=ENABLED}"
{ "Arn": "arn:aws:internetmonitor:us-east-1:111122223333:monitor/TestMonitor", "Status": "ACTIVE" }
注意

您不能更改监测仪的名称。

查看显示器详细信息

要使用 Amazon CLI 查看有关监测仪的信息,请使用 get-monitor 命令。

aws internetmonitor get-monitor --monitor-name "TestMonitor"
{ "ClientLocationType": "city", "CreatedAt": "2022-09-22T19:27:47Z", "ModifiedAt": "2022-09-22T19:28:30Z", "MonitorArn": "arn:aws:internetmonitor:us-east-1:111122223333:monitor/TestMonitor", "MonitorName": "TestMonitor", "ProcessingStatus": "OK", "ProcessingStatusInfo": "The monitor is actively processing data", "Resources": [ "arn:aws:ec2:us-east-1:111122223333:vpc/vpc-11223344556677889" ], "MaxCityNetworksToMonitor": 10000, "Status": "ACTIVE" }

列出运行状况事件

当应用程序的互联网流量性能下降时,Internet Monitor 会在监视器中创建运行状况事件。要使用 Amazon CLI 查看当前运行状况事件列表,请使用 list-health-events 命令

aws internetmonitor list-health-events --monitor-name "TestMonitor"
{ "HealthEvents": [ { "EventId": "2022-06-20T01-05-05Z/latency", "Status": "RESOLVED", "EndedAt": "2022-06-20T01:15:14Z", "ServiceLocations": [ { "Name": "us-east-1" } ], "PercentOfTotalTrafficImpacted": 1.21, "ClientLocations": [ { "City": "Lockport", "PercentOfClientLocationImpacted": 60.370000000000005, "PercentOfTotalTraffic": 2.01, "Country": "United States", "Longitude": -78.6913, "AutonomousSystemNumber": 26101, "Latitude": 43.1721, "Subdivision": "New York", "NetworkName": "YAHOO-BF1" } ], "StartedAt": "2022-06-20T01:05:05Z", "ImpactType": "PERFORMANCE", "EventArn": "arn:aws:internetmonitor:us-east-1:111122223333:monitor/TestMonitor/health-event/2022-06-20T01-05-05Z/latency" }, { "EventId": "2022-06-20T01-17-56Z/latency", "Status": "RESOLVED", "EndedAt": "2022-06-20T01:30:23Z", "ServiceLocations": [ { "Name": "us-east-1" } ], "PercentOfTotalTrafficImpacted": 1.29, "ClientLocations": [ { "City": "Toronto", "PercentOfClientLocationImpacted": 75.32, "PercentOfTotalTraffic": 1.05, "Country": "Canada", "Longitude": -79.3623, "AutonomousSystemNumber": 14061, "Latitude": 43.6547, "Subdivision": "Ontario", "CausedBy": { "Status": "ACTIVE", "Networks": [ { "AutonomousSystemNumber": 16509, "NetworkName": "Amazon.com" } ], "NetworkEventType": "AWS" }, "NetworkName": "DIGITALOCEAN-ASN" }, { "City": "Lockport", "PercentOfClientLocationImpacted": 22.91, "PercentOfTotalTraffic": 2.01, "Country": "United States", "Longitude": -78.6913, "AutonomousSystemNumber": 26101, "Latitude": 43.1721, "Subdivision": "New York", "NetworkName": "YAHOO-BF1" }, { "City": "Hangzhou", "PercentOfClientLocationImpacted": 2.88, "PercentOfTotalTraffic": 0.7799999999999999, "Country": "China", "Longitude": 120.1612, "AutonomousSystemNumber": 37963, "Latitude": 30.2994, "Subdivision": "Zhejiang", "NetworkName": "Hangzhou Alibaba Advertising Co.,Ltd." } ], "StartedAt": "2022-06-20T01:17:56Z", "ImpactType": "PERFORMANCE", "EventArn": "arn:aws:internetmonitor:us-east-1:111122223333:monitor/TestMonitor/health-event/2022-06-20T01-17-56Z/latency" }, { "EventId": "2022-06-20T01-34-20Z/latency", "Status": "RESOLVED", "EndedAt": "2022-06-20T01:35:04Z", "ServiceLocations": [ { "Name": "us-east-1" } ], "PercentOfTotalTrafficImpacted": 1.15, "ClientLocations": [ { "City": "Lockport", "PercentOfClientLocationImpacted": 39.45, "PercentOfTotalTraffic": 2.01, "Country": "United States", "Longitude": -78.6913, "AutonomousSystemNumber": 26101, "Latitude": 43.1721, "Subdivision": "New York", "NetworkName": "YAHOO-BF1" }, { "City": "Toronto", "PercentOfClientLocationImpacted": 29.770000000000003, "PercentOfTotalTraffic": 1.05, "Country": "Canada", "Longitude": -79.3623, "AutonomousSystemNumber": 14061, "Latitude": 43.6547, "Subdivision": "Ontario", "CausedBy": { "Status": "ACTIVE", "Networks": [ { "AutonomousSystemNumber": 16509, "NetworkName": "Amazon.com" } ], "NetworkEventType": "AWS" }, "NetworkName": "DIGITALOCEAN-ASN" }, { "City": "Hangzhou", "PercentOfClientLocationImpacted": 2.88, "PercentOfTotalTraffic": 0.7799999999999999, "Country": "China", "Longitude": 120.1612, "AutonomousSystemNumber": 37963, "Latitude": 30.2994, "Subdivision": "Zhejiang", "NetworkName": "Hangzhou Alibaba Advertising Co.,Ltd." } ], "StartedAt": "2022-06-20T01:34:20Z", "ImpactType": "PERFORMANCE", "EventArn": "arn:aws:internetmonitor:us-east-1:111122223333:monitor/TestMonitor/health-event/2022-06-20T01-34-20Z/latency" } ] }

查看特定的运行状况事件

要使用 CLI 查看有关特定运行状况事件的更多详细信息,请使用您的监视器名称和运行状况事件 ID 运行 get-health-event 命令。

aws internetmonitor get-monitor --monitor-name "TestMonitor" --event-id "health-event/TestMonitor/2021-06-03T01:02:03Z/latency"
{ "EventId": "2022-06-20T01-34-20Z/latency", "Status": "RESOLVED", "EndedAt": "2022-06-20T01:35:04Z", "ServiceLocations": [ { "Name": "us-east-1" } ], "EventArn": "arn:aws:internetmonitor:us-east-1:111122223333:monitor/TestMonitor/health-event/2022-06-20T01-34-20Z/latency", "LastUpdatedAt": "2022-06-20T01:35:04Z", "ClientLocations": [ { "City": "Lockport", "PercentOfClientLocationImpacted": 39.45, "PercentOfTotalTraffic": 2.01, "Country": "United States", "Longitude": -78.6913, "AutonomousSystemNumber": 26101, "Latitude": 43.1721, "Subdivision": "New York", "NetworkName": "YAHOO-BF1" }, { "City": "Toronto", "PercentOfClientLocationImpacted": 29.770000000000003, "PercentOfTotalTraffic": 1.05, "Country": "Canada", "Longitude": -79.3623, "AutonomousSystemNumber": 14061, "Latitude": 43.6547, "Subdivision": "Ontario", "CausedBy": { "Status": "ACTIVE", "Networks": [ { "AutonomousSystemNumber": 16509, "NetworkName": "Amazon.com" } ], "NetworkEventType": "AWS" }, "NetworkName": "DIGITALOCEAN-ASN" }, { "City": "Shenzhen", "PercentOfClientLocationImpacted": 4.07, "PercentOfTotalTraffic": 0.61, "Country": "China", "Longitude": 114.0683, "AutonomousSystemNumber": 37963, "Latitude": 22.5455, "Subdivision": "Guangdong", "NetworkName": "Hangzhou Alibaba Advertising Co.,Ltd." }, { "City": "Hangzhou", "PercentOfClientLocationImpacted": 2.88, "PercentOfTotalTraffic": 0.7799999999999999, "Country": "China", "Longitude": 120.1612, "AutonomousSystemNumber": 37963, "Latitude": 30.2994, "Subdivision": "Zhejiang", "NetworkName": "Hangzhou Alibaba Advertising Co.,Ltd." } ], "StartedAt": "2022-06-20T01:34:20Z", "ImpactType": "PERFORMANCE", "PercentOfTotalTrafficImpacted": 1.15 }

查看监视器列表

要使用 CLI 查看您账户中所有监视器的列表,请运行 list-monitors 命令。

aws internetmonitor list-monitors
{ "Monitors": [ { "MonitorName": "TestMonitor", "ProcessingStatus": "OK", "Status": "ACTIVE" } ], "NextToken": " zase12" }

编辑监视器

要使用 CLI 更新有关监视器的信息,请使用 update-monitor 命令并指定要更新的监视器的名称。您可以更新要监测的流量百分比、要监测的城市-网络最大数量限制、添加或移除网络监测仪用于监测流量的资源,以及将监测仪状态从 ACTIVE 改为 INACTIVE,反之亦然。请注意,您不能更改监视器的名称。

update-monitor 调用的响应仅返回 MonitorArnStatus

以下示例显示了如何使用 update-monitor 命令将要监测的城市网络最大数量更改为 50000

aws internetmonitor update-monitor --monitor-name "TestMonitor" --max-city-networks-to-monitor 50000
{ "MonitorArn": "arn:aws:internetmonitor:us-east-1:111122223333:monitor/TestMonitor", "Status": " ACTIVE " }

以下示例显示了如何添加和删除资源:

aws internetmonitor update-monitor --monitor-name "TestMonitor" \ --resources-to-add "arn:aws:ec2:us-east-1:111122223333:vpc/vpc-11223344556677889" \ --resources-to-remove "arn:aws:ec2:us-east-1:111122223333:vpc/vpc-2222444455556666"
{ "MonitorArn": "arn:aws:internetmonitor:us-east-1:111122223333:monitor/TestMonitor", "Status": "ACTIVE" }

以下示例显示了如何使用 update-monitor 命令将监视器状态更改为 INACTIVE

aws internetmonitor update-monitor --monitor-name "TestMonitor" --status "INACTIVE"
{ "MonitorArn": "arn:aws:internetmonitor:us-east-1:111122223333:monitor/TestMonitor", "Status": "INACTIVE" }

删除监视器

您可以使用 delete-monitor 命令通过 CLI 删除监视器。首先,必须将监视器设置为非活动状态。为此,请使用 update-monitor 命令将状态更改为 INACTIVE。使用 get-monitor 命令并检查状态,确认监视器处于非活动状态。

当监视器状态为 INACTIVE 时,您可以使用 CLI 运行 delete-monitor 命令来删除监视器。成功的 delete-monitor 调用的响应为空。

aws internetmonitor delete-monitor --monitor-name "TestMonitor"
{}