

# 自訂指標
<a name="dd-detect-custom-metrics"></a>

使用 AWS IoT Device Defender 自訂指標，您可以定義和監控機群或使用案例獨有的指標，例如連接到 Wi-Fi 閘道的裝置數量、電池電量，或智慧插頭的電源循環次數。自訂指標行為定義在安全性設定檔中，此設定檔會指定裝置群組 (物件群組) 或所有裝置的預期行為。您可以設定警示來監控行為，然後您可以使用這些警示，來偵測和回應裝置特有的問題。

**Topics**
+ [

## 如何在主控台中使用自訂指標
](#dd-detect-custom-metrics-how-to-console)
+ [

## 如何從 CLI 使用自訂指標
](#dd-detect-custom-metrics-how-to-cli)
+ [

## 自訂指標 CLI 命令
](#dd-detect-custom-metrics-cli-commands)
+ [

## 自訂指標 API
](#dd-detect-custom-metrics-apis)

## 如何在主控台中使用自訂指標
<a name="dd-detect-custom-metrics-how-to-console"></a>

**Topics**
+ [

### AWS IoT Device Defender Agent SDK (Python)
](#dd-detect-custom-metrics-device-agent)
+ [

### 建立自訂指標並將其新增至安全性設定檔
](#dd-detect-console-create)
+ [

### 檢視自訂指標詳細資訊
](#dd-detect-console-read)
+ [

### 更新自訂指標
](#dd-detect-console-edit)
+ [

### 刪除自訂指標
](#dd-detect-console-delete)

### AWS IoT Device Defender Agent SDK (Python)
<a name="dd-detect-custom-metrics-device-agent"></a>

若要開始使用，請下載 AWS IoT Device Defender Agent SDK (Python) 範例代理程式。此代理程式會收集指標並發佈報告。一旦發佈了裝置端指標，您就可以檢視所收集的指標，並判斷設定警示的閾值。設定裝置代理程式的指示可在 [AWS IoT Device Defender Agent SDK (Python) Readme](https://github.com/aws-samples/aws-iot-device-defender-agent-sdk-python/blob/master/README.rst) 上取得。如需詳細資訊，請參閱 [AWS IoT Device Defender Agent SDK (Python)](https://github.com/aws-samples/aws-iot-device-defender-agent-sdk-python)。

### 建立自訂指標並將其新增至安全性設定檔
<a name="dd-detect-console-create"></a>

下列程序顯示如何在主控台中建立自訂指標。

1. 在 [AWS IoT 主控台](https://console.aws.amazon.com/iot)的導覽窗格中，展開 **Defend** (防禦)，然後選擇 **Detect** (偵測)、**Metrics** (指標)。

1. 在 **** (自訂指標) 頁面上，選擇 **Custom metrics** (建立)。

1. 在 **Create custom metric** (建立自訂指標) 頁面上，執行下列動作。

   1. 在 **Name** (名稱) 下，為您的自訂指標輸入名稱。在建立自訂指標之後，您無法修改此名稱。

   1. 在 **Display name (optional)** (顯示名稱 (選用)) 下，您可以為自訂指標輸入易記名稱。它不必是唯一的，而且它可以在建立後進行修改。

   1. 在 **Type** (類型) 下，選擇您要監控的指標類型。指標類型包括 **string-list**、**ip-address-list**、**number-list** 和 **number**。建立後無法修改類型。
**注意**  
ML Detect 只允許 **number** (數字) 類型。

   1. 在 **Tags** (標籤) 下，您可以選取與資源相關聯的標籤。

   完成時請選擇 **Confirm** (確認)。

1. 在您建立了自訂度量之後，**Custom metrics** (自訂指標) 頁面即會出現，您可以在其中查看新建立的自訂指標。

1. 接著，您需要將自訂指標新增至安全性設定檔。在 [AWS IoT 主控台](https://console.aws.amazon.com/iot)的導覽窗格中，展開 **Defend** (防禦)，然後選擇 **Detect** (偵測)、**Security profiles** (安全性描述檔)。

1. 選擇您要將自訂指標新增至哪個安全性設定檔。

1. 選擇 **Actions** (動作)、**Edit** (編輯)。

1. 選擇 **Additional Metrics to retain** (要保留的其他指標)，然後選擇您的自訂指標。在下列畫面上選擇 **Next** (下一步)，直到您到達 **Confirm** (確認) 頁面。選擇 **Save** (儲存) 和 **Continue** (繼續)。在成功新增了您的自訂指標之後，安全性設定檔詳細資訊頁面即會出現。
**注意**  
當任何指標值為負數時，百分位數統計資料不適用於指標。

### 檢視自訂指標詳細資訊
<a name="dd-detect-console-read"></a>

下列程序顯示如何在主控台中檢視自訂指標的詳細資訊。

1. 在 [AWS IoT 主控台](https://console.aws.amazon.com/iot)的導覽窗格中，展開 **Defend** (防禦)，然後選擇 **Detect** (偵測)、**Metrics** (指標)。

1. 選擇您要檢視其詳細資訊之自訂指標的 **Metric name** (指標名稱)。

### 更新自訂指標
<a name="dd-detect-console-edit"></a>

下列程序顯示如何在主控台中更新自訂指標。

1. 在 [AWS IoT 主控台](https://console.aws.amazon.com/iot)的導覽窗格中，展開 **Defend** (防禦)，然後選擇 **Detect** (偵測)、**Metrics** (指標)。

1. 選擇您要更新之自訂指標旁邊的選項按鈕。然後，針對 **Actions** (動作)，選擇 **Edit** (編輯)。

1. 在 **Update custom metric** (更新自訂指標) 頁面上，您可以編輯顯示名稱，以及移除或新增標籤。

1. 完成後，請選擇 **Update** (更新)。**Custom metrics** (自訂指標) 頁面。

### 刪除自訂指標
<a name="dd-detect-console-delete"></a>

下列程序顯示如何在主控台中刪除自訂指標。

1. 首先，從所參照的任何安全性設定檔中移除您的自訂指標。您可以在自訂指標詳細資訊頁面上檢視包含自訂指標的安全性設定檔。在 [AWS IoT 主控台](https://console.aws.amazon.com/iot)的導覽窗格中，展開 **Defend** (防禦)，然後選擇 **Detect** (偵測)、**Metrics** (指標)。

1. 選擇您要移除的自訂指標。從自訂指標詳細資訊頁面上列示在 **Security Profiles** (安全性設定檔) 的任何安全性設定檔中移除自訂指標。

1. 在 [AWS IoT 主控台](https://console.aws.amazon.com/iot)的導覽窗格中，展開 **Defend** (防禦)，然後選擇 **Detect** (偵測)、**Metrics** (指標)。

1. 選擇您要刪除之自訂指標旁邊的選項按鈕。然後，針對 **Actions** (動作)，選擇 **Delete** (刪除)。

1. 在 **Are you sure you want to delete custom metric?** (您確定要刪除自訂指標嗎？) 訊息上，選擇 **Delete custom metric** 刪除自訂指標。
**警告**  
在刪除了自訂指標之後，您會遺失與該指標相關聯的所有資料。這個動作無法復原。

## 如何從 CLI 使用自訂指標
<a name="dd-detect-custom-metrics-how-to-cli"></a>

**Topics**
+ [

### AWS IoT Device Defender Agent SDK (Python)
](#dd-detec-custom-metrics-cli-sdk)
+ [

### 建立自訂指標並將其新增至安全性設定檔
](#dd-detect-custom-cli-create)
+ [

### 檢視自訂指標詳細資訊
](#dd-detect-custom-cli-read)
+ [

### 更新自訂指標
](#dd-detect-custom-cli-edit)
+ [

### 刪除自訂指標
](#dd-detect-custom-cli-delete)

### AWS IoT Device Defender Agent SDK (Python)
<a name="dd-detec-custom-metrics-cli-sdk"></a>

若要開始使用，請下載 AWS IoT Device Defender Agent SDK (Python) 範例代理程式。此代理程式會收集指標並發佈報告。在發佈您的裝置端指標之後，您可以檢視所收集的指標，並判斷設定警示的閥值。設定裝置代理程式的指示可在 [AWS IoT Device Defender Agent SDK (Python) Readme](https://github.com/aws-samples/aws-iot-device-defender-agent-sdk-python/blob/master/README.rst) 上取得。如需詳細資訊，請參閱 [AWS IoT Device Defender Agent SDK (Python)](https://github.com/aws-samples/aws-iot-device-defender-agent-sdk-python)。

### 建立自訂指標並將其新增至安全性設定檔
<a name="dd-detect-custom-cli-create"></a>

下列程序顯示如何從 CLI 建立自訂指標，並將其新增至安全性設定檔。

1. 使用 `[create-custom-metric](https://docs.aws.amazon.com/cli/latest/reference/iot/create-custom-metric.html)` 命令來建立自訂指標。下列範例會建立測量電池百分比的自訂指標。

   ```
   aws iot create-custom-metric \
       --metric-name "batteryPercentage" \
       --metric-type "number" \
       --display-name "Remaining battery percentage." \
       --region us-east-1
       --client-request-token "02ccb92b-33e8-4dfa-a0c1-35b181ed26b0" \
   ```

   輸出：

   ```
   {
       "metricName": "batteryPercentage",
       "metricArn": "arn:aws:iot:us-east-1:1234564789012:custommetric/batteryPercentage"
   }
   ```

1. 在建立您的自訂指標之後，您可以使用 `[update-security-profile](https://docs.aws.amazon.com/cli/latest/reference/iot/update-security-profile.html)` 將自訂指標新增至現有設定檔，或使用 `[create-security-profile](https://docs.aws.amazon.com/cli/latest/reference/iot/create-security-profile.html)` 建立新的安全性設定檔，以將自訂指標新增至其中。在這裡，我們會建立新的安全性設定檔 (稱為 *batteryUsage*)，將新的 *batteryPercentage* 自訂指標新增至其中。我們也會新增 Rules Detect 指標，稱為 *cellularBandwidth*。

   ```
   aws iot create-security-profile \
       --security-profile-name batteryUsage \
       --security-profile-description "Shows how much battery is left in percentile."  \
       --behaviors "[{\"name\":\"great-than-75\",\"metric\":\"batteryPercentage\",\"criteria\":{\"comparisonOperator\":\"greater-than\",\"value\":{\"number\":75},\"consecutiveDatapointsToAlarm\":5,\"consecutiveDatapointsToClear\":1}},{\"name\":\"cellularBandwidth\",\"metric\":\"aws:message-byte-size\",\"criteria\":{\"comparisonOperator\":\"less-than\",\"value\":{\"count\":128},\"consecutiveDatapointsToAlarm\":1,\"consecutiveDatapointsToClear\":1}}]" \
       --region us-east-1
   ```

   輸出：

   ```
   {
       "securityProfileArn": "arn:aws:iot:us-east-1:1234564789012:securityprofile/batteryUsage",
       "securityProfileName": "batteryUsage"
   }
   ```

**注意**  
當任何指標值為負數時，百分位數統計資料不適用於指標。

### 檢視自訂指標詳細資訊
<a name="dd-detect-custom-cli-read"></a>

下列程序顯示如何從 CLI 檢視自訂指標的詳細資訊。
+ 使用 `[list-custom-metrics](https://docs.aws.amazon.com/cli/latest/reference/iot/list-custom-metrics.html)` 命令來檢視您的所有自訂指標。

  ```
  aws iot list-custom-metrics \
      --region us-east-1
  ```

  此令命的輸出結果如下所示：

  ```
  {
      "metricNames": [
          "batteryPercentage"
      ]
  }
  ```

### 更新自訂指標
<a name="dd-detect-custom-cli-edit"></a>

下列程序顯示如何從 CLI 更新自訂指標。
+ 使用 `[update-custom-metric](https://docs.aws.amazon.com/cli/latest/reference/iot/update-custom-metric.html)` 命令來更新自訂指標。下列範例會更新 `display-name`。

  ```
  aws iot update-custom-metric \
      --metric-name batteryPercentage \
      --display-name 'remaining battery percentage on device' \
      --region us-east-1
  ```

  此令命的輸出結果如下所示：

  ```
  {
      "metricName": "batteryPercentage",
      "metricArn": "arn:aws:iot:us-east-1:1234564789012:custommetric/batteryPercentage",
      "metricType": "number",
      "displayName": "remaining battery percentage on device",
      "creationDate": "2020-11-17T23:01:35.110000-08:00",
      "lastModifiedDate": "2020-11-17T23:02:12.879000-08:00"
  }
  ```

### 刪除自訂指標
<a name="dd-detect-custom-cli-delete"></a>

下列程序顯示如何從 CLI 刪除自訂指標。

1. 若要刪除自訂指標，首先從自訂指標連接至的任何安全性設定檔中移除自訂指標。使用 `[list-security-profiles](https://docs.aws.amazon.com/cli/latest/reference/iot/list-security-profiles.html)` 命令來檢視具有特定自訂指標的安全性設定檔。

1. 若要從安全性設定檔移除自訂指標，請使用 `[update-security-profiles](https://docs.aws.amazon.com/cli/latest/reference/iot/update-security-profiles.html)` 命令。輸入您要保留的所有資訊，但排除自訂指標：

   ```
   aws iot update-security-profile \
     --security-profile-name batteryUsage \
     --behaviors "[{\"name\":\"cellularBandwidth\",\"metric\":\"aws:message-byte-size\",\"criteria\":{\"comparisonOperator\":\"less-than\",\"value\":{\"count\":128},\"consecutiveDatapointsToAlarm\":1,\"consecutiveDatapointsToClear\":1}}]"
   ```

   此令命的輸出結果如下所示：

   ```
   {
     "behaviors": [{\"name\":\"cellularBandwidth\",\"metric\":\"aws:message-byte-size\",\"criteria\":{\"comparisonOperator\":\"less-than\",\"value\":{\"count\":128},\"consecutiveDatapointsToAlarm\":1,\"consecutiveDatapointsToClear\":1}}],
     "securityProfileName": "batteryUsage",
     "lastModifiedDate": 2020-11-17T23:02:12.879000-09:00,
     "securityProfileDescription": "Shows how much battery is left in percentile.",
     "version": 2,
     "securityProfileArn": "arn:aws:iot:us-east-1:1234564789012:securityprofile/batteryUsage",  
     "creationDate": 2020-11-17T23:02:12.879000-09:00
   }
   ```

1. 將自訂指標分開之後，使用 `[delete-custom-metric](https://docs.aws.amazon.com/cli/latest/reference/iot/delete-custom-metric.html)` 命令來刪除自訂指標。

   ```
   aws iot delete-custom-metric  \
     --metric-name batteryPercentage \
     --region us-east-1
   ```

   此令命的輸出結果如下所示

   ```
   HTTP 200
   ```

## 自訂指標 CLI 命令
<a name="dd-detect-custom-metrics-cli-commands"></a>

您可以使用以下 CLI 命令來建立和管理自訂指標。
+ [create-custom-metric](https://docs.aws.amazon.com/cli/latest/reference/iot/create-custom-metric.html)
+ [describe-custom-metric](https://docs.aws.amazon.com/cli/latest/reference/iot/describe-custom-metric.html)
+ [list-custom-metrics](https://docs.aws.amazon.com/cli/latest/reference/iot/list-custom-metrics.html)
+ [update-custom-metric](https://docs.aws.amazon.com/cli/latest/reference/iot/update-custom-metric.html)
+ [delete-custom-metric](https://docs.aws.amazon.com/cli/latest/reference/iot/delete-custom-metric.html)
+ [list-security-profiles](https://docs.aws.amazon.com/cli/latest/reference/iot/list-security-profiles.html)

## 自訂指標 API
<a name="dd-detect-custom-metrics-apis"></a>

下列 API 可以用來建立和管理自訂指標。
+ [CreateCustomMetric](https://docs.aws.amazon.com/iot/latest/apireference/API_CreateCustomMetric.html)
+ [DescribeCustomMetric](https://docs.aws.amazon.com/iot/latest/apireference/API_DescribeCustomMetric.html)
+ [ListCustomMetrics](https://docs.aws.amazon.com/iot/latest/apireference/API_ListCustomMetrics.html)
+ [UpdateCustomMetric](https://docs.aws.amazon.com/iot/latest/apireference/API_UpdateCustomMetric.html)
+ [DeleteCustomMetric](https://docs.aws.amazon.com/iot/latest/apireference/API_DeleteCustomMetric.html)
+ [ListSecurityProfiles](https://docs.aws.amazon.com/iot/latest/apireference/API_ListSecurityProfiles.html)