

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用命令列安裝、設定及管理應用程式以進行監控
<a name="appinsights-setting-up-command"></a>

本節提供設定、設定和管理應用程式的步驟，以便使用 AWS CLI 和 進行監控 AWS 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>

**使用 新增應用程式 AWS CLI**  
若要使用 AWS CLI 為名為 的資源群組新增應用程式`my-resource-group`，並啟用 OpsCenter 將建立的 opsItem 交付至 SNS 主題 ARN ` arn:aws:sns:us-east-1:123456789012:MyTopic`，請使用下列命令。

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

**使用 新增應用程式 AWS Tools for Windows PowerShell**  
若要使用 AWS Tools for Windows PowerShell 為已啟用 `my-resource-group` OpsCenter 的資源群組新增應用程式，以將建立的 opsItem 交付至 SNS 主題 ARN `arn:aws:sns:us-east-1:123456789012:MyTopic`，請使用下列命令。

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

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

**使用 描述應用程式 AWS CLI**  
若要使用 AWS CLI 描述在名為 的資源群組上建立的應用程式`my-resource-group`，請使用下列命令。

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

**使用 描述應用程式 AWS Tools for Windows PowerShell**  
若要使用 AWS Tools for Windows PowerShell 描述在名為 的資源群組上建立的應用程式`my-resource-group`，請使用下列命令。

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

### 列出應用程式中的元件
<a name="appinsights-list-components"></a>

**使用 列出應用程式中的元件 AWS CLI**  
若要使用 AWS CLI 列出在名為 的資源群組上建立的元件`my-resource-group`，請使用下列命令。

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

**使用 列出應用程式中的元件 AWS Tools for Windows PowerShell**  
若要使用 AWS Tools for Windows PowerShell 列出在名為 的資源群組上建立的元件`my-resource-group`，請使用下列命令。

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

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

**使用 描述元件 AWS CLI**  
您可以使用下列 AWS CLI 命令來描述名為 的元件`my-component`，該元件屬於在名為 的資源群組上建立的應用程式`my-resource-group`。

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

**使用 描述元件 AWS Tools for Windows PowerShell**  
您可以使用下列 AWS Tools for Windows PowerShell 命令來描述名為 的元件`my-component`，該元件屬於在名為 的資源群組上建立的應用程式`my-resource-group`。

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

### 將類似資源分組到自訂元件
<a name="appinsights-group-resources-components"></a>

建議您將類似的資源 (例如 .NET Web 伺服器執行個體) 分組到自訂元件，以實現更輕鬆的安裝和更好的監控與洞見。CloudWatch Application Insights 支援 EC2 執行個體的自訂群組。

**使用 將資源分組到自訂元件 AWS CLI**  
若要使用 AWS CLI 將三個執行個體 (`arn:aws:ec2:us-east-1:123456789012:instance/i-11111`、 `arn:aws:ec2:us-east-1:123456789012:instance/i-22222`和 `arn:aws:ec2:us-east-1:123456789012:instance/i-33333`) 分組為`my-component`針對名為 的資源群組所建立之應用程式的自訂元件`my-resource-group`，請使用下列命令。

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

**使用 將資源分組到自訂元件 AWS Tools for Windows PowerShell**  
若要使用 AWS Tools for Windows PowerShell 將三個執行個體 (`arn:aws:ec2:us-east-1:123456789012:instance/i-11111`、 `arn:aws:ec2:us-east-1:123456789012:instance/i-22222`和 `arn:aws:ec2:us-east-1:123456789012:instance/i-33333`) 分組為名為 的自訂元件`my-component`，對於為名為 的資源群組建立的應用程式`my-resource-group`，請使用下列命令。

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

### 取消群組自訂元件
<a name="appinsights-ungroup-resources-components"></a>

**使用 取消自訂元件的群組 AWS CLI**  
若要使用 在資源群組`my-component`上建立的應用程式中 AWS CLI 取消群組名為 的自訂元件，`my-resource-group`請使用下列命令。

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

**使用 取消自訂元件的群組 AWS Tools for Windows PowerShell**  
若要使用 在資源群組`my-component`上建立的應用程式中 AWS Tools for Windows PowerShell 取消群組名為 的自訂元件，`my-resource-group`請使用下列命令。

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

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

**使用 更新應用程式 AWS CLI**  
您可以使用 AWS CLI 更新應用程式，針對應用程式偵測到的問題產生 AWS Systems Manager OpsCenter OpsItems，並使用`arn:aws:sns:us-east-1:123456789012:MyTopic`下列命令將建立的 OpsItems 與 SNS 主題 建立關聯。

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

**使用 AWS Tools for Windows PowerShell 更新應用程式**  
您可以使用 AWS Tools for Windows PowerShell 更新應用程式，針對應用程式偵測到的問題產生 AWS SSM OpsCenter OpsItems，並使用下列命令將建立的 OpsItems 與 SNS 主題 `arn:aws:sns:us-east-1:123456789012:MyTopic` 建立關聯。

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

### 更新自訂元件
<a name="appinsights-update-component"></a>

**使用 更新自訂元件 AWS CLI**  
您可以使用下列命令，使用`my-component`新的元件名稱 `my-new-component`和更新的執行個體群組來 AWS CLI 更新名為 的自訂元件。

```
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:ec2:us-east-1:123456789012:instance/i-44444 arn:aws:ec2:us-east-1:123456789012:instance/i-55555
```

**使用 AWS Tools for Windows PowerShell 更新自訂元件**  
您可以使用下列命令，使用`my-component`新的元件名稱 `my-new-component`和更新的執行個體群組來 AWS Tools for Windows PowerShell 更新名為 的自訂元件。

```
Update-CWAIComponent -ComponentName my-component -NewComponentName my-new-component -ResourceGroupName my-resource-group -ResourceList arn:aws:ec2:us-east-1:123456789012:instance/i-44444,arn:aws:ec2:us-east-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>

**使用 列出應用程式的問題 AWS CLI**  
若要針對在名為 的資源群組上建立的應用程式，使用 AWS CLI 列出自 Unix Epoch 起 1，000 到 10，000 毫秒之間偵測到的應用程式問題`my-resource-group`，請使用下列命令。

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

**使用 AWS Tools for Windows PowerShell 列出應用程式的問題**  
若要針對在名為 的資源群組上建立的應用程式，使用 AWS Tools for Windows PowerShell 列出自 Unix Epoch 起 1，000 到 10，000 毫秒之間偵測到的應用程式問題`my-resource-group`，請使用下列命令。

```
$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>

**使用 描述應用程式問題 AWS CLI**  
若要使用 AWS CLI 描述問題 ID 為 的問題`p-1234567890`，請使用下列命令。

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

**使用 AWS Tools for Windows PowerShell 描述應用程式問題**  
若要使用 AWS Tools for Windows PowerShell 描述問題 ID 為 的問題`p-1234567890`，請使用下列命令。

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

### 描述與問題相關聯的異常或錯誤
<a name="appinsights-describe-anomalies"></a>

**使用 描述與問題相關的異常或錯誤 AWS CLI**  
若要使用 AWS CLI 來描述與問題 ID 為 的問題相關的異常或錯誤`p-1234567890`，請使用下列命令。

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

**使用 描述與問題相關的異常或錯誤 AWS Tools for Windows PowerShell**  
若要使用 AWS Tools for Windows PowerShell 來描述與問題 ID 為 的問題相關的異常或錯誤`p-1234567890`，請使用下列命令。

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

### 描述應用程式的異常或錯誤
<a name="appinsights-describe-anomalies"></a>

**使用 CLI AWS 描述應用程式的異常或錯誤**  
若要使用 AWS CLI 來描述觀察 ID 為 的應用程式異常或錯誤`o-1234567890`，請使用下列命令。

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

**使用 AWS Tools for Windows PowerShell 描述應用程式的異常或錯誤**  
若要使用 AWS Tools for Windows PowerShell 來描述觀察 ID 為 的應用程式異常或錯誤`o-1234567890`，請使用下列命令。

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

### 描述元件的監控組態
<a name="appinsights-describe-monitoring"></a>

**使用 描述元件的監控組態 AWS CLI**  
若要使用 AWS CLI 來描述`my-component`在資源群組 上建立的應用程式中名為 之元件的監控組態`my-resource-group`，請使用下列命令。

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

**使用 AWS Tools for Windows PowerShell 描述元件的監控組態**  
若要使用 AWS Tools for Windows PowerShell 來描述名為 之元件的監控組態`my-component`，請在資源群組 上建立的應用程式`my-resource-group`中使用下列命令。

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

如需元件組態的詳細資訊和範例 JSON 檔案，請參閱 [使用元件組態](component-config.md)。

### 描述元件的建議監控組態
<a name="appinsights-describe-rec-monitoring"></a>

**使用 描述元件的建議監控組態 AWS CLI**  
當元件是 .NET 工作者應用程式的一部分時，您可以使用下列命令`my-resource-group`， AWS CLI 描述`my-component`在資源群組 上建立的應用程式中所呼叫元件的建議監控組態。

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

**使用 描述元件的建議監控組態 AWS Tools for Windows PowerShell**  
當元件是 .NET 工作者應用程式的一部分時，您可以使用下列命令`my-resource-group`， AWS Tools for Windows PowerShell 描述`my-component`在資源群組 上建立的應用程式中所呼叫元件的建議監控組態。

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

如需元件組態的詳細資訊和範例 JSON 檔案，請參閱 [使用元件組態](component-config.md)。

### 更新元件的監控組態
<a name="update-monitoring"></a>

**使用 更新元件的監控組態 AWS CLI**  
若要使用 AWS CLI 在名為 的資源群組上建立`my-component`的應用程式中更新名為 的元件`my-resource-group`，請使用下列命令。命令包含這些動作：

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"
```

**使用 更新元件的監控組態 AWS Tools for Windows PowerShell**  
若要使用 AWS Tools for Windows PowerShell 在名為 的資源群組上建立`my-component`的應用程式中更新名為 的元件`my-resource-group`，請使用下列命令。命令包含這些動作：

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>

**使用 從 Application Insights 監控中移除指定的資源群組 AWS CLI**  
若要使用 AWS CLI 從監控中移除在名為 `my-resource-group` 的資源群組上建立的應用程式，請使用下列命令。

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

**使用 從 Application Insights 監控中移除指定的資源群組 AWS Tools for Windows PowerShell**  
若要使用 AWS 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:ec2:<REGION>:<ACCOUNT_ID>:instance/<CLUSTER_INSTANCE_1_ID>" "arn:aws: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.aws.amazon.com/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.aws.amazon.com/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.aws.amazon.com/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. 為資源群組建立具有 PostgreSQL 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,
         }
      ]
   }"
   ```