

# ML Detect 指南
<a name="dd-detect-ml-getting-started"></a>

**注意**  
在下列區域目前未提供 ML Detect：  
亞太地區 (馬來西亞)

在此入門指南中，您可以建立 ML Detect 安全性設定檔，其會使用機器學習 (ML)，根據您裝置的歷史指標資料建立預期行為的模型。當 ML Detect 建立 ML 模型時，您可以監控其進度。在建置 ML 模型之後，您可以持續檢視和調查警示，並緩解已識別的問題。

如需 ML Detect 及其 API 和 CLI 命令的相關資訊，請參閱 [ML Detect](dd-detect-ml.md)。

**Topics**
+ [

## 必要條件
](#ml-detect-prereqs)
+ [

## 如何在主控台中使用 ML Detect
](#dd-detect-ml-console)
+ [

## 如何搭配 CLI 使用 ML Detect
](#dd-detect-ml-cli)

## 必要條件
<a name="ml-detect-prereqs"></a>
+ AWS 帳戶。如果您沒有此項，請參閱[設定](https://docs.aws.amazon.com/iot/latest/developerguide/dd-setting-up.html)。

## 如何在主控台中使用 ML Detect
<a name="dd-detect-ml-console"></a>

**Topics**
+ [

### 啟用 ML Detect
](#enable-ml-detect-console)
+ [

### 監控您的 ML 模型狀態
](#monitor-ml-models-console)
+ [

### 檢閱您的 ML Detect 警示
](#review-ml-alarms-console)
+ [

### 微調您的 ML 警示
](#fine-tune-ml-models-console)
+ [

### 標記警示的驗證狀態
](#mark-your-alarms)
+ [

### 緩解已識別的裝置問題
](#mitigate-ml-issues-console)

### 啟用 ML Detect
<a name="enable-ml-detect-console"></a>

下列程序詳述如何在主控台中設定 ML Detect。

1. 首先，確保您的裝置將建立 [ML Detect 最低需求](dd-detect-ml.md#dd-detect-ml-requirements)中定義的最小必要資料點，以持續訓練和重新整理模型。如需進行資料收集，請確定您的安全性設定檔已連接到目標，該目標可以是物件或物件群組。

1. 在 [AWS IoT 主控台](https://console.aws.amazon.com/iot)的導覽窗格中，展開 **Defend** (防禦)。依序選擇 **Detect** (偵測)、**Security profiles** (安全性設定檔)、**Create security profile** (建立安全性設定檔)、**Create ML anomaly Detect profile** (建立 ML 異常偵測設定檔)。

1. 在 **Set basic configurations** (設定基本組態) 頁面上，執行下列動作：
   + 在 **Target** (目標) 下，選擇您的目標裝置群組。
   + 在 **Security profile name** (安全性設定檔名稱) 下，輸入安全性設定檔的名稱。
   + (選用) 在 **Description** (描述) 下，您可以撰寫 ML 設定檔的簡短描述。
   + 在 **Selected metric behaviors in Security Profile** (安全性設定檔中選取的指標行為) 下，選擇您要監控的指標。  
![\[建立 ML 安全性設定檔組態頁面，當中包含選取作為目標的所有已註冊物件、列出的指標行為 (例如，授權失敗和連線嘗試次數)，以及新增雲端或裝置端指標的選項。\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-ml-set-basic.png)

   完成後，請選擇 **Next (下一步)**。

1. 在 **Set SNS (optional)** (設定 SNS (選用)) 頁面上，指定當裝置違反您設定檔中的行為時，警示通知的 SNS 主題。選擇您要用來發佈至所選 SNS 主題的 IAM 角色。

   如果您尚未有 SNS 角色，請使用下列步驟來建立具備適當許可和必要信任關係的角色。
   + 導覽至 [IAM 主控台](https://console.aws.amazon.com/iam/)。在導覽窗格中，選擇 **Roles** (角色)，然後選擇 **Create role** (建立角色)。
   + 在 **Select type of trusted entity** (選取信任的實體類型) 下，選取 **AWS 服務**。然後，在 **Choose a use case** (選擇使用案例) 下，選擇 **IoT**，並在 **Select your use case** (選取您的使用案例) 下，選擇 **IoT - Device Defender Mitigation Actions** (IoT - Device Defender 緩解動作)。完成時，請選擇 **Next: Permissions** (下一步：許可)。
   + 在 **Attached permissions policies** (連接的許可政策) 下，確保已選取 **AWSIoTDeviceDefenderPublishFindingsToSNSMitigationAction**，然後選擇 **Next: Tags** (下一步：標籤)。  
![\[AWS IoT Device Defender 角色的許可政策表，當中包含政策名稱、每一項政策所提供存取權的用途說明，以及篩選或搜尋政策的選項。\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-ml-sns-findings.png)
   + 在 **Add tags (optional)** (新增標籤 (選用)) 下，您可以新增想要與您角色建立關聯的任何標籤。完成後，請選擇 **Next: Review (下一步：檢閱)**。
   + 在 **Review** (檢閱) 下，給與您的角色一個名稱，並確保 **AWSIoTDeviceDefenderPublishFindingsToSNSMitigationAction** 已列示在 **Permissions** (許可) 下，而且 **** (AWS 服務：iot.amazonaws.com) 已列示在 **Trust relationships** (信任關係) 下。完成時，選擇 **Create role** (建立角色)。  
![\[IAM 角色摘要頁面，當中顯示 Sample-SNS-role 詳細資訊，例如角色 ARN、說明、執行個體設定檔 ARN、路徑、建立時間、工作階段持續時間上限，以及對 SNS 緩解動作政策套用的 AWS IoT Device Defender 發佈問題清單\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-ml-detect-permissions.png)  
![\[IAM Sample-SNS-role 摘要，當中顯示角色 ARN、提供發佈 SNS 通知的 AWS IoT Device Defender 寫入存取權的角色說明、路徑、建立時間，以及受信任實體\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-ml-detect-trust-relationships.png)

1. 在 **Edit Metric behavior** (編輯指標行為) 頁面上，您可以自訂 ML 行為設定。  
![\[編輯指標行為區段，其中包含授權失敗、輸入位元組數和連線嘗試次數指標，可讓您設定警示觸發、通知和 ML Detect 可信度等級的資料點。\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-ml-update-config.png)

1. 完成後，請選擇 **Next (下一步)**。

1. 在 **Review configuration** (檢閱組態) 頁面上，驗證您想要機器學習監控的行為，然後選擇 **Next** (下一步)。  
![\[編輯 ML 安全性設定檔頁面，當中顯示以所有已註冊物件為目標的 Smart_lights_ML_Detect_Security_Profile，包含授權失敗、輸出位元組數、連線嘗試次數和中斷連線次數的指標行為。\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-ml-review-config.png)

   

1. 在您建立了安全性設定檔之後，系統會將您重新導向至 **Security Profiles** (安全性設定檔) 頁面，其中會出現新建立的安全性設定檔。
**注意**  
初始的 ML 模型訓練和建置需要 14 天才能完成。如果您的裝置上有任何異常活動，您可能會在完成之後看到警示。

### 監控您的 ML 模型狀態
<a name="monitor-ml-models-console"></a>

當您的 ML 模型處於初始訓練期間時，您可以採取以下步驟隨時監控其進度。

1. 在 [AWS IoT 主控台](https://console.aws.amazon.com/iot)的導覽窗格中，展開 **Defend** (防禦)，然後選擇 **Detect** (偵測)、**Security profiles** (安全性描述檔)。

1. 在**Security Profiles** (安全性設定檔) 頁面上，選擇您要檢閱的安全性設定檔。然後，選擇 **Behaviors and ML training** (行為與 ML 訓練)。

1. 在 **Behaviors and ML training** (行為與 ML 訓練) 頁面上，檢查 ML 模型的訓練進度。

   在您的模型狀態為 **Active** (作用中) 之後，它會根據您的使用情況開始做出偵測決策，並每天更新設定檔。  
![\[此儀表板顯示低可信度的機器學習模型，用於監控 TCP/UDP 接聽程式連接埠和已建立的 TCP 連線。\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-ml-active-state.png)

**注意**  
如果您的模型未如預期進展，請確定您的裝置符合 [最低需求](dd-detect-ml.md#dd-detect-ml-requirements)。

### 檢閱您的 ML Detect 警示
<a name="review-ml-alarms-console"></a>

在建置您的 ML 模型並準備好進行資料推論之後，您可以定期檢視和調查模型識別的警示。

1. 在 [AWS IoT 主控台](https://console.aws.amazon.com/iot)的導覽窗格中，展開 **Defend** (防禦)，然後選擇 **Detect** (偵測)、**Alarms** (警示)。  
![\[AWS IoT Device Defender 警示清單，當中顯示 5 個作用中授權失敗警示，包含物件名稱、安全性設定檔、行為類型、行為名稱、上次發出時間及驗證狀態欄。\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-ml-alarms.png)

1. 如果導覽至 **History** (歷史記錄) 標籤，您也可以檢視不再處於警示狀態之裝置的詳細資訊。  
![\[此線形圖顯示兩週期間內的警示、已清除和已失效警示，y 軸表示警示數量，x 軸表示日期。\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-ml-history-alarm.png)

   若要取得詳細資訊，請在 **Manage** (管理) 下選擇 **Things** (物件)、選擇您想要查看其更多詳細資訊的物件，然後導覽至 **Defender metrics** (Defender 指標)。您可以存取 **Defender metrics graph** (Defender 指標圖形)，並針對警示中來自 **Active** (作用中) 標籤的任何項目執行調查。在此情況下，此圖形會顯示訊息大小中的峰值，這會啟動警示。您可以看到後續清除的警示。  
![\[IoT 物件儀表板會顯示所指定日期和時間的訊息大小最大指標圖，峰值為 801 個位元組。\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-ml-defender-metrics.png)

### 微調您的 ML 警示
<a name="fine-tune-ml-models-console"></a>

在建置您的 ML 模型並準備好進行資料評估之後，您可以更新安全性設定檔的 ML 行為設定以變更組態。下列程序顯示如何在 AWS CLI 中更新您安全性設定檔的 ML 行為設定。

1. 在 [AWS IoT 主控台](https://console.aws.amazon.com/iot)的導覽窗格中，展開 **Defend** (防禦)，然後選擇 **Detect** (偵測)、**Security profiles** (安全性描述檔)。

1. 在**Security Profiles** (安全性設定檔) 頁面上，選取您要檢閱之安全性設定檔旁邊的核取方塊。然後，選擇 **Actions** (動作)、**Edit** (編輯)。  
![\[AWS IoT Device Defender 安全性設定檔清單，當中顯示設定檔名稱、ML 閾值類型、保留的行為、目標物件、建立日期、通知狀態\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-ml-fine-tune.png)

1. 在 **Set basic configurations** (設定基本組態) 下，您可以調整安全性設定檔目標物件群組，或變更您要監控的指標。  
![\[建立 ML 安全性設定檔組態頁面，當中包含選取作為目標的所有已註冊物件、列出的指標行為 (例如，授權失敗和連線嘗試次數)，以及新增雲端或裝置端指標的選項\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-ml-set-basic.png)

1. 您可以導覽至 **Edit metric behaviors** (編輯指標行為) 來更新下列任一項。
   + 啟動警示所需的 ML 模型資料點
   + 清除警示所需的 ML 模型資料點
   + 您的 ML Detect 可信度
   + 您的 ML Detect 通知 (例如**Not suppressed** (未抑制)、**Suppressed** (已抑制))  
![\[編輯指標行為區段，當中包含設定 ML 安全性設定檔的授權失敗、輸出位元組數和連線嘗試次數指標的選項。\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-ml-update-config-2.png)

### 標記警示的驗證狀態
<a name="mark-your-alarms"></a>

設定驗證狀態並提供該驗證狀態的描述，以標記您的警示。這可協助您和團隊識別您不需要回應的警示。

1. 在 [AWS IoT 主控台](https://console.aws.amazon.com/iot/)的導覽窗格中，展開 **Defend** (防禦)，然後選擇 **Detect** (偵測)、**Alarms** (警示)。選取警示以標記其驗證狀態。  
![\[此 AWS IoT Device Defender 警示檢視會顯示 IoT 主控台物件的作用中授權失敗行為事件，例如 fdsa 安全性設定檔的 iotconsole-6f8379bc-c245-4ffe-8ef7-b2b52e78975c。\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-ml-alarm-select.png)

1. 選擇 **Mark verification state** (標記驗證狀態)。驗證狀態模式會開啟。

1. 選擇適當的驗證狀態、輸入驗證描述 (選填)，然後選擇 **Mark** (標記)。此動作會將驗證狀態和描述指派給所選警示。  
![\[用於標示警示驗證狀態的對話方塊，選項包括：不明、真陽性、誤報、良性肯定。\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-ml-alarm-state-window.png)

### 緩解已識別的裝置問題
<a name="mitigate-ml-issues-console"></a>

1. *(選用)* 在設定隔離緩解動作之前，讓我們先設定隔離群組，將違規裝置移至其中。您也可以使用現有群組

1. 導覽至 **Manage** (管理)、**Thing groups** (物件群組)，然後導覽至 **Create Thing Group** (建立物件群組)。命名您的物件群組 在本教學課程中，我們會將物件群組命名為 `Quarantine_group`。在 **Thing group** (物件群組)、**Security** (安全性) 下，將以下政策套用到物件群組。

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Effect": "Deny",
               "Action": "iot:*",
               "Resource": "*"
           }
       ]
   }
   ```

------  
![\[AWS IoT 控制台「建立物件群組」頁面，內含「建立物件群組」按鈕。\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-create-thing-group.png)

   完成時，選擇 **Create thing group** (建立物件群組)。

1. 既然已建立物件群組，就讓我們建立一個緩解動作，將警示中的裝置移至 `Quarantine_group`。

   在 **Defend** (防禦)、**Mitigation actions** (緩解動作) 下，選擇 **Create** (建立)。  
![\[AWS IoT Device Defender 緩解動作組態表單，當中包含動作名稱、動作類型、許可、動作執行角色及物件群組欄位。\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-miti-create.png)

1. 在 **Create a new mitigation action** (建立新的緩解動作) 頁面上，輸入下列資訊。
   + **Action name** (動作名稱)：為您的緩解動作命名，例如 **Quarantine\$1action**。
   + **Action type** (動作類型)：選擇動作的類型。我們將選擇 **Add things to thing group (Audit or Detect mitigation)** (將物件新增至物件群組 (稽核或偵測緩解))。
   + **Action execution role** (動作執行角色)：建立角色或選擇現有角色 (如果您先前已建立角色的話)。
   + **Parameters** (參數)：選擇物件群組。我們可以使用 `Quarantine_group`，這是我們先前建立的。  
![\[AWS IoT Device Defender 緩解動作組態表單，當中包含動作名稱、動作類型、許可、動作執行角色及物件群組欄位。\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-miti-create-form.png)

   完成後，選擇**儲存**。您現在具可將警示中裝置移至隔離物件群組的緩解動作，以及可在調查時隔離裝置的緩解動作。

1. 導覽至 **Defender**、**Detect** (偵測)、**Alarms** (警示)。您可以在 **Active** (作用中) 下查看哪些裝置處於警示狀態，。  
![\[AWS IoT Device Defender 警示清單，當中顯示 5 個作用中授權失敗警示，包含物件名稱、安全性設定檔、行為類型、行為名稱、上次發出時間及驗證狀態欄。\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-ml-alarms.png)

   選取您要移至隔離群組的裝置，然後選擇 **Start Mitigation Actions** (啟動緩解動作)。

1. 在 **Start mitigation actions** (啟動緩解動作)、**Start Actions** (啟動動作) 下，選取您稍早建立的緩解動作。例如，我們將選擇 **Quarantine\$1action**，然後選擇 **Start** (啟動)。隨即開啟 Action Tasks (動作任務) 頁面。  
![\[緩解動作對話方塊，當中列出受影響的物件「udml7」，且包含用於確認不可復原動作的核取方塊，以及選擇要執行之動作的下拉式清單。\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-ml-start-action.png)

1. 現在，裝置在 **Quarantine\$1group** 中隔離，而且您可以調查引發警示的問題根本原因。完成調查後，您可以將裝置移出物件群組或採取進一步的動作。  
![\[AWS IoT Device Defender 偵測動作任務表，當中顯示一項會將物件新增至 quarantine_group 物件群組的隔離動作。\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-ml-action-tasks.png)

   

## 如何搭配 CLI 使用 ML Detect
<a name="dd-detect-ml-cli"></a>

下列顯示如何使用 CLI 設定 ML Detect。

**Topics**
+ [

### 啟用 ML Detect
](#enable-ml-detect-cli)
+ [

### 監控您的 ML 模型狀態
](#monitor-ml-models-cli)
+ [

### 檢閱您的 ML Detect 警示
](#review-ml-alarms-cli)
+ [

### 微調您的 ML 警示
](#fine-tune-ml-models-cli)
+ [

### 標記警示的驗證狀態
](#mark-verification-state-cli)
+ [

### 緩解已識別的裝置問題
](#mitigate-issues-cli)

### 啟用 ML Detect
<a name="enable-ml-detect-cli"></a>

下列程序顯示如何在 AWS CLI 啟用 ML Detect。

1. 確保您的裝置將建立 [ML Detect 最低需求](dd-detect-ml.md#dd-detect-ml-requirements)中定義的最小必要資料點，以持續訓練和重新整理模型。如需進行資料收集，請確定您的物件位於已連接到安全性設定檔的物件群組中。

1. 使用 `[create-security-profile](https://docs.aws.amazon.com/cli/latest/reference/iot/create-security-profile.html)` 命令建立 ML Detect 安全性設定檔。下列範例會建立名為 *security-profile-for-smart-lights* 的安全性設定檔，檢查傳送的訊息數目、授權失敗次數、連線嘗試次數，以及中斷連線次數。此範例會使用 `mlDetectionConfig`，以確定指標將使用 ML Detect 模型。

   ```
   aws iot create-security-profile \
       --security-profile-name security-profile-for-smart-lights \
       --behaviors \
        '[{
       "name": "num-messages-sent-ml-behavior",
       "metric": "aws:num-messages-sent",
       "criteria": {
         "consecutiveDatapointsToAlarm": 1,
         "consecutiveDatapointsToClear": 1,
         "mlDetectionConfig": {
           "confidenceLevel": "HIGH"
         }
       },
       "suppressAlerts": true
     },
     {
       "name": "num-authorization-failures-ml-behavior",
       "metric": "aws:num-authorization-failures",
       "criteria": {
         "consecutiveDatapointsToAlarm": 1,
         "consecutiveDatapointsToClear": 1,
         "mlDetectionConfig": {
           "confidenceLevel": "HIGH"
         }
       },
       "suppressAlerts": true
     },
     {
       "name": "num-connection-attempts-ml-behavior",
       "metric": "aws:num-connection-attempts",
       "criteria": {
         "consecutiveDatapointsToAlarm": 1,
         "consecutiveDatapointsToClear": 1,
         "mlDetectionConfig": {
           "confidenceLevel": "HIGH"
         }
       },
       "suppressAlerts": true
     },
     {
       "name": "num-disconnects-ml-behavior",
       "metric": "aws:num-disconnects",
       "criteria": {
         "consecutiveDatapointsToAlarm": 1,
         "consecutiveDatapointsToClear": 1,
         "mlDetectionConfig": {
           "confidenceLevel": "HIGH"
         }
       },
       "suppressAlerts": true
     }]'
   ```

   輸出：

   ```
   {
       "securityProfileName": "security-profile-for-smart-lights",
       "securityProfileArn": "arn:aws:iot:eu-west-1:123456789012:securityprofile/security-profile-for-smart-lights"
     }
   ```

1. 接下來，將您的安全性設定檔與一或多個物件群組建立關聯。使用 `[attach-security-profile](https://docs.aws.amazon.com/cli/latest/reference/iot/attach-security-profile.html)` 命令，將物件群組連接至您的安全性設定檔。下列範例會將名為 *ML\$1Detect\$1beta\$1static\$1group* 的物件群組與 *security-profile-for-smart-lights* 安全性設定檔建立關聯。

   ```
   aws iot attach-security-profile \
   --security-profile-name security-profile-for-smart-lights \
   --security-profile-target-arn arn:aws:iot:eu-west-1:123456789012:thinggroup/ML_Detect_beta_static_group
   ```

   輸出：

   無。

1. 在您建立了完整的安全性設定檔之後，ML 模型就會開始訓練。初始的 ML 模型訓練和建置需要 14 天才能完成。14 天後，如果您的裝置上有任何異常活動，您可以預期看到警示。

### 監控您的 ML 模型狀態
<a name="monitor-ml-models-cli"></a>

下列程序顯示如何在進行中的訓練監控您的 ML 模型。
+ 使用 `[get-behavior-model-training-summaries](https://docs.aws.amazon.com/cli/latest/reference/iot/get-behavior-model-training-summaries.html)` 命令來檢視 ML 模型的進度。下列範例會取得 *security-profile-for-smart-lights* 安全性設定檔的 ML 模型訓練進度摘要。`modelStatus` 會顯示模型是否已完成訓練，或是由於特定行為仍在擱置建置。

  ```
  aws iot get-behavior-model-training-summaries \
     --security-profile-name security-profile-for-smart-lights
  ```

  輸出：

  ```
  {
      "summaries": [
          {
              "securityProfileName": "security-profile-for-smart-lights",
              "behaviorName": "Messages_sent_ML_behavior",
              "trainingDataCollectionStartDate": "2020-11-30T14:00:00-08:00",
              "modelStatus": "ACTIVE",
              "datapointsCollectionPercentage": 29.408,
              "lastModelRefreshDate": "2020-12-07T14:35:19.237000-08:00"
          },
          {
              "securityProfileName": "security-profile-for-smart-lights",
              "behaviorName": "Messages_received_ML_behavior",
              "modelStatus": "PENDING_BUILD",
              "datapointsCollectionPercentage": 0.0
          },
          {
              "securityProfileName": "security-profile-for-smart-lights",
              "behaviorName": "Authorization_failures_ML_behavior",
              "trainingDataCollectionStartDate": "2020-11-30T14:00:00-08:00",
              "modelStatus": "ACTIVE",
              "datapointsCollectionPercentage": 35.464,
              "lastModelRefreshDate": "2020-12-07T14:29:44.396000-08:00"
          },
          {
              "securityProfileName": "security-profile-for-smart-lights",
              "behaviorName": "Message_size_ML_behavior",
              "trainingDataCollectionStartDate": "2020-11-30T14:00:00-08:00",
              "modelStatus": "ACTIVE",
              "datapointsCollectionPercentage": 29.332,
              "lastModelRefreshDate": "2020-12-07T14:30:44.113000-08:00"
          },
          {
              "securityProfileName": "security-profile-for-smart-lights",
              "behaviorName": "Connection_attempts_ML_behavior",
              "trainingDataCollectionStartDate": "2020-11-30T14:00:00-08:00",
              "modelStatus": "ACTIVE",
              "datapointsCollectionPercentage": 32.891999999999996,
              "lastModelRefreshDate": "2020-12-07T14:29:43.121000-08:00"
          },
          {
              "securityProfileName": "security-profile-for-smart-lights",
              "behaviorName": "Disconnects_ML_behavior",
              "trainingDataCollectionStartDate": "2020-11-30T14:00:00-08:00",
              "modelStatus": "ACTIVE",
              "datapointsCollectionPercentage": 35.46,
              "lastModelRefreshDate": "2020-12-07T14:29:55.556000-08:00"
          }
      ]
  }
  ```

**注意**  
如果您的模型未如預期進展，請確定您的裝置符合 [最低需求](dd-detect-ml.md#dd-detect-ml-requirements)。

### 檢閱您的 ML Detect 警示
<a name="review-ml-alarms-cli"></a>

在建置您的 ML 模型並準備好進行資料評估之後，您可以定期檢視模型所推論的任何警示。下列程序顯示如何在 AWS CLI 中檢視您的警示。
+ 若要查看所有作用中的警示，請使用 `[list-active-violations](https://docs.aws.amazon.com/cli/latest/reference/iot/list-active-violations.html)` 命令。

  ```
  aws iot list-active-violations \
  --max-results 2
  ```

  輸出：

  ```
  {
      "activeViolations": []
  }
  ```

  或者，您可以檢視在指定時段使用 `[list-violation-events](https://docs.aws.amazon.com/cli/latest/reference/iot/list-violation-events.html)` 命令探索到的所有違規。下列範例列出從 2020 年 9 月 22 日 5:42:13 GMT 到 2020 年 10 月 26 日 2020 5:42:13 GMT 的違規事件。

  ```
  aws iot list-violation-events \
      --start-time 1599500533 \ 
      --end-time 1600796533 \
      --max-results 2
  ```

  輸出：

  ```
  {
      "violationEvents": [
          {
              "violationId": "1448be98c09c3d4ab7cb9b6f3ece65d6",
              "thingName": "lightbulb-1",
              "securityProfileName": "security-profile-for-smart-lights",
              "behavior": {
                  "name": "LowConfidence_MladBehavior_MessagesSent",
                  "metric": "aws:num-messages-sent",
                  "criteria": {
                      "consecutiveDatapointsToAlarm": 1,
                      "consecutiveDatapointsToClear": 1,
                      "mlDetectionConfig": {
                          "confidenceLevel": "HIGH"
                      }
                  },
                  "suppressAlerts": true
              },
              "violationEventType": "alarm-invalidated",
              "violationEventTime": 1600780245.29
          },
          {
              "violationId": "df4537569ef23efb1c029a433ae84b52",
              "thingName": "lightbulb-2",
              "securityProfileName": "security-profile-for-smart-lights",
              "behavior": {
                  "name": "LowConfidence_MladBehavior_MessagesSent",
                  "metric": "aws:num-messages-sent",
                  "criteria": {
                      "consecutiveDatapointsToAlarm": 1,
                      "consecutiveDatapointsToClear": 1,
                      "mlDetectionConfig": {
                          "confidenceLevel": "HIGH"
                      }
                  },
                  "suppressAlerts": true
              },
              "violationEventType": "alarm-invalidated",
              "violationEventTime": 1600780245.281
          }
      ],
      "nextToken": "Amo6XIUrsOohsojuIG6TuwSR3X9iUvH2OCksBZg6bed2j21VSnD1uP1pflxKX1+a3cvBRSosIB0xFv40kM6RYBknZ/vxabMe/ZW31Ps/WiZHlr9Wg7R7eEGli59IJ/U0iBQ1McP/ht0E2XA2TTIvYeMmKQQPsRj/eoV9j7P/wveu7skNGepU/mvpV0O2Ap7hnV5U+Prx/9+iJA/341va+pQww7jpUeHmJN9Hw4MqW0ysw0Ry3w38hOQWEpz2xwFWAxAARxeIxCxt5c37RK/lRZBlhYqoB+w2PZ74730h8pICGY4gktJxkwHyyRabpSM/G/f5DFrD9O5v8idkTZzBxW2jrbzSUIdafPtsZHL/yAMKr3HAKtaABz2nTsOBNre7X2d/jIjjarhon0Dh9l+8I9Y5Ey+DIFBcqFTvhibKAafQt3gs6CUiqHdWiCenfJyb8whmDE2qxvdxGElGmRb+k6kuN5jrZxxw95gzfYDgRHv11iEn8h1qZLD0czkIFBpMppHj9cetHPvM+qffXGAzKi8tL6eQuCdMLXmVE3jbqcJcjk9ItnaYJi5zKDz9FVbrz9qZZPtZJFHp"
  }
  ```

### 微調您的 ML 警示
<a name="fine-tune-ml-models-cli"></a>

一旦建置了您的 ML 模型並準備好進行資料評估，您就可以更新安全性設定檔的 ML 行為設定以變更組態。下列程序顯示如何在 AWS CLI 中更新您安全性設定檔的 ML 行為設定。
+ 若要變更安全性設定檔的 ML 行為設定，請使用 `[update-security-profile](https://docs.aws.amazon.com/cli/latest/reference/iot/update-security-profile.html)` 命令。下列範例會更新 *security-profile-for-smart-lights* 安全性設定檔的行為，方法為變更一些行為的 `confidenceLevel`，並取消抑制所有行為的通知。

  ```
  aws iot update-security-profile \
      --security-profile-name security-profile-for-smart-lights \
      --behaviors \
       '[{
        "name": "num-messages-sent-ml-behavior",
        "metric": "aws:num-messages-sent",
        "criteria": {
            "mlDetectionConfig": {
                "confidenceLevel" : "HIGH"
            }
        },
        "suppressAlerts": false
    },
    {
        "name": "num-authorization-failures-ml-behavior",
        "metric": "aws:num-authorization-failures",
        "criteria": {
            "mlDetectionConfig": {
                "confidenceLevel" : "HIGH"
            }
        },
        "suppressAlerts": false
    },
    {
        "name": "num-connection-attempts-ml-behavior",
        "metric": "aws:num-connection-attempts",
        "criteria": {
            "mlDetectionConfig": {
                "confidenceLevel" : "HIGH"
            }
        },
        "suppressAlerts": false
    },
    {
        "name": "num-disconnects-ml-behavior",
        "metric": "aws:num-disconnects",
        "criteria": {
            "mlDetectionConfig": {
                "confidenceLevel" : "LOW"
            }
        },
        "suppressAlerts": false
  
    }]'
  ```

  輸出：

  ```
   {
      "securityProfileName": "security-profile-for-smart-lights",
      "securityProfileArn": "arn:aws:iot:eu-west-1:123456789012:securityprofile/security-profile-for-smart-lights",
      "behaviors": [
          {
              "name": "num-messages-sent-ml-behavior",
              "metric": "aws:num-messages-sent",
              "criteria": {
                  "mlDetectionConfig": {
                      "confidenceLevel": "HIGH"
                  }
              }
          },
          {
              "name": "num-authorization-failures-ml-behavior",
              "metric": "aws:num-authorization-failures",
              "criteria": {
                  "mlDetectionConfig": {
                      "confidenceLevel": "HIGH"
                  }
              }
          },
          {
              "name": "num-connection-attempts-ml-behavior",
              "metric": "aws:num-connection-attempts",
              "criteria": {
                  "mlDetectionConfig": {
                      "confidenceLevel": "HIGH"
                  }
              },
              "suppressAlerts": false
          },
          {
              "name": "num-disconnects-ml-behavior",
              "metric": "aws:num-disconnects",
              "criteria": {
                  "mlDetectionConfig": {
                      "confidenceLevel": "LOW"
                  }
              },
              "suppressAlerts": true
          }
      ],
      "version": 2,
      "creationDate": 1600799559.249,
      "lastModifiedDate": 1600800516.856
  }
  ```

### 標記警示的驗證狀態
<a name="mark-verification-state-cli"></a>

您可以使用驗證狀態標記警示，以協助分類警示並調查異常情況。
+ 以驗證狀態和該狀態的描述標記警示。例如，若要將警示的驗證狀態設定為誤判，請使用下列命令：

  ```
  aws iot put-verification-state-on-violation --violation-id 12345  --verification-state FALSE_POSITIVE --verification-state-description "This is dummy description"  --endpoint https://us-east-1.iot.amazonaws.com --region us-east-1
  ```

  輸出：

  無。

### 緩解已識別的裝置問題
<a name="mitigate-issues-cli"></a>

1. 使用 `[create-thing-group](https://docs.aws.amazon.com/cli/latest/reference/iot/create-thing-group.html)` 命令，針對緩解動作建立物件群組。在下列範例中，我們會建立名為 **ThingGroupForDetectMitigationAction** 的物件群組。

   ```
   aws iot create-thing-group —thing-group-name ThingGroupForDetectMitigationAction
   ```

   輸出：

   ```
   {
    "thingGroupName": "ThingGroupForDetectMitigationAction",
    "thingGroupArn": "arn:aws:iot:us-east-1:123456789012:thinggroup/ThingGroupForDetectMitigationAction",
    "thingGroupId": "4139cd61-10fa-4c40-b867-0fc6209dca4d"
   }
   ```

1. 接下來，使用 `[create-mitigation-action](https://docs.aws.amazon.com/cli/latest/reference/iot/create-mitigation-action.html)` 命令來建立緩解動作。在下列範例中，我們會建立稱為 **detect\$1mitigation\$1action** 的緩解動作，搭配用來套用緩解動作之 IAM 角色的 ARN。我們也會定義動作的類型和該動作的參數。在此情況下，我們的緩解會將物件移到我們先前建立的物件群組，稱為 **ThingGroupForDetectMitigationAction**。

   ```
   aws iot create-mitigation-action --action-name detect_mitigation_action \
   --role-arn arn:aws:iam::123456789012:role/MitigationActionValidRole \
   --action-params \  
   '{
        "addThingsToThingGroupParams": {
            "thingGroupNames": ["ThingGroupForDetectMitigationAction"],
            "overrideDynamicGroups": false
        }
    }'
   ```

   輸出：

   ```
   {
    "actionArn": "arn:aws:iot:us-east-1:123456789012:mitigationaction/detect_mitigation_action",
    "actionId": "5939e3a0-bf4c-44bb-a547-1ab59ffe67c3"
   }
   ```

1. 使用 `[start-detect-mitigation-actions-task](https://docs.aws.amazon.com/cli/latest/reference/iot/start-detect-mitigation-actions-task.html)` 命令來啟動緩解動作任務。`task-id`、`target` 和 `actions` 是必要的參數。

   ```
   aws iot start-detect-mitigation-actions-task \
       --task-id taskIdForMitigationAction \
       --target '{ "violationIds" : [ "violationId-1", "violationId-2" ] }' \
       --actions "detect_mitigation_action" \
       --include-only-active-violations \
       --include-suppressed-alerts
   ```

   輸出：

   ```
   {
       "taskId": "taskIdForMitigationAction"
   }
   ```

1. (選用) 若要檢視任務中包含的緩解動作執行，請使用 `[list-detect-mitigation-actions-executions](https://docs.aws.amazon.com/cli/latest/reference/iot/list-detect-mitigation-actions-executions.html)` 命令。

   ```
   aws iot list-detect-mitigation-actions-executions \
       --task-id taskIdForMitigationAction \
       --max-items 5 \
       --page-size 4
   ```

   輸出：

   ```
   {
       "actionsExecutions": [
           {
               "taskId": "e56ee95e - f4e7 - 459 c - b60a - 2701784290 af",
               "violationId": "214_fe0d92d21ee8112a6cf1724049d80",
               "actionName": "underTest_MAThingGroup71232127",
               "thingName": "cancelDetectMitigationActionsTaskd143821b",
               "executionStartDate": "Thu Jan 07 18: 35: 21 UTC 2021",
               "executionEndDate": "Thu Jan 07 18: 35: 21 UTC 2021",
               "status": "SUCCESSFUL",
           }
       ]
   }
   ```

1. (選用) 使用 `[describe-detect-mitigation-actions-task](https://docs.aws.amazon.com/cli/latest/reference/iot/describe-detect-mitigation-actions-task.html)` 命令來取得緩解動作任務的相關資訊。

   ```
   aws iot describe-detect-mitigation-actions-task \
       --task-id taskIdForMitigationAction
   ```

   輸出：

   ```
   {
       "taskSummary": {
           "taskId": "taskIdForMitigationAction",
           "taskStatus": "SUCCESSFUL",
           "taskStartTime": 1609988361.224,
           "taskEndTime": 1609988362.281,
           "target": {
               "securityProfileName": "security-profile-for-smart-lights",
               "behaviorName": "num-messages-sent-ml-behavior"
           },
           "violationEventOccurrenceRange": {
               "startTime": 1609986633.0,
               "endTime": 1609987833.0
           },
           "onlyActiveViolationsIncluded": true,
           "suppressedAlertsIncluded": true,
           "actionsDefinition": [
               {
                   "name": "detect_mitigation_action",
                   "id": "5939e3a0-bf4c-44bb-a547-1ab59ffe67c3",
                   "roleArn": "arn:aws:iam::123456789012:role/MitigatioActionValidRole",
                   "actionParams": {
                       "addThingsToThingGroupParams": {
                           "thingGroupNames": [
                               "ThingGroupForDetectMitigationAction"
                           ],
                           "overrideDynamicGroups": false
                       }
                   }
               }
           ],
           "taskStatistics": {
               "actionsExecuted": 0,
               "actionsSkipped": 0,
               "actionsFailed": 0
           }
       }
   }
   ```

1. (選用) 若要取得緩解動作任務的清單，請使用 `[list-detect-mitigation-actions-tasks](https://docs.aws.amazon.com/cli/latest/reference/iot/list-detect-mitigation-actions-tasks.html)` 命令。

   ```
   aws iot list-detect-mitigation-actions-tasks \
       --start-time 1609985315 \
       --end-time 1609988915 \
       --max-items 5 \
       --page-size 4
   ```

   輸出：

   ```
   {
       "tasks": [
           {
               "taskId": "taskIdForMitigationAction",
               "taskStatus": "SUCCESSFUL",
               "taskStartTime": 1609988361.224,
               "taskEndTime": 1609988362.281,
               "target": {
                   "securityProfileName": "security-profile-for-smart-lights",
                   "behaviorName": "num-messages-sent-ml-behavior"
               },
               "violationEventOccurrenceRange": {
                   "startTime": 1609986633.0,
                   "endTime": 1609987833.0
               },
               "onlyActiveViolationsIncluded": true,
               "suppressedAlertsIncluded": true,
               "actionsDefinition": [
                   {
                       "name": "detect_mitigation_action",
                       "id": "5939e3a0-bf4c-44bb-a547-1ab59ffe67c3",
                       "roleArn": "arn:aws:iam::123456789012:role/MitigatioActionValidRole",
                       "actionParams": {
                           "addThingsToThingGroupParams": {
                               "thingGroupNames": [
                                   "ThingGroupForDetectMitigationAction"
                               ],
                               "overrideDynamicGroups": false
                           }
                       }
                   }
               ],
               "taskStatistics": {
                   "actionsExecuted": 0,
                   "actionsSkipped": 0,
                   "actionsFailed": 0
               }
           }
       ]
   }
   ```

1. (選用) 若要取消緩解動作任務，請使用 `[cancel-detect-mitigation-actions-task](https://docs.aws.amazon.com/cli/latest/reference/iot/cancel-detect-mitigation-actions-task.html)` 命令。

   ```
   aws iot cancel-detect-mitigation-actions-task \
       --task-id taskIdForMitigationAction
   ```

   輸出：

   無。