

# 使用命令行设置、配置和管理您的应用程序以进行监控
<a name="appinsights-setting-up-command"></a>

本节提供设置、配置和管理应用程序以使用 Amazon CLI 和 Amazon Tools for Windows PowerShell 进行监控的步骤。

**Topics**
+ [添加和管理应用程序](#appinsights-config-app-command)
+ [管理和更新监控](#appinsights-monitoring)
+ [针对 SQL Always On 可用性组配置监控](#configure-sql)
+ [为 MySQL RDS 配置监控](#configure-mysql-rds)
+ [为 MySQL EC2 配置监控](#configure-mysql-ec2)
+ [为 PostgreSQL RDS 配置监控](#configure-postgresql-rds)
+ [为 PostgreSQL EC2 配置监控](#configure-postgresql-ec2)
+ [为 Oracle RDS 配置监控](#configure-oracle-rds)
+ [为 Oracle EC2 配置监控](#configure-oracle-ec2)

## 添加和管理应用程序
<a name="appinsights-config-app-command"></a>

您可以使用命令行添加、管理和配置 Application Insights 应用程序以及获取有关它的信息。

**Topics**
+ [添加应用程序](#appinsights-add-app)
+ [描述应用程序](#appinsights-describe-app)
+ [列出应用程序中的组件](#appinsights-list-components)
+ [描述组件](#appinsights-describe-components)
+ [将类似资源分组到自定义组件中](#appinsights-group-resources-components)
+ [对自定义组件取消分组](#appinsights-ungroup-resources-components)
+ [更新 应用程序](#appinsights-update-app)
+ [更新自定义组件](#appinsights-update-component)

### 添加应用程序
<a name="appinsights-add-app"></a>

**使用 Amazon CLI 添加应用程序**  
要使用 Amazon CLI 为名为 `my-resource-group` 的资源组添加应用程序，并启用 OpsCenter 以将创建的 opsItem 传递到 SNS 主题 ARN ` arn-cn:aws:sns:cn-north-1:123456789012:MyTopic`，请使用以下命令。

```
aws application-insights create-application --resource-group-name my-resource-group --ops-center-enabled --ops-item-sns-topic-arn arn-cn:aws:sns:cn-north-1:123456789012:MyTopic
```

**使用 Amazon Tools for Windows PowerShell 添加应用程序**  
要使用 Amazon Tools for Windows PowerShell 为名为 `my-resource-group` 的资源组添加应用程序，并启用 OpsCenter 以将创建的 opsItem 传递到 SNS 主题 ARN `arn:aws:sns:cn-north-1:123456789012:MyTopic`，请使用以下命令。

```
New-CWAIApplication -ResourceGroupName my-resource-group -OpsCenterEnabled true -OpsItemSNSTopicArn arn-cn:aws:sns:cn-north-1:123456789012:MyTopic
```

### 描述应用程序
<a name="appinsights-describe-app"></a>

**使用 Amazon CLI 描述应用程序**  
要使用 Amazon CLI 描述在名为 `my-resource-group` 的资源组上创建的应用程序，请使用以下命令。

```
aws application-insights describe-application --resource-group-name my-resource-group
```

**使用 Amazon Tools for Windows PowerShell 描述应用程序**  
要使用 Amazon Tools for Windows PowerShell 描述在名为 `my-resource-group` 的资源组上创建的应用程序，请使用以下命令。

```
Get-CWAIApplication -ResourceGroupName my-resource-group
```

### 列出应用程序中的组件
<a name="appinsights-list-components"></a>

**使用 Amazon CLI 列出应用程序中的组件**  
要使用 Amazon CLI 列出在名为 `my-resource-group` 的资源组上创建的组件，请使用以下命令。

```
aws application-insights list-components --resource-group-name my-resource-group
```

**使用 Amazon Tools for Windows PowerShell 列出应用程序中的组件**  
要使用 Amazon Tools for Windows PowerShell 列出在名为 `my-resource-group` 的资源组上创建的组件，请使用以下命令。

```
Get-CWAIComponentList -ResourceGroupName my-resource-group
```

### 描述组件
<a name="appinsights-describe-components"></a>

**使用 Amazon CLI 描述组件**  
您可以使用以下 Amazon CLI 命令描述属于在名为 `my-resource-group` 的资源组上创建的应用程序且名为 `my-component` 的组件。

```
aws application-insights describe-component --resource-group-name my-resource-group --component-name my-component
```

**使用 Amazon Tools for Windows PowerShell 描述组件**  
您可以使用以下 Amazon Tools for Windows PowerShell 命令描述属于在名为 `my-resource-group` 的资源组上创建的应用程序且名为 `my-component` 的组件。

```
Get-CWAIComponent -ComponentName my-component -ResourceGroupName my-resource-group
```

### 将类似资源分组到自定义组件中
<a name="appinsights-group-resources-components"></a>

我们建议将类似的资源（如 .NET Web 服务器实例）分组到自定义组件中，以便简化接入流程并获取更优质的监控与洞察效果。CloudWatch Application Insights 支持 EC2 实例的自定义组。

**使用 Amazon CLI 将资源分组到自定义组件**  
要为名为 `my-resource-group` 的资源组创建的应用程序使用 Amazon CLI 将三个实例（`arn-cn:aws:ec2:cn-north-1:123456789012:instance/i-11111`、`arn-cn:aws:ec2:cn-north-1:123456789012:instance/i-22222` 和 `arn-cn:aws:ec2:cn-north-1:123456789012:instance/i-33333`）一起分组到一个名为 `my-component` 的自定义组件，请使用以下命令。

```
aws application-insights create-component --resource-group-name my-resource-group --component-name my-component --resource-list arn:aws-cn:ec2:cn-north-1:123456789012:instance/i-11111 arn:aws-cn:ec2:cn-north-1:123456789012:instance/i-22222 arn:aws-cn:ec2:cn-north-1:123456789012:instance/i-33333
```

**使用 Amazon Tools for Windows PowerShell 将资源分组到自定义组件**  
要为名为 `my-resource-group` 的资源组创建的应用程序使用 Amazon Tools for Windows PowerShell 将三个实例（`arn:aws-cn:ec2:cn-north-1:123456789012:instance/i-11111`、`arn:aws-cn:ec2:cn-north-1:123456789012:instance/i-22222` 和 `arn:aws-cn:ec2:cn-north-1:123456789012:instance/i-33333`）一起分组到一个名为 `my-component` 的自定义组件，请使用以下命令。

```
New-CWAIComponent -ResourceGroupName my-resource-group -ComponentName my-component -ResourceList arn:aws-cn:ec2:cn-north-1:123456789012:instance/i-11111,arn:aws-cn:ec2:cn-north-1:123456789012:instance/i-22222,arn:aws-cn:ec2:cn-north-1:123456789012:instance/i-33333 
```

### 对自定义组件取消分组
<a name="appinsights-ungroup-resources-components"></a>

**使用 Amazon CLI 对自定义组件取消分组**  
要使用 Amazon CLI 对在资源组 `my-resource-group` 上创建的应用程序中名为 `my-component` 的自定义组件取消分组，请使用以下命令。

```
aws application-insights delete-component --resource-group-name my-resource-group --component-name my-new-component
```

**使用 Amazon Tools for Windows PowerShell 对自定义组件取消分组**  
要使用 Amazon Tools for Windows PowerShell 对在资源组 `my-resource-group` 上创建的应用程序中名为 `my-component` 的自定义组件取消分组，请使用以下命令。

```
Remove-CWAIComponent -ComponentName my-component -ResourceGroupName my-resource-group
```

### 更新 应用程序
<a name="appinsights-update-app"></a>

**使用 Amazon CLI 更新应用程序**  
您可以使用 Amazon CLI 更新应用程序，以针对在应用程序中检测到的问题生成 Amazon Systems Manager OpsCenter OpsItem，并使用以下命令将创建的 OpsItem 与 SNS 主题 `arn:aws-cn:sns:cn-north-1:123456789012:MyTopic` 相关联。

```
aws application-insights update-application --resource-group-name my-resource-group --ops-center-enabled --ops-item-sns-topic-arn arn:aws-cn:sns:cn-north-1:123456789012:MyTopic
```

**使用 Amazon Tools for Windows PowerShell 更新应用程序**  
您可以使用 Amazon Tools for Windows PowerShell 更新应用程序，以针对在应用程序中检测到的问题生成 Amazon SSM OpsCenter OpsItem，并使用以下命令将创建的 OpsItem 与 SNS 主题 相关联。

```
Update-CWAIApplication -ResourceGroupName my-resource-group -OpsCenterEnabled true -OpsItemSNSTopicArn arn:aws-cn:sns:cn-north-1:123456789012:MyTopic
```

### 更新自定义组件
<a name="appinsights-update-component"></a>

**使用 Amazon CLI 更新自定义组件**  
您可以使用 Amazon CLI，通过使用以下命令，使用新的组件名称 `my-new-component` 和更新的实例组来更新名为 `my-component` 的自定义组件。

```
aws application-insights update-component --resource-group-name my-resource-group --component-name my-component --new-component-name my-new-component --resource-list arn:aws-cn:ec2:cn-north-1:123456789012:instance/i-44444 arn:aws-cn:ec2:cn-north-1:123456789012:instance/i-55555
```

**使用 Amazon Tools for Windows PowerShell 更新自定义组件**  
您可以使用 Amazon Tools for Windows PowerShell，通过使用以下命令，使用新的组件名称 `my-new-component` 和更新的实例组来更新名为 `my-component` 的自定义组件。

```
Update-CWAIComponent -ComponentName my-component -NewComponentName my-new-component -ResourceGroupName my-resource-group -ResourceList arn:aws-cn:ec2:cn-north-1:123456789012:instance/i-44444,arn:aws-cn:ec2:cn-north-1:123456789012:instance/i-55555
```

## 管理和更新监控
<a name="appinsights-monitoring"></a>

您可以使用命令行管理和更新针对 Application Insights 应用程序的监控。

**Topics**
+ [列出应用程序的问题](#appinsights-list-problems-monitoring)
+ [描述应用程序问题](#appinsights-describe-app-problem)
+ [描述与问题关联的异常或错误](#appinsights-describe-anomalies)
+ [描述应用程序的异常或错误](#appinsights-describe-anomalies)
+ [描述组件的监控配置](#appinsights-describe-monitoring)
+ [描述建议的组件监控配置](#appinsights-describe-rec-monitoring)
+ [更新组件的监控配置](#update-monitoring)
+ [对指定的资源组取消 Application Insights 监控](#update-monitoring)

### 列出应用程序的问题
<a name="appinsights-list-problems-monitoring"></a>

**使用 Amazon CLI 列出应用程序的问题**  
要使用 Amazon CLI 列出自 Unix Epoch 以来针对在名为 `my-resource-group` 的资源组上创建的应用程序在 1000 到 10000 毫秒之间检测到的应用程序问题，请使用以下命令。

```
aws application-insights list-problems --resource-group-name my-resource-group --start-time 1000 --end-time 10000
```

**使用 Amazon Tools for Windows PowerShell 列出应用程序的问题**  
要使用 Amazon Tools for Windows PowerShell 列出自 Unix Epoch 以来针对在名为 `my-resource-group` 的资源组上创建的应用程序在 1000 到 10000 毫秒之间检测到的应用程序问题，请使用以下命令。

```
$startDate = "8/6/2019 3:33:00"
$endDate = "8/6/2019 3:34:00"
Get-CWAIProblemList -ResourceGroupName my-resource-group -StartTime $startDate -EndTime $endDate
```

### 描述应用程序问题
<a name="appinsights-describe-app-problem"></a>

**使用 Amazon CLI 描述应用程序问题**  
要使用 Amazon CLI 描述问题 ID 为 `p-1234567890` 的问题，请使用以下命令。

```
aws application-insights describe-problem —problem-id p-1234567890
```

**使用 Amazon Tools for Windows PowerShell 描述应用程序问题**  
要使用 Amazon Tools for Windows PowerShell 描述问题 ID 为 `p-1234567890` 的问题，请使用以下命令。

```
Get-CWAIProblem -ProblemId p-1234567890
```

### 描述与问题关联的异常或错误
<a name="appinsights-describe-anomalies"></a>

**使用 Amazon CLI 描述与问题关联的异常或错误**  
要使用 Amazon CLI 描述与问题 ID 为 `p-1234567890` 的问题关联的异常或错误，请使用以下命令。

```
aws application-insights describe-problem-observations --problem-id -1234567890
```

**使用 Amazon Tools for Windows PowerShell 描述与问题关联的异常或错误**  
要使用 Amazon Tools for Windows PowerShell 描述与问题 ID 为 `p-1234567890` 的问题关联的异常或错误，请使用以下命令。

```
Get-CWAIProblemObservation -ProblemId p-1234567890
```

### 描述应用程序的异常或错误
<a name="appinsights-describe-anomalies"></a>

**使用 Amazon CLI 描述应用程序的异常或错误**  
要使用 Amazon CLI 描述观察 ID 为 `o-1234567890` 的应用程序的异常或错误，请使用以下命令。

```
aws application-insights describe-observation —observation-id o-1234567890
```

**使用 Amazon Tools for Windows PowerShell描述应用程序的异常或错误**  
要使用 Amazon Tools for Windows PowerShell 描述观察 ID 为 `o-1234567890` 的应用程序的异常或错误，请使用以下命令。

```
Get-CWAIObservation -ObservationId o-1234567890
```

### 描述组件的监控配置
<a name="appinsights-describe-monitoring"></a>

**使用 Amazon CLI 组件描述组件的监控配置**  
要使用 Amazon CLI 描述在资源组 `my-resource-group` 上创建的应用程序中名为 `my-component` 的组件的监控配置，请使用以下命令。

```
aws application-insights describe-component-configuration —resource-group-name my-resource-group —component-name my-component
```

**使用 Amazon Tools for Windows PowerShell 描述组件的监控配置**  
要使用 Amazon Tools for Windows PowerShell 描述在资源组 `my-resource-group` 上创建的应用程序中名为 `my-component` 的组件的监控配置，请使用以下命令。

```
Get-CWAIComponentConfiguration -ComponentName my-component -ResourceGroupName my-resource-group
```

有关组件配置和示例 JSON 文件的更多信息，请参阅[使用组件配置](component-config.md)。

### 描述建议的组件监控配置
<a name="appinsights-describe-rec-monitoring"></a>

**使用 Amazon CLI 描述建议的组件监控配置**  
当组件是 .NET Worker 应用程序的一部分时，您可以使用 Amazon CLI，通过使用以下命令来描述在资源组 `my-resource-group` 上创建的应用程序中名为 `my-component` 的组件的建议监控配置。

```
aws application-insights describe-component-configuration-recommendation --resource-group-name my-resource-group --component-name my-component --tier DOT_NET_WORKER
```

**使用 Amazon Tools for Windows PowerShell 描述建议的组件监控配置**  
当组件是 .NET Worker 应用程序的一部分时，您可以使用 Amazon Tools for Windows PowerShell，通过使用以下命令来描述在资源组 `my-resource-group` 上创建的应用程序中名为 `my-component` 的组件的建议监控配置。

```
Get-CWAIComponentConfigurationRecommendation -ComponentName my-component -ResourceGroupName my-resource-group -Tier DOT_NET_WORKER
```

有关组件配置和示例 JSON 文件的更多信息，请参阅[使用组件配置](component-config.md)。

### 更新组件的监控配置
<a name="update-monitoring"></a>

**使用 Amazon CLI 更新组件的监控配置**  
要使用 Amazon CLI 更新在名为 `my-resource-group` 的资源组上创建的应用程序中名为 `my-component` 的组件，请使用以下命令。该命令包括以下操作：

1. 启用组件监控。

1. 将组件层设置为 `.NET Worker`。

1. 更新组件的 JSON 配置以从本地文件 `configuration.txt` 中读取。

```
aws application-insights update-component-configuration --resource-group-name my-resource-group --component-name my-component --tier DOT_NET_WORKER --monitor --component-configuration "file://configuration.txt"
```

**使用 Amazon Tools for Windows PowerShell 更新组件的监控配置**  
要使用 Amazon Tools for Windows PowerShell 更新在名为 `my-resource-group` 的资源组上创建的应用程序中名为 `my-component` 的组件，请使用以下命令。该命令包括以下操作：

1. 启用组件监控。

1. 将组件层设置为 `.NET Worker`。

1. 更新组件的 JSON 配置以从本地文件 `configuration.txt` 中读取。

```
[string]$config = Get-Content -Path configuration.txt
Update-CWAIComponentConfiguration -ComponentName my-component -ResourceGroupName my-resource-group -Tier DOT_NET_WORKER -Monitor 1 -ComponentConfiguration $config
```

有关组件配置和示例 JSON 文件的更多信息，请参阅[使用组件配置](component-config.md)。

### 对指定的资源组取消 Application Insights 监控
<a name="update-monitoring"></a>

**使用 Amazon CLI 对指定的资源组取消 Application Insights 监控**  
要使用 Amazon CLI 对在名为 `my-resource-group` 的资源组上创建的应用程序取消监控，请使用以下命令。

```
aws application-insights delete-application --resource-group-name my-resource-group
```

**使用 Amazon Tools for Windows PowerShell 对指定的资源组取消 Application Insights 监控**  
要使用 Amazon Tools for Windows PowerShell 对在名为 `my-resource-group` 的资源组上创建的应用程序取消监控，请使用以下命令。

```
Remove-CWAIApplication -ResourceGroupName my-resource-group
```

## 针对 SQL Always On 可用性组配置监控
<a name="configure-sql"></a>

1. 使用 SQL HA EC2 实例为资源组创建应用程序。

   ```
   aws application-insights create-application ‐-region <REGION> ‐-resource-group-name  <RESOURCE_GROUP_NAME>
   ```

1. 通过创建新的应用程序组件，定义表示 SQL HA 集群的 EC2 实例。

   ```
   aws application-insights create-component ‐-resource-group-name  "<RESOURCE_GROUP_NAME>" ‐-component-name SQL_HA_CLUSTER ‐-resource-list  "arn:aws-cn:ec2:<REGION>:<ACCOUNT_ID>:instance/<CLUSTER_INSTANCE_1_ID>" "arn:aws-cn:ec2:<REGION>:<ACCOUNT_ID>:instance/<CLUSTER_INSTANCE_2_ID>
   ```

1. 配置 SQL HA 组件。

   ```
   aws application-insights  update-component-configuration ‐-resource-group-name "<RESOURCE_GROUP_NAME>" ‐-region <REGION> ‐-component-name "SQL_HA_CLUSTER" ‐-monitor ‐-tier SQL_SERVER_ALWAYSON_AVAILABILITY_GROUP ‐-monitor  ‐-component-configuration '{
     "subComponents" : [ {
       "subComponentType" : "AWS::EC2::Instance",
       "alarmMetrics" : [ {
         "alarmMetricName" : "CPUUtilization",
         "monitor" : true
       }, {
         "alarmMetricName" : "StatusCheckFailed",
         "monitor" : true
       }, {
         "alarmMetricName" : "Processor % Processor Time",
         "monitor" : true
       }, {
         "alarmMetricName" : "Memory % Committed Bytes In Use",
         "monitor" : true
       }, {
         "alarmMetricName" : "Memory Available Mbytes",
         "monitor" : true
       }, {
         "alarmMetricName" : "Paging File % Usage",
         "monitor" : true
       }, {
         "alarmMetricName" : "System Processor Queue Length",
         "monitor" : true
       }, {
         "alarmMetricName" : "Network Interface Bytes Total/sec",
         "monitor" : true
       }, {
         "alarmMetricName" : "PhysicalDisk % Disk Time",
         "monitor" : true
       }, {
         "alarmMetricName" : "SQLServer:Buffer Manager Buffer cache hit ratio",
         "monitor" : true
       }, {
         "alarmMetricName" : "SQLServer:Buffer Manager Page life expectancy",
         "monitor" : true
       }, {
         "alarmMetricName" : "SQLServer:General Statistics Processes blocked",
         "monitor" : true
       }, {
         "alarmMetricName" : "SQLServer:General Statistics User Connections",
         "monitor" : true
       }, {
         "alarmMetricName" : "SQLServer:Locks Number of Deadlocks/sec",
         "monitor" : true
       }, {
         "alarmMetricName" : "SQLServer:SQL Statistics Batch Requests/sec",
         "monitor" : true
       }, {
         "alarmMetricName" : "SQLServer:Database Replica File Bytes Received/sec",
         "monitor" : true
       }, {
         "alarmMetricName" : "SQLServer:Database Replica Log Bytes Received/sec",
         "monitor" : true
       }, {
         "alarmMetricName" : "SQLServer:Database Replica Log remaining for undo",
         "monitor" : true
       }, {
         "alarmMetricName" : "SQLServer:Database Replica Log Send Queue",
         "monitor" : true
       }, {
         "alarmMetricName" : "SQLServer:Database Replica Mirrored Write Transaction/sec",
         "monitor" : true
       }, {
         "alarmMetricName" : "SQLServer:Database Replica Recovery Queue",
         "monitor" : true
       }, {
         "alarmMetricName" : "SQLServer:Database Replica Redo Bytes Remaining",
         "monitor" : true
       }, {
         "alarmMetricName" : "SQLServer:Database Replica Redone Bytes/sec",
         "monitor" : true
       }, {
         "alarmMetricName" : "SQLServer:Database Replica Total Log requiring undo",
         "monitor" : true
       }, {
         "alarmMetricName" : "SQLServer:Database Replica Transaction Delay",
         "monitor" : true
       } ],
       "windowsEvents" : [ {
         "logGroupName" : "WINDOWS_EVENTS-Application-<RESOURCE_GROUP_NAME>",
         "eventName" : "Application",
         "eventLevels" : [ "WARNING", "ERROR", "CRITICAL", "INFORMATION" ],
         "monitor" : true
       }, {
         "logGroupName" : "WINDOWS_EVENTS-System-<RESOURCE_GROUP_NAME>",
         "eventName" : "System",
         "eventLevels" : [ "WARNING", "ERROR", "CRITICAL" ],
         "monitor" : true
       }, {
         "logGroupName" : "WINDOWS_EVENTS-Security-<RESOURCE_GROUP_NAME>",
         "eventName" : "Security",
         "eventLevels" : [ "WARNING", "ERROR", "CRITICAL" ],
         "monitor" : true
       } ],
       "logs" : [ {
         "logGroupName" : "SQL_SERVER_ALWAYSON_AVAILABILITY_GROUP-<RESOURCE_GROUP_NAME>",
         "logPath" : "C:\\Program Files\\Microsoft SQL Server\\MSSQL**.MSSQLSERVER\\MSSQL\\Log\\ERRORLOG",
         "logType" : "SQL_SERVER",
         "monitor" : true,
         "encoding" : "utf-8"
       } ]
     }, {
       "subComponentType" : "AWS::EC2::Volume",
       "alarmMetrics" : [ {
         "alarmMetricName" : "VolumeReadBytes",
         "monitor" : true
       }, {
       "alarmMetricName" : "VolumeWriteBytes",
         "monitor" : true
       }, {
       "alarmMetricName" : "VolumeReadOps",
         "monitor" : true
       }, {
       "alarmMetricName" : "VolumeWriteOps",
         "monitor" : true
       }, {
       "alarmMetricName" : "VolumeQueueLength",
         "monitor" : true
       }, {
       "alarmMetricName" : "VolumeThroughputPercentage",
         "monitor" : true
       }, {
       "alarmMetricName" : "BurstBalance",
         "monitor" : true
       } ]
     } ]
   }'
   ```

**注意**  
Application Insights 必须获取应用程序事件日志（信息级别）才能检测集群活动（如故障转移）。

## 为 MySQL RDS 配置监控
<a name="configure-mysql-rds"></a>

1. 使用 RDS MySQL 数据库实例为资源组创建应用程序。

   ```
   aws application-insights create-application ‐-region <REGION> ‐-resource-group-name  <RESOURCE_GROUP_NAME>
   ```

1. 默认情况下，启用错误日志。可以使用数据参数组启用慢速查询日志。有关更多信息，请参阅[访问 MySQL 慢速查询和常规日志](https://docs.amazonaws.cn/AmazonRDS/latest/UserGuide/USER_LogAccess.Concepts.MySQL.html#USER_LogAccess.MySQL.Generallog)。
   + `set slow_query_log = 1`
   + `set log_output = FILE`

1. 将要监控的日志导出到 CloudWatch Logs 中。有关更多信息，请参阅[将 MySQL 日志发布到 CloudWatch Logs](https://docs.amazonaws.cn/AmazonRDS/latest/UserGuide/USER_LogAccess.Concepts.MySQL.html#USER_LogAccess.MySQLDB.PublishtoCloudWatchLogs)。

1. 配置 MySQL RDS 组件。

   ```
   aws application-insights  update-component-configuration ‐-resource-group-name "<RESOURCE_GROUP_NAME>" ‐-region <REGION> ‐-component-name "<DB_COMPONENT_NAME>" ‐-monitor ‐-tier DEFAULT ‐-monitor  ‐-component-configuration "{\"alarmMetrics\":[{\"alarmMetricName\":\"CPUUtilization\",\"monitor\":true}],\"logs\":[{\"logType\":\"MYSQL\",\"monitor\":true},{\"logType\": \"MYSQL_SLOW_QUERY\",\"monitor\":false}]}"
   ```

## 为 MySQL EC2 配置监控
<a name="configure-mysql-ec2"></a>

1. 使用 SQL HA EC2 实例为资源组创建应用程序。

   ```
   aws application-insights create-application ‐-region <REGION> ‐-resource-group-name  <RESOURCE_GROUP_NAME>
   ```

1. 默认情况下，启用错误日志。可以使用数据参数组启用慢速查询日志。有关更多信息，请参阅[访问 MySQL 慢速查询和常规日志](https://docs.amazonaws.cn/AmazonRDS/latest/UserGuide/USER_LogAccess.Concepts.MySQL.html#USER_LogAccess.MySQL.Generallog)。
   + `set slow_query_log = 1`
   + `set log_output = FILE`

1. 配置 MySQL EC2 组件。

   ```
   aws application-insights  update-component-configuration ‐-resource-group-name "<RESOURCE_GROUP_NAME>" ‐-region <REGION> ‐-component-name "<DB_COMPONENT_NAME>" ‐-monitor ‐-tier MYSQL ‐-monitor  ‐-component-configuration "{\"alarmMetrics\":[{\"alarmMetricName\":\"CPUUtilization\",\"monitor\":true}],\"logs\":[{\"logGroupName\":\"<UNIQUE_LOG_GROUP_NAME>\",\"logPath\":\"C:\\\\ProgramData\\\\MySQL\\\\MySQL Server **\\\\Data\\\\<FILE_NAME>.err\",\"logType\":\"MYSQL\",\"monitor\":true,\"encoding\":\"utf-8\"}]}"
   ```

## 为 PostgreSQL RDS 配置监控
<a name="configure-postgresql-rds"></a>

1. 使用 Postgre RDS 数据库实例为资源组创建应用程序。

   ```
   aws application-insights create-application ‐-region <REGION> ‐-resource-group-name  <RESOURCE_GROUP_NAME>
   ```

1. 默认情况下，没有启用将 PostgreSQL 日志发布到 CloudWatch 的功能。要启用监控，请打开 RDS 控制台并选择要监控的数据库。选择右上角的 **Modify**（修改），然后选中标注为 **PostgreSQL** 日志的复选框。选择 **Continue（继续）**以保存此设置。

1. 您的 PostgreSQL 日志将导出到 CloudWatch。

1. 配置 PostgreSQL RDS 组件。

   ```
   aws application-insights update-component-configuration --region <REGION> --resource-group-name <RESOURCE_GROUP_NAME> --component-name <DB_COMPONENT_NAME> --monitor --tier DEFAULT --component-configuration 
   "{
      \"alarmMetrics\":[
         {
            \"alarmMetricName\": \"CPUUtilization\",
            \"monitor\": true
         }
      ],
      \"logs\":[
         {
            \"logType\": \"POSTGRESQL\",
            \"monitor\": true
         }
      ]
   }"
   ```

## 为 PostgreSQL EC2 配置监控
<a name="configure-postgresql-ec2"></a>

1. 使用 PostgreSQL EC2 实例为资源组创建应用程序。

   ```
   aws application-insights create-application ‐-region <REGION> ‐-resource-group-name  <RESOURCE_GROUP_NAME>
   ```

1. 配置 PostgreSQL EC2 组件。

   ```
   aws application-insights update-component-configuration ‐-region <REGION> ‐-resource-group-name <RESOURCE_GROUP_NAME> ‐-component-name <DB_COMPONENT_NAME> ‐-monitor ‐-tier POSTGRESQL ‐-component-configuration 
   "{
      \"alarmMetrics\":[
         {
            \"alarmMetricName\":\"CPUUtilization\",
            \"monitor\":true
         }
      ],
      \"logs\":[
         {
            \"logGroupName\":\"<UNIQUE_LOG_GROUP_NAME>\",
            \"logPath\":\"/var/lib/pgsql/data/log/\",
            \"logType\":\"POSTGRESQL\",
            \"monitor\":true,
            \"encoding\":\"utf-8\"
         }
      ]
   }"
   ```

## 为 Oracle RDS 配置监控
<a name="configure-oracle-rds"></a>

1. 使用 Oracle RDS 数据库实例为资源组创建应用程序。

   ```
   aws application-insights create-application ‐-region <REGION> ‐-resource-group-name  <RESOURCE_GROUP_NAME>
   ```

1. 默认情况下，没有启用将 Oracle 日志发布到 CloudWatch 的功能。要启用监控，请打开 RDS 控制台并选择要监控的数据库。选择右上角的 **Modify**（修改），然后选中标注为 **Alert**（提示）日志和 **Listener**（侦听器）日志的复选框。选择 **Continue（继续）**以保存此设置。

1. 您的 Oracle 日志将导出到 CloudWatch。

1. 配置 Oracle RDS 组件。

   ```
   aws application-insights update-component-configuration --region <REGION> --resource-group-name <RESOURCE_GROUP_NAME> --component-name <DB_COMPONENT_NAME> --monitor --tier DEFAULT --component-configuration 
   "{
      \"alarmMetrics\":[
         {
            \"alarmMetricName\": \"CPUUtilization\",
            \"monitor\": true
         }
      ],
      \"logs\":[
         {
            \"logType\": \"ORACLE_ALERT\",
            \"monitor\": true
         },
         {
            \"logType\": \"ORACLE_LISTENER\",
            \"monitor\": true
         }
      ]
   }"
   ```

## 为 Oracle EC2 配置监控
<a name="configure-oracle-ec2"></a>

1. 使用 Oracle EC2 实例为资源组创建应用程序。

   ```
   aws application-insights create-application ‐-region <REGION> ‐-resource-group-name  <RESOURCE_GROUP_NAME>
   ```

1. 配置 Oracle EC2 组件。

   ```
   aws application-insights update-component-configuration ‐-region <REGION> ‐-resource-group-name <RESOURCE_GROUP_NAME> ‐-component-name <DB_COMPONENT_NAME> ‐-monitor ‐-tier ORACLE ‐-component-configuration 
   "{
      \"alarmMetrics\":[
         {
            \"alarmMetricName\":\"CPUUtilization\",
            \"monitor\":true
         }
      ],
      \"logs\":[
         {
            \"logGroupName\":\"<UNIQUE_LOG_GROUP_NAME>\",
            \"logPath\":\"/opt/oracle/diag/rdbms/*/*/trace\",
            \"logType\":\"ORACLE_ALERT\",
            \"monitor\":true,
         },
         {
            \"logGroupName\":\"<UNIQUE_LOG_GROUP_NAME>\",
            \"logPath\":\"/opt/oracle/diag/tnslsnr/$HOSTNAME/listener/trace/\",
            \"logType\":\"ORACLE_ALERT\",
            \"monitor\":true,
         }
      ]
   }"
   ```