

# 在本地服务器上安装 CloudWatch 代理
<a name="install-CloudWatch-Agent-on-premise"></a>

 如果已在电脑上下载 CloudWatch 代理并创建了您的代理配置文件，您可以使用该配置文件在其他本地服务器中安装该代理。

## 在本地部署服务器上下载 CloudWatch 代理
<a name="download-CloudWatch-Agent-onprem"></a>

您可以使用 Systems Manager Run Command 或 Amazon S3 下载链接下载 CloudWatch 代理程序包。

### 使用 Systems Manager 下载
<a name="download-CloudWatch-Agent-onprem-fleet-sys"></a>

要使用 Systems Manager Run Command，您必须通过 Amazon EC2 Systems Manager 注册您的本地部署服务器。有关更多信息，请参阅 *AWS Systems Manager 用户指南*中的[在混合环境中设置 Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-managedinstances.html)。

如果已注册您的服务器，请将 SSM Agent 更新为最新版本。

有关在运行 Linux 的服务器上更新 SSM Agent 的信息，请参阅 *AWS Systems Manager 用户指南*中的[为混合环境安装 SSM Agent (Linux)](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-managedinstances.html#sysman-install-managed-linux)。

有关在运行 Windows Server 的服务器上更新 SSM Agent 的信息，请参阅 *AWS Systems Manager 用户指南*中的[为混合环境安装 SSM Agent (Windows)](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-managedinstances.html#sysman-install-managed-win)。

**使用 SSM Agent 在本地部署服务器上下载 CloudWatch 代理软件包**

1. 通过 [https://console.aws.amazon.com/systems-manager/](https://console.aws.amazon.com/systems-manager/) 打开 Systems Manager 控制台。

1. 在导航窗格中，选择 **Run Command**。

   –或者–

   如果打开了 AWS Systems Manager 主页，请向下滚动并选择 **Explore Run Command（浏览 Run Command）**。

1. 选择 **Run command（运行命令）**。

1. 在 **Command document（命令文档）**列表中，选择 **AWS-ConfigureAWSPackage** 旁边的按钮。

1. 在 **Targets（目标）**区域中，选择要在其上安装 CloudWatch 代理的服务器。如果未看到特定的服务器，则可能没有为 Run Command 配置该服务器。有关更多信息，请参阅 *AWS Systems Manager 用户指南*中的[为混合环境设置 AWS Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-managedinstances.html)。

1. 在**操作**列表中，选择**安装**。

1. 在 **Name (名称)** 框中，输入 *AmazonCloudWatchAgent*。

1. 将 **Version (版本)** 保留空白以安装最新版本的代理。

1. 选择**运行**。

   将下载代理软件包，后续步骤是配置并启动该代理。

## （安装在本地部署服务器上）指定 IAM 凭证和 AWS 区域
<a name="install-CloudWatch-Agent-iam_user-SSM-onprem"></a>

要使 CloudWatch 代理能够从本地部署服务器发送数据，您必须指定先前创建的 IAM 用户的访问密钥和私有密钥。

您还必须使用 `region` 字段指定要将指标发送到的 AWS 区域。

以下为该文件的示例。

```
[AmazonCloudWatchAgent]
aws_access_key_id=my_access_key
aws_secret_access_key=my_secret_key
region = us-west-1
```

对于 *my\$1access\$1key* 和 *my\$1secret\$1key*，请使用来自没有写入到 Systems Manager Parameter Store 权限的 IAM 用户的密钥。

如果您将此配置文件命名为 `AmazonCloudWatchAgent`，则无需执行任何操作。（可选）您可以为其指定一个不同的名称，并将该名称指定为 ` common-config.toml` 文件中的 `shared_credential_profile` 值（将在下一节中介绍）。

下面是使用 **aws configure** 命令为 CloudWatch 代理创建命名配置文件的示例。该示例假设您使用默认配置文件名称 `AmazonCloudWatchAgent`。

**为 CloudWatch 代理创建 AmazonCloudWatchAgent 配置文件**

1. 如果您还没有这样做，请在服务器上安装 AWS Command Line Interface。有关更多信息，请参阅[安装 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html)。

1. 在 Linux 服务器上，输入以下命令并按照提示进行操作：

   ```
   sudo aws configure --profile AmazonCloudWatchAgent
   ```

   在 Windows Server 上，以管理员身份打开 PowerShell，输入以下命令并按照提示进行操作。

   ```
   aws configure --profile AmazonCloudWatchAgent
   ```

## （可选）修改 CloudWatch 代理的通用配置和命名配置文件
<a name="CloudWatch-Agent-profile-onprem"></a>

CloudWatch 代理包含一个名为 `common-config.toml` 的配置文件。您可以（可选）使用该文件指定代理和区域信息。

在运行 Linux 的服务器上，该文件位于 `/opt/aws/amazon-cloudwatch-agent/etc` 目录中。在运行 Windows Server 的服务器上，该文件位于 `C:\ProgramData\Amazon\AmazonCloudWatchAgent` 目录中。

默认 `common-config.toml` 如下所示：

```
# This common-config is used to configure items used for both ssm and cloudwatch access
 
 
## Configuration for shared credential.
## Default credential strategy will be used if it is absent here:
##            Instance role is used for EC2 case by default.
##            AmazonCloudWatchAgent profile is used for onPremise case by default.
# [credentials]
#    shared_credential_profile = "{profile_name}"
#    shared_credential_file= "{file_name}"
 
## Configuration for proxy.
## System-wide environment-variable will be read if it is absent here.
## i.e. HTTP_PROXY/http_proxy; HTTPS_PROXY/https_proxy; NO_PROXY/no_proxy
## Note: system-wide environment-variable is not accessible when using ssm run-command.
## Absent in both here and environment-variable means no proxy will be used.
# [proxy]
#    http_proxy = "{http_url}"
#    https_proxy = "{https_url}"
#    no_proxy = "{domain}"
```

最初将注释所有行。要设置凭证配置文件或代理设置，请从该行中删除 `#` 并指定一个值。您可以手动编辑该文件，或者使用 Systems Manager 中的 `RunShellScript` Run Command 执行该操作：
+ `shared_credential_profile` – 对于本地部署服务器，此行指定 IAM 用户凭证配置文件，以用于将数据发送到 CloudWatch。如果您将此行注释掉，则会使用 `AmazonCloudWatchAgent`。有关创建此配置文件的更多信息，请参阅[（安装在本地部署服务器上）指定 IAM 凭证和 AWS 区域](#install-CloudWatch-Agent-iam_user-SSM-onprem)。

  在 EC2 实例上，您可以使用此行让 CloudWatch 代理将数据从该实例发送到不同 AWS 区域中的 CloudWatch。要执行此操作，请指定一个包含 `region` 字段的命名配置文件，该字段指定要发送到的区域的名称。

  如果指定 `shared_credential_profile`，您还必须从 `[credentials]` 行开头删除 `#`。
+ `shared_credential_file` – 要让代理在位于默认路径以外的路径中的文件中查找凭证，请在此处指定完整的路径和文件名。在 Linux 上，默认路径为 `/root/.aws`；在 Windows Server 上，默认路径为 `C:\\Users\\Administrator\\.aws`。

  下面的第一个示例显示对 Linux 服务器有效的 `shared_credential_file` 行的语法，第二个示例对 Windows Server 有效。在 Windows Server 上，您必须转义 \$1 字符。

  ```
  shared_credential_file= "/usr/username/credentials"
  ```

  ```
  shared_credential_file= "C:\\Documents and Settings\\username\\.aws\\credentials"
  ```

  如果指定 `shared_credential_file`，您还必须从 `[credentials]` 行开头删除 `#`。
+ 代理设置 – 如果您的服务器使用 HTTP 或 HTTPS 代理联系 AWS 服务，请在 `http_proxy` 和 `https_proxy` 字段中指定这些代理。如果应从代理中排除某些 URL，请在 `no_proxy` 字段中指定这些 URL 并以逗号分隔。