

# VPC 的 CloudWatch 指标
<a name="vpc-cloudwatch"></a>

Amazon VPC 会将有关 VPC 的数据发布到 Amazon CloudWatch。您可以将有关 VPC 的统计数据作为一组有序时间序列数据（称作*指标*）进行检索。可将指标视为要监控的变量，而将数据视为该变量随时间变化的值。有关更多信息，请参阅 [Amazon CloudWatch 用户指南](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/)。

**Topics**
+ [NAU 指标与维度](#nau-cloudwatch)
+ [启用或禁用 NAU 监控](#nau-monitoring-enable)
+ [NAU CloudWatch 告警示例](#nau-cloudwatch-alarm-example)

## NAU 指标与维度
<a name="nau-cloudwatch"></a>

[网络地址用量](network-address-usage.md) (NAU) 是应用于虚拟网络中资源的指标，可以帮助您规划和监控 VPC 的大小。监控 NAU 不收取任何费用。监控 NAU 非常有用，因为如果耗尽了 VPC 的 NAU 限额或对等 NAU 限额，您将无法启动新的 EC2 实例或预调配新的资源（例如网络负载均衡器、VPC 端点、Lambda 函数、中转网关连接或 NAT 网关）。

如果您为 VPC 启用了网络地址用量监控，Amazon VPC 会将与 NAU 相关的指标发送到 Amazon CloudWatch。VPC 的大小取决于 VPC 包含的网络地址用量（NAU）单元数。

您可以使用这些指标来了解您的 VPC 增长率、预测 VPC 何时达到其大小限制，或者在超过其大小阈值时创建告警。

`AWS/EC2` 命名空间包括以下用于监控 NAU 的指标。


| 指标 | 描述 | 
| --- | --- | 
|  NetworkAddressUsage  |  每个 VPC 的 NAU 计数。 **报告标准** [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/vpc/latest/userguide/vpc-cloudwatch.html) **尺寸** [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/vpc/latest/userguide/vpc-cloudwatch.html)  | 
|  NetworkAddressUsagePeered  | VPC 和与之对等的所有 VPC 的 NAU 计数。**报告标准**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/vpc/latest/userguide/vpc-cloudwatch.html)**尺寸**[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/vpc/latest/userguide/vpc-cloudwatch.html) | 

`AWS/Usage` 命名空间包括以下用于监控 NAU 的指标。


| 指标 | 描述 | 
| --- | --- | 
|  ResourceCount  |  每个 VPC 的 NAU 计数。 **报告标准** [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/vpc/latest/userguide/vpc-cloudwatch.html) **尺寸** [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/vpc/latest/userguide/vpc-cloudwatch.html)  | 
|  ResourceCount  |  VPC 和与之对等的所有 VPC 的 NAU 计数。 **报告标准** [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/vpc/latest/userguide/vpc-cloudwatch.html) **尺寸** [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/vpc/latest/userguide/vpc-cloudwatch.html)  | 
|  ResourceCount  |  跨 VPC 的 NAU 用量组合视图。 **报告标准** [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/vpc/latest/userguide/vpc-cloudwatch.html) **尺寸** [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/vpc/latest/userguide/vpc-cloudwatch.html)  | 
|  ResourceCount  |  跨对等 VPC 的 NAU 用量组合视图。 **报告标准** [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/vpc/latest/userguide/vpc-cloudwatch.html) **尺寸** [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/vpc/latest/userguide/vpc-cloudwatch.html)  | 

## 启用或禁用 NAU 监控
<a name="nau-monitoring-enable"></a>

要在 CloudWatch 中查看 NAU 指标，必须先在每个要监控的 VPC 上启用监控。

**启用或禁用监控 NAU**

1. 通过以下网址打开 Amazon VPC 控制台：[https://console.aws.amazon.com/vpc/](https://console.aws.amazon.com/vpc/)。

1. 在导航窗格中，选择 **Your VPCs**(您的 VPC)。

1. 选中 VPC 的复选框。

1. 依次选择 **Actions**（操作）、**Edit VPC settings**（编辑 VPC 设置）。

1. 请执行以下操作之一：
   + 要启用监控，依次选择 **Network mapping units metrics settings**（网络映射单元指标设置），**Enable network address usage metrics**（启用网络地址用量指标）。
   + 要禁用监控，请清除 **Network mapping units metrics settings**（网络映射单元指标设置）和 **Enable network address usage metrics**（启用网络地址用量指标）。

**使用命令行启用或禁用监控**
+ [modify-vpc-attribute](https://docs.aws.amazon.com/cli/latest/reference/ec2/modify-vpc-attribute.html) (AWS CLI)
+ [Edit-EC2VpcAttribute](https://docs.aws.amazon.com/powershell/latest/reference/items/Edit-EC2VpcAttribute.html) (AWS Tools for Windows PowerShell)

## NAU CloudWatch 告警示例
<a name="nau-cloudwatch-alarm-example"></a>

您可以使用以下 AWS CLI 命令和示例 `.json` 创建 Amazon CloudWatch 警报和 SNS 通知，以 50,000 个 NAU 为阈值跟踪 VPC 的 NAU 利用率。此示例要求您先创建 Amazon SNS 主题。有关更多信息，请参阅 *Amazon Simple Notification Service 开发人员指南*中的 [Amazon SNS 入门](https://docs.aws.amazon.com/sns/latest/dg/sns-getting-started.html)。

```
aws cloudwatch put-metric-alarm --cli-input-json file://nau-alarm.json
```

以下是 `nau-alarm.json` 的示例。

```
{
    "Namespace": "AWS/EC2",
    "MetricName": "NetworkAddressUsage",
    "Dimensions": [{
        "Name": "Per-VPC Metrics",
        "Value": "vpc-0123456798"
    }],
    "AlarmActions": ["arn:aws:sns:us-west-1:123456789012:my_sns_topic"],
    "ComparisonOperator": "GreaterThanThreshold",
    "Period": 86400,
    "EvaluationPeriods": 1,
    "Threshold": 50000,
    "AlarmDescription": "Tracks NAU utilization of the VPC with 50k NAUs as the threshold",
    "AlarmName": "VPC NAU Utilization",
    "Statistic": "Maximum"
}
```