

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

# 入門教學課程
<a name="fleet-metrics-get-started"></a>

在此教學課程中，您要建立能夠監控感應器溫度的[機群指標](iot-fleet-metrics.md)，以偵測潛在的異常情況。建立機群指標時，您可以定義[彙總查詢](index-aggregate.md)，其可偵測溫度超過華氏 80 度的感應器數目。您可以指定每 60 秒執行一次查詢，其結果會發送到 CloudWatch；您可以在 CloudWatch 中檢視具有潛在高溫風險的感應器數量，並設定警示。為完成此教學課程，您需要使用 [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html)。

於本教學課程中，您會了解如何：
+ [設定](#fleet-metrics-tutorial-setup)
+ [建立機群指標](#fleet-metrics-tutorial-create)
+ [在 CloudWatch 中檢視指標](#fleet-metrics-tutorial-view-data)
+ [清除資源](#fleet-metrics-tutorial-delete-fleet-metrics)

此教學課程約需 15 分鐘方能完成。

## 先決條件
<a name="fleet-metrics-tutorial-prerequisites"></a>
+ 安裝 [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) 的最新版本
+ 熟悉[如何查詢彙總資料](https://docs.aws.amazon.com/iot/latest/developerguide/index-aggregate.html)
+ 熟悉[如何使用 Amazon CloudWatch 指標](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/working_with_metrics.html) 

## 設定
<a name="fleet-metrics-tutorial-setup"></a>

若要使用機群指標，請啟用機群索引。若要針對具有指定資料來源和相關組態的物件或物件群組啟用機群索引，請遵循[管理物件索引](managing-index.md#enable-index)和[管理物件群組索引](thinggroup-index.md#enable-group-index)中的指示。

**設定**

1. 執行下列命令來啟用機群索引，並指定要從中搜尋的資料來源。

   ```
   aws iot update-indexing-configuration \
   --thing-indexing-configuration "thingIndexingMode=REGISTRY_AND_SHADOW,customFields=[{name=attributes.temperature,type=Number},{name=attributes.rackId,type=String},{name=attributes.stateNormal,type=Boolean}],thingConnectivityIndexingMode=STATUS" \
   ```

   上述的 CLI 命令範例可啟用機群索引，支援使用 `AWS_Things` 索引來搜尋登錄檔資料、影子資料和物件連線狀態。

   組態變更可能需要幾分鐘時間才能完成。在建立機群指標之前，請驗證是否已啟用機群索引。

   若要檢查機群索引是否已啟用，請執行下列 CLI 命令：

   ```
   aws --region us-east-1 iot describe-index --index-name "AWS_Things"
   ```

   如需詳細資訊，請參閱[啟用物件索引](managing-index.md#enable-index)。

1. 執行以下 Bash 指令碼來建立十個物件並提供相應描述。

   ```
   # Bash script. Type `bash` before running in other shells.
   
   Temperatures=(70 71 72 73 74 75 47 97 98 99)
   Racks=(Rack1 Rack1 Rack2 Rack2 Rack3 Rack4 Rack5 Rack6 Rack6 Rack6)
   IsNormal=(true true true true true true false false false false)
   
   for ((i=0; i < 10; i++))
   do
     thing=$(aws iot create-thing --thing-name "TempSensor$i" --attribute-payload attributes="{temperature=${Temperatures[@]:$i:1},rackId=${Racks[@]:$i:1},stateNormal=${IsNormal[@]:$i:1}}")
     aws iot describe-thing --thing-name "TempSensor$i"
   done
   ```

   這個指令碼建立了十個物件來表示十個感應器。如下表所述，每個物件都有 `temperature`、`rackId` 以及 `stateNormal` 屬性：    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot/latest/developerguide/fleet-metrics-get-started.html)

   此指令碼的輸出包含十個 JSON 檔案。其中一個 JSON 檔案如下所示：

   ```
   {
       "version": 1, 
       "thingName": "TempSensor0", 
       "defaultClientId": "TempSensor0", 
       "attributes": {
           "rackId": "Rack1", 
           "stateNormal": "true", 
           "temperature": "70"
       }, 
       "thingArn": "arn:aws:iot:region:account:thing/TempSensor0", 
       "thingId": "example-thing-id"
   }
   ```

   如需詳細資訊，請參閱[建立物件](https://docs.aws.amazon.com/iot/latest/developerguide/thing-registry.html#create-thing)。

## 建立機群指標
<a name="fleet-metrics-tutorial-create"></a>

**若要建立機群指標**

1. 執行下列命令建立名為 *high\$1temp\$1FM* 的機群指標。您可以建立機群指標，以此監控 CloudWatch 中華氏溫度超過 80 度的感應器數量。

   ```
   aws iot create-fleet-metric --metric-name "high_temp_FM" --query-string "thingName:TempSensor* AND attributes.temperature >80" --period 60 --aggregation-field "attributes.temperature" --aggregation-type name=Statistics,values=count
   ```

   --metric-name 

   資料類型：字串。此 `--metric-name` 參數會指定機群指標名稱。在此範例中，您建立的是名為 *high\$1temp\$1FM* 的機群指標。

   --query-string

   資料類型：字串。此 `--query-string` 參數會指定查詢字串。在這個範例中，查詢字串意味著查詢名稱開頭為 *TempSensor* 和溫度高於華氏 80 度的所有物件。如需詳細資訊，請參閱[查詢語法](query-syntax.md)。

   --period 

   資料類型：整數。此 `--period` 參數會指定彙總資料的擷取時間 (以秒為單位)。在此範例中，您指定要建立的機群指標每 60 秒擷取一次彙總資料。

   --aggregation-field

   資料類型：字串。此 `--aggregation-field` 參數會指定要評估的屬性。在此範例中，要評估的是溫度屬性。

   --aggregation-type

   此 `--aggregation-type` 參數指定要在機群指標中顯示的統計摘要。您可以為監控任務自訂以下彙總類型的彙總查詢屬性：**Statistics** (統計數字)、**Cardinality** (基數) 以及 **Percentile** (百分位數)。在此範例中，您可以針對彙總類型和**統計資料**指定**計數**，以傳回具有與查詢相符之屬性的裝置計數，也就是傳回名稱開頭為 *TempSensor* 和溫度高於華氏 80 度的裝置計數。如需詳細資訊，請參閱[查詢彙總資料](index-aggregate.md)。

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

   ```
   {
       "metricArn": "arn:aws:iot:region:111122223333:fleetmetric/high_temp_FM", 
       "metricName": "high_temp_FM"
   }
   ```
**注意**  
資料點可能需要一些時間才能在 CloudWatch 中顯示。

   若要進一步了解如何建立機群指標，請參閱[管理機群指標](managing-fleet-metrics.md)。

   如果無法建立機群指標，請參閱[機群指標故障診斷](fleet-indexing-troubleshooting.md#fleet-metrics-troubleshooting)。

1. (選用) 執行下列命令來描述名為 *high\$1temp\$1FM* 的機群指標：

   ```
   aws iot describe-fleet-metric --metric-name "high_temp_FM"
   ```

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

   ```
   {
       "queryVersion": "2017-09-30", 
       "lastModifiedDate": 1625249775.834, 
       "queryString": "*", 
       "period": 60, 
       "metricArn": "arn:aws:iot:region:111122223333:fleetmetric/high_temp_FM", 
       "aggregationField": "registry.version", 
       "version": 1, 
       "aggregationType": {
           "values": [
               "count"
           ], 
           "name": "Statistics"
       }, 
       "indexName": "AWS_Things", 
       "creationDate": 1625249775.834, 
       "metricName": "high_temp_FM"
   }
   ```

## 在 CloudWatch 中檢視機群指標
<a name="fleet-metrics-tutorial-view-data"></a>

建立機群指標後，您可以在 CloudWatch 中檢視指標資料。在本教學課程中，您會看到一個指標，其中顯示名稱開頭為 *TempSensor* 和溫度高於華氏 80 度的感應器數量。

**在 CloudWatch 中檢視資料點**

1. 透過 [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/) 開啟 CloudWatch 主控台。

1. 在左側面板的 CloudWatch 功能表上，選擇**指標**來展開子功能表，然後選擇**所有指標**。此操作會開啟頁面，其中上半部分會顯示圖形，下半部分則包含四個標籤式區段。

1. 第一個標籤式區段**所有指標**會列出可以在群組中檢視的所有指標。請選擇 **IoTFleetMetrics**。這包含您的所有機群指標。

1. 在 **All metrics** (所有指標) 索引標籤的 **Aggregation type** (彙總類型) 區段，選擇 **Aggregation type** (彙總類型) 檢視建立的所有機群指標。

1. 選擇機群指標，在 **Aggregation type** (彙總類型) 區段的左側顯示圖表。您會看到**指標名稱**左側的值 *count*，這是您在本教學課程的[建立機群指標](#fleet-metrics-tutorial-create)區段中指定的彙總類型值。

1. 選擇位於 **All metrics** (所有指標) 索引標籤右側的名為 **Graphed metrics** (圖表化指標) 的第二個索引標籤，檢視從上一步中選擇的機群指標。

   您應該可以看到如下所示的圖表，其中顯示溫度高於華氏 80 度的感應器數量：  
![\[AWS IoT 機群指標\]](http://docs.aws.amazon.com/zh_tw/iot/latest/developerguide/images/fm-metric-in-cloudwatch.png)
**注意**  
CloudWatch 中的 **Period** (期間) 屬性預設為 5 分鐘。這是在 CloudWatch 中顯示的資料點之間的時間間隔。您可以根據需求變更 **Period** (期間) 設定。

1. (選用) 您可以設定指標警示。

   1. 在左側面板的 CloudWatch 功能表上，選擇**警示**來展開子功能表，然後選擇**所有警示**。

   1. 在 **Alarms** (警示) 頁面中，選擇右上角的 **Create alarm** (建立警示)。請遵循主控台中的 **Create alarm** (建立警示) 指示，視需要建立警示。如需詳細資訊，請參閱[使用 Amazon CloudWatch 警示](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html)。

如需進一步了解，請參閱[使用 Amazon CloudWatch 指標](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/working_with_metrics.html)。

如果無法在 CloudWatch 中看到資料點，請參閱[機群指標故障診斷](fleet-indexing-troubleshooting.md#fleet-metrics-troubleshooting)。

## 清除
<a name="fleet-metrics-tutorial-delete-fleet-metrics"></a>

**刪除機群指標**

可以使用 **delete-fleet-metric** CLI 命令來刪除機群指標。

若要刪除名為 *high\$1temp\$1FM* 的機群指標，請執行下列命令。

```
aws iot delete-fleet-metric --metric-name "high_temp_FM"
```

**清理物件**

可以使用 **delete-thing** CLI 命令來刪除物件。

若要刪除您已建立的十個物件，請執行下列指令碼：

```
# Bash script. Type `bash` before running in other shells.

for ((i=0; i < 10; i++))
do
  thing=$(aws iot delete-thing --thing-name "TempSensor$i")
done
```

**清除 CloudWatch 中的指標**

CloudWatch 不支援刪除指標。指標的過期時間因保留排程而定。如需進一步了解，請參閱[使用 Amazon CloudWatch 指標](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/working_with_metrics.html)。