使用自訂庫存 - AWS Systems Manager

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

使用自訂庫存

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

注意

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

若要將自訂清查指派給節點,您可以使用「Systems Manager」PutInventoryAPI作業,如中所述將自訂庫存中繼資料指派給受管理節點。或者,您可以建立自訂詳細目錄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結構描述需要SchemaVersionTypeName、和Content區段,但您可以在這些區段中定義資訊。

{ "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部分包括屬性和 data。 這些項目不區分大小寫。但是,如果您定義屬性 (例如:"Vendor「:" DELL「),則必須在自訂詳細目錄檔案中一致地參照此屬性。如果在一個檔案中指定 Vendor DELL "": "(在中使用大寫字母「V」vendor),然後在另一個檔案中指定 vendor DELL "": "(在中使用小寫字母「v」vendor),系統會傳回錯誤。

注意

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

建立檔案之後,請務必將其儲存在節點上。下表顯示節點上必須儲存自訂詳細目錄JSON檔案的位置。

作業系統 路徑

Linux

/var/利布/亞馬遜/SSM/node-id/庫存/自定義

macOS

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

Windows

% SystemDrive %\ProgramData\ AmazonSSM\InstanceData\node-id\ 庫存\ 自定義

如需如何使用自訂清查的範例,請參閱使用 EC2Systems Manager 自訂清查類型取得叢集的磁碟使用率

刪除自訂清查

您可以使用此DeleteInventoryAPI作業刪除自訂詳細目錄類型,以及與該類型相關聯的資料。您可以使用 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 ,剩餘計數總是相同的,因為系統尚未刪除任何內容。您可以使用 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"

了解刪除清查摘要

為了協助您更充分了解庫存摘要刪除操作的內容,請參考以下範例。將自訂: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

您可以 EventBridge 將 Amazon 設定為在使用者刪除自訂庫存時建立事件。 EventBridge 針對自訂庫存刪除作業提供三種類型的事件:

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

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

  • 關閉自訂詳細目錄類型的警告:如果使用者呼叫先前已關閉的自訂詳細目錄類型版本PutInventoryAPI作業,則會發出警告事件。

下方是每個事件的範例。

執行個體的刪除動作

{
   "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. 在打開 Amazon EventBridge 控制台https://console.aws.amazon.com/events/

  2. 在導覽窗格中,選擇規則

  3. 選擇建立規則

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

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

  5. 針對事件匯流排,選擇要與此規則建立關聯的事件匯流排。如果您希望此規則回應來自您自己的相符事件 AWS 帳戶,請選取預設值。當您帳戶 AWS 服務 中的某個事件發出時,它始終會進入您帳戶的默認事件總線。

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

  7. 選擇 Next (下一步)

  8. 對於事件來源,請選擇AWS 事件或 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. 針對 [選取目標] 選擇 [SNS主題],然後針對 [主題] 選擇您的主題。

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

  18. 選擇 Next (下一步)

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

  20. 選擇 Next (下一步)

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