使用自訂庫存 - AWS Systems Manager

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

使用自訂庫存

透過建立 AWS Systems Manager 庫存自訂庫存,將想要的任意中繼資料指派給節點。舉例來說,假設您負責管理資料中心內的大量伺服器,而這些伺服器皆已設定為 Systems Manager 受管節點。目前,您都是將伺服器機架位置的相關資訊存放在試算表中。透過自訂庫存,即可在節點上將每個節點的機架位置指定為中繼資料。當您使用 Systems Manager 收集庫存時,系統將一併收集中繼資料和其他庫存中繼資料。然後,您可以透過使用資源資料同步將所有庫存中繼資料連接到中央 Amazon S3 儲存貯體,並查詢資料。

注意

Systems Manager 支援每個 AWS 帳戶 最多 20 個自訂庫存類型。

若要指派自訂庫存給節點,則可使用 Systems Manager PutInventory API 操作,如中 演練:指派自訂庫存中繼資料給受管節點 所述。或者,您也能選擇建立自訂庫存 JSON 檔案,並將其上傳至節點。本節將說明如何建立 JSON 檔案。

以下範例 JSON 檔案與自訂庫存會指定與現場部署伺服器相關的機架資訊。此範例會指定一種的自訂庫存資料 ("TypeName": "Custom:RackInformation"),並在 Content 下提供多個描述資料的項目。

{ "SchemaVersion": "1.0", "TypeName": "Custom:RackInformation", "Content": { "Location": "US-EAST-02.CMH.RACK1", "InstalledTime": "2016-01-01T01:01:01Z", "vendor": "DELL", "Zone" : "BJS12", "TimeZone": "UTC-8" } }

您也可以在 Content 區段中指定不同項目,如下範例所示。

{ "SchemaVersion": "1.0", "TypeName": "Custom:PuppetModuleInfo", "Content": [{ "Name": "puppetlabs/aws", "Version": "1.0" }, { "Name": "puppetlabs/dsc", "Version": "2.0" } ] }

自訂庫存的 JSON 結構描述需要 SchemaVersionTypeNameContent 區段,而您可以自行定義這些區段的資訊。

{ "SchemaVersion": "user_defined", "TypeName": "Custom:user_defined", "Content": { "user_defined_attribute1": "user_defined_value1", "user_defined_attribute2": "user_defined_value2", "user_defined_attribute3": "user_defined_value3", "user_defined_attribute4": "user_defined_value4" } }

TypeName 的值限制為 100 個字元。此外,該 TypeName 值必須以大寫字母 Custom 開頭。例如:Custom:PuppetModuleInfo。因此,下列範例會導致例外狀況:CUSTOM:PuppetModuleInfocustom:PuppetModuleInfo

Content 區段包含屬性和資料。這些項目不需區分大小寫。然而,若您有定義屬性 (如 "Vendor": "DELL"),自訂庫存檔案便必須持續參考此屬性。換而言之,如果您在某個檔案中指定 "Vendor": "DELL" (vendor 使用大寫「V」),卻在另一個檔案中指定 "vendor": "DELL" (vendor 使用小寫「v」),系統就會傳回錯誤。

注意

您必須以 .json 副檔名儲存檔案,且您定義的清查必須只包含字串值。

建立檔案之後,請務必將其儲存在節點上。下表顯示自訂庫存 JSON 檔案在節點上的存放位置:

作業系統 路徑

Linux

/var/lib/amazon/ssm/node-id/inventory/custom

macOS

/opt/aws/ssm/data/node-id/inventory/custom

Windows

%SystemDrive%\ProgramData\Amazon\SSM\InstanceData\node-id\inventory\custom

如需使用自訂庫存的範例,請參閱部落格文章 Get Disk Utilization of Your Fleet Using EC2 Systems Manager Custom Inventory Types

刪除自訂清查

透過 DeleteInventory API 操作,即可刪除自訂庫存類型,以及與該類型相關聯的資料。您可以使用 AWS Command Line Interface (AWS CLI) 來呼叫 delete-inventory 命令,進而刪除庫存類型的所有資料。此外,您也能利用 SchemaDeleteOption 來呼叫 delete-inventory 命令,藉此刪除自訂庫存類型。

注意

庫存類型也稱為庫存結構描述。

SchemaDeleteOption 參數包含下列選項:

  • DeleteSchema:此選項可刪除指定的自訂類型,以及與其相關聯的所有資料。您稍後可以視需求重新建立該結構描述。

  • DisableSchema:如果您選擇此選項,系統即會關閉目前版本,然後刪除所有相關資料。當版本低於或等於遭關閉版本時,系統會忽略所有新資料。在版本比遭關閉版本更新的情況下,您能夠呼叫 PutInventory 動作,重新啟用這個庫存類型。

使用 AWS CLI 刪除或關閉自訂庫存
  1. 如果您尚未安裝並設定 AWS Command Line Interface (AWS CLI),請進行相應的操作。

    如需相關資訊,請參閱安裝或更新最新版本的 AWS CLI

  2. 執行下列命令,即可利用 dry-run 選項來查看系統要刪除的資料。此命令不會刪除任何資料。

    aws ssm delete-inventory --type-name "Custom:custom_type_name" --dry-run

    系統會傳回如下資訊。

    {
       "DeletionSummary":{
          "RemainingCount":3,
          "SummaryItems":[
             {
                "Count":2,
                "RemainingCount":2,
                "Version":"1.0"
             },
             {
                "Count":1,
                "RemainingCount":1,
                "Version":"2.0"
             }
          ],
          "TotalCount":3
       },
       "TypeName":"Custom:custom_type_name"
    }

    如需了解如何刪除庫存摘要的相關資訊,請參閱了解刪除清查摘要

  3. 執行下列命令,藉此刪除自訂庫存類型的所有資料。

    aws ssm delete-inventory --type-name "Custom:custom_type_name"
    注意

    此命令輸出並不會顯示刪除進度。因此,TotalCount 與 Remaining Count 的結果都是相同的,因為系統尚未刪除任何項目。您能使用 describe-inventory-deletions 命令來顯示刪除進度,本主題稍後會予以說明。

    系統會傳回如下資訊。

    {
       "DeletionId":"system_generated_deletion_ID",
       "DeletionSummary":{
          "RemainingCount":3,
          "SummaryItems":[
             {
                "Count":2,
                "RemainingCount":2,
                "Version":"1.0"
             },
             {
                "Count":1,
                "RemainingCount":1,
                "Version":"2.0"
             }
          ],
          "TotalCount":3
       },
       "TypeName":"custom_type_name"
    }

    系統將從 Systems Manager 庫存服務刪除指定自訂庫存類型的所有資料。

  4. 執行下列命令。該命令會針對目前的庫存類型版本執行以下動作:關閉目前版本,然後刪除所有相關資料。當版本低於或等於遭關閉版本時,系統會忽略所有新資料。

    aws ssm delete-inventory --type-name "Custom:custom_type_name" --schema-delete-option "DisableSchema"

    系統會傳回如下資訊。

    {
       "DeletionId":"system_generated_deletion_ID",
       "DeletionSummary":{
          "RemainingCount":3,
          "SummaryItems":[
             {
                "Count":2,
                "RemainingCount":2,
                "Version":"1.0"
             },
             {
                "Count":1,
                "RemainingCount":1,
                "Version":"2.0"
             }
          ],
          "TotalCount":3
       },
       "TypeName":"Custom:custom_type_name"
    }

    若要檢視遭關閉的庫存類型,則可使用下列命令。

    aws ssm get-inventory-schema --type-name Custom:custom_type_name
  5. 執行下列命令,以便刪除庫存類型。

    aws ssm delete-inventory --type-name "Custom:custom_type_name" --schema-delete-option "DeleteSchema"

    系統會刪除指定自訂類型的結構描述及所有庫存資料。

    系統會傳回如下資訊。

    {
       "DeletionId":"system_generated_deletion_ID",
       "DeletionSummary":{
          "RemainingCount":3,
          "SummaryItems":[
             {
                "Count":2,
                "RemainingCount":2,
                "Version":"1.0"
             },
             {
                "Count":1,
                "RemainingCount":1,
                "Version":"2.0"
             }
          ],
          "TotalCount":3
       },
       "TypeName":"Custom:custom_type_name"
    }

檢視刪除狀態

您可以使用 describe-inventory-deletions AWS CLI 命令來檢查刪除操作的狀態。若要檢視特定刪除操作的狀態,請指定刪除 ID。或者,您能夠省略刪除 ID,以檢視過去 30 天內執行的所有刪除操作清單。

  1. 執行下列命令,藉此檢視刪除操作狀態。系統將在 delete-inventory 摘要中傳回刪除 ID。

    aws ssm describe-inventory-deletions --deletion-id system_generated_deletion_ID

    系統會傳回最新狀態。刪除操作可能尚未完成。系統會傳回如下資訊。

    {"InventoryDeletions": 
      [
        {"DeletionId": "system_generated_deletion_ID", 
         "DeletionStartTime": 1521744844, 
         "DeletionSummary": 
          {"RemainingCount": 1, 
           "SummaryItems": 
            [
              {"Count": 1, 
               "RemainingCount": 1, 
               "Version": "1.0"}
            ], 
           "TotalCount": 1}, 
         "LastStatus": "InProgress", 
         "LastStatusMessage": "The Delete is in progress", 
         "LastStatusUpdateTime": 1521744844, 
         "TypeName": "Custom:custom_type_name"}
      ]
    }

    如果刪除操作成功,LastStatusMessage 會顯示以下狀態:Deletion is successful (刪除成功)。

    {"InventoryDeletions": 
      [
        {"DeletionId": "system_generated_deletion_ID", 
         "DeletionStartTime": 1521744844, 
         "DeletionSummary": 
          {"RemainingCount": 0, 
           "SummaryItems": 
            [
              {"Count": 1, 
               "RemainingCount": 0, 
               "Version": "1.0"}
            ], 
           "TotalCount": 1}, 
         "LastStatus": "Complete", 
         "LastStatusMessage": "Deletion is successful", 
         "LastStatusUpdateTime": 1521745253, 
         "TypeName": "Custom:custom_type_name"}
      ]
    }
  2. 執行下列命令,即可檢視過去 30 天內執行的所有刪除操作清單。

    aws ssm describe-inventory-deletions --max-results a number
    {"InventoryDeletions": 
      [
        {"DeletionId": "system_generated_deletion_ID", 
         "DeletionStartTime": 1521682552, 
         "DeletionSummary": 
          {"RemainingCount": 0, 
           "SummaryItems": 
            [
              {"Count": 1, 
               "RemainingCount": 0, 
               "Version": "1.0"}
            ], 
           "TotalCount": 1}, 
         "LastStatus": "Complete", 
         "LastStatusMessage": "Deletion is successful", 
         "LastStatusUpdateTime": 1521682852, 
         "TypeName": "Custom:custom_type_name"}, 
        {"DeletionId": "system_generated_deletion_ID", 
         "DeletionStartTime": 1521744844, 
         "DeletionSummary": 
          {"RemainingCount": 0, 
           "SummaryItems": 
            [
              {"Count": 1, 
               "RemainingCount": 0, 
               "Version": "1.0"}
            ], 
           "TotalCount": 1}, 
         "LastStatus": "Complete", 
         "LastStatusMessage": "Deletion is successful", 
         "LastStatusUpdateTime": 1521745253, 
         "TypeName": "Custom:custom_type_name"}, 
        {"DeletionId": "system_generated_deletion_ID", 
         "DeletionStartTime": 1521680145, 
         "DeletionSummary": 
          {"RemainingCount": 0, 
           "SummaryItems": 
            [
              {"Count": 1, 
               "RemainingCount": 0, 
               "Version": "1.0"}
            ], 
           "TotalCount": 1}, 
         "LastStatus": "Complete", 
         "LastStatusMessage": "Deletion is successful", 
         "LastStatusUpdateTime": 1521680471, 
         "TypeName": "Custom:custom_type_name"}
      ], 
     "NextToken": "next-token"

了解刪除清查摘要

為了協助您更充分了解庫存摘要刪除操作的內容,請參考以下範例。使用者將 Custom:RackSpace 庫存指派給三個節點。庫存項目 1 和 2 皆使用自訂類型 1.0 版 ("SchemaVersion":"1.0")。另一方面,庫存項目 3 則是使用自訂類型 2.0 版 ("SchemaVersion":"2.0")。

RackSpace 自訂庫存 1

{ "CaptureTime":"2018-02-19T10:48:55Z", "TypeName":"CustomType:RackSpace", "InstanceId":"i-1234567890", "SchemaVersion":"1.0" "Content":[ { content of custom type omitted } ] }

RackSpace 自訂庫存 2

{ "CaptureTime":"2018-02-19T10:48:55Z", "TypeName":"CustomType:RackSpace", "InstanceId":"i-1234567891", "SchemaVersion":"1.0" "Content":[ { content of custom type omitted } ] }

RackSpace 自訂庫存 3

{ "CaptureTime":"2018-02-19T10:48:55Z", "TypeName":"CustomType:RackSpace", "InstanceId":"i-1234567892", "SchemaVersion":"2.0" "Content":[ { content of custom type omitted } ] }

使用者可執行下列命令,以預覽即將刪除的資料。

aws ssm delete-inventory --type-name "Custom:RackSpace" --dry-run

系統會傳回如下資訊。

{
   "DeletionId":"1111-2222-333-444-66666",
   "DeletionSummary":{
      "RemainingCount":3,           
      "TotalCount":3,             
                TotalCount and RemainingCount are the number of items that would be deleted if this was not a dry run. These numbers are the same because the system didn't delete anything.
      "SummaryItems":[
         {
            "Count":2,             The system found two items that use SchemaVersion 1.0. Neither item was deleted.           
            "RemainingCount":2,
            "Version":"1.0"
         },
         {
            "Count":1,             The system found one item that uses SchemaVersion 1.0. This item was not deleted.
            "RemainingCount":1,
            "Version":"2.0"
         }
      ],

   },
   "TypeName":"Custom:RackSpace"
}

使用者可執行下列命令,以刪除 Custom:RackSpace 庫存。

注意

此命令輸出並不會顯示刪除進度。因此,TotalCountRemainingCount 都是相同的,因為系統尚未刪除任何項目。您可以使用 describe-inventory-deletions 命令來顯示刪除進度。

aws ssm delete-inventory --type-name "Custom:RackSpace"

系統會傳回如下資訊。

{
   "DeletionId":"1111-2222-333-444-7777777",
   "DeletionSummary":{
      "RemainingCount":3,           There are three items to delete
      "SummaryItems":[
         {
            "Count":2,              The system found two items that use SchemaVersion 1.0.
            "RemainingCount":2,     
            "Version":"1.0"
         },
         {
            "Count":1,              The system found one item that uses SchemaVersion 2.0.
            "RemainingCount":1,     
            "Version":"2.0"
         }
      ],
      "TotalCount":3                
   },
   "TypeName":"RackSpace"
}

在 EventBridge 中檢視清查刪除操作

您可以設定 Amazon EventBridge,在每次使用者刪除自訂庫存時建立事件。EventBridge 提供了三種事件類型,皆適用於自訂庫存刪除操作:

  • 執行個體的刪除動作:指出特定受管節點的自訂庫存是否已成功刪除。

  • 刪除動作摘要:刪除動作的摘要。

  • 關閉自訂庫存類型的警告:當使用者對先前關閉的自訂庫存類型版本呼叫 PutInventory API 操作時,就會出現這個警告事件。

下方是每個事件的範例。

執行個體的刪除動作

{
   "version":"0",
   "id":"998c9cde-56c0-b38b-707f-0411b3ff9d11",
   "detail-type":"Inventory Resource State Change",
   "source":"aws.ssm",
   "account":"478678815555",
   "time":"2018-05-24T22:24:34Z",
   "region":"us-east-1",
   "resources":[
      "arn:aws:ssm:us-east-1:478678815555:managed-instance/i-0a5feb270fc3f0b97"
   ],
   "detail":{
      "action-status":"succeeded",
      "action":"delete",
      "resource-type":"managed-instance",
      "resource-id":"i-0a5feb270fc3f0b97",
      "action-reason":"",
      "type-name":"Custom:MyInfo"
   }
}

刪除動作摘要

{
   "version":"0",
   "id":"83898300-f576-5181-7a67-fb3e45e4fad4",
   "detail-type":"Inventory Resource State Change",
   "source":"aws.ssm",
   "account":"478678815555",
   "time":"2018-05-24T22:28:25Z",
   "region":"us-east-1",
   "resources":[

   ],
   "detail":{
      "action-status":"succeeded",
      "action":"delete-summary",
      "resource-type":"managed-instance",
      "resource-id":"",
      "action-reason":"The delete for type name Custom:MyInfo was completed. The deletion summary is: {\"totalCount\":2,\"remainingCount\":0,\"summaryItems\":[{\"version\":\"1.0\",\"count\":2,\"remainingCount\":0}]}",
      "type-name":"Custom:MyInfo"
   }
}

關閉自訂庫存類型的警告

{
   "version":"0",
   "id":"49c1855c-9c57-b5d7-8518-b64aeeef5e4a",
   "detail-type":"Inventory Resource State Change",
   "source":"aws.ssm",
   "account":"478678815555",
   "time":"2018-05-24T22:46:58Z",
   "region":"us-east-1",
   "resources":[
      "arn:aws:ssm:us-east-1:478678815555:managed-instance/i-0ee2d86a2cfc371f6"
   ],
   "detail":{
      "action-status":"failed",
      "action":"put",
      "resource-type":"managed-instance",
      "resource-id":"i-0ee2d86a2cfc371f6",
      "action-reason":"The inventory item with type name Custom:MyInfo was sent with a disabled schema version 1.0. You must send a version greater than 1.0",
      "type-name":"Custom:MyInfo"
   }
}

遵循以下處理程序,即可建立適用自訂庫存刪除操作的 EventBridge 規則。這項處理程序會說明如何建立規則,使其可傳送自訂庫存刪除操作通知至 Amazon SNS 主題。開始操作前,請確認您擁有 Amazon SNS 主題,或是建立一個新的主題。如需詳細資訊,請參閱 Amazon Simple Notification Service 開發人員指南中的入門

設定適用庫存刪除操作的 EventBridge
  1. https://console.aws.amazon.com/events/ 開啟 Amazon EventBridge 主控台。

  2. 在導覽窗格中,選擇 Rules (規則)。

  3. 選擇建立規則

  4. 輸入規則的名稱和描述。

    在同一個區域和同一個事件匯流排上,規則不能與另一個規則同名。

  5. 針對 Event bus (事件匯流排),選擇要與此規則建立關聯的事件匯流排。如果您想要此規則回應匹配來自您的 AWS 帳戶 的事件,請選取 default (預設)。當您帳戶中的 AWS 服務 發出事件時,一律會前往您帳戶的預設事件匯流排。

  6. 針對規則類型選擇具有事件模式的規則

  7. 選擇 Next (下一步)

  8. Event source (事件來源) 欄位中,選擇 AWS events or EventBridge partner events (事件或 EventBridge 合作夥伴事件)。

  9. Event pattern (事件模式) 區段中,選擇 Event pattern form (事件模式表單)。

  10. 事件來源欄位中,選擇 AWS 服務

  11. 針對 AWS service ( 服務),請選擇 Systems Manager

  12. Event type (事件類型) 中,選擇 Inventory (庫存)。

  13. 針對 Specific detail type(s) (特定詳細資訊類型),請選擇 Inventory Resource State Change (庫存資源狀態變更)。

  14. 選擇 Next (下一步)

  15. 目標類型欄位中,選擇 AWS 服務

  16. 針對 Select a target (選取目標),選擇 SNS topic (SNS 主題),然後針對 Topic (主題),選擇您的主題。

  17. Additional settings (其他設定) 區段中,針對 Configure target input (設定目標輸入),確認已選取 Matched event (相符的事件)。

  18. 選擇 Next (下一步)

  19. (選用) 為規則輸入一或多個標籤。如需詳細資訊,請參閱《Amazon EventBridge 使用者指南》中的標記您的 Amazon EventBridge 資源

  20. 選擇 Next (下一步)

  21. 檢閱規則的詳細資訊,然後選擇建立規則