Share via


教學課程:資料收集規則 (DCR)

本教學課程會描述如何編輯已使用命令列工具佈建的資料收集規則 (DCR) 定義。

在本教學課程中,您會了解如何:

  • 利用現有的入口網站功能預先建立 DCR
  • 使用 ARM API 呼叫來取得資料收集規則的內容
  • 使用 ARM API 呼叫將變更套用至資料收集規則
  • 使用 PowerShell 指令碼自動化 DCR 更新程序

注意

本教學課程會逐步解說編輯現有 DCR 的一種方法。 如需其他方法,請參閱 Azure 監視器 中的建立和編輯資料收集規則。

必要條件

若要完成此教學課程,您需要下列項目:

教學課程概觀

雖然在入口網站上透過精靈是設定將自訂資料擷取至 Log Analytics 的最簡單方式,在某些情況下,您可能會想要稍後更新您的資料收集規則:

  • 變更資料收集設定 (例如與 DCR 相關聯的資料收集端點)
  • 更新資料流的資料剖析或篩選邏輯
  • 變更資料目的地 (例如,將資料傳送至 Azure 資料表,因為此選項不會直接提供作為 DCR 型自訂記錄精靈的一部分)

在本教學課程中,您要先設定自訂記錄的擷取。 接著您會修改自訂記錄的 KQL 轉換,以包含其他篩選,並將變更套用至 DCR。 最後,我們會將所有編輯作業合併成單一 PowerShell 指令碼,其可用於根據上述任何原因來編輯任何 DCR。

設定新的自訂記錄

從設定新的自訂記錄開始。 遵循教學課程:使用 Azure 入口網站將自訂記錄傳送至 Azure 監視器記錄 (預覽)。 請記下所建立的 DCR 的資源識別碼。

擷取 DCR 內容

為了更新 DCR,我們將擷取其內容,並將其儲存為檔案,以便進一步編輯。

  1. 按一下 Azure 入口網站中的 [Cloud Shell] 按鈕,並確保已將環境設定為 [PowerShell]

    Screenshot of opening cloud shell

  2. 執行下列命令來擷取 DCR 內容,並將其儲存至檔案。 以 DCR ResourceID 取代 <ResourceId>,並以要儲存 DCR 的檔案名取代 <FilePath>

    $ResourceId = "<ResourceId>" # Resource ID of the DCR to edit
    $FilePath = "<FilePath>" # Store DCR content in this file
    $DCR = Invoke-AzRestMethod -Path ("$ResourceId"+"?api-version=2022-06-01") -Method GET
    $DCR.Content | ConvertFrom-Json | ConvertTo-Json -Depth 20 | Out-File -FilePath $FilePath
    

編輯 DCR

現在,當 DCR 內容儲存為 JSON 檔案時,您可以使用您選擇的編輯器在 DCR 中進行變更。 您可以從 Cloud Shell 環境下載檔案 (如果您有使用該環境)。

或者,您也可以使用隨環境提供的程式碼編輯器。 例如,如果您在雲端磁碟機上名為 temp.dcr 的檔案中儲存 DCR,則可以使用下列命令來開啟 DCR,以直接在 Cloud Shell 視窗中編輯:

code "temp.dcr"

讓我們修改 DCR 內的 KQL 轉換,以捨棄 RequestType 是任何項目的資料列,但 "GET" 除外。

  1. 開啟在上一個部分中建立的檔案,以使用您所選的編輯器進行編輯。
  2. 找出包含 ”transformKql” 屬性的行,而如果您已遵循自訂記錄建立的教學課程,其看起來應該類似這樣:
    "transformKql": "  source\n    | extend TimeGenerated = todatetime(Time)\n    | parse RawData with \n    ClientIP:string\n    ' ' *\n    ' ' *\n    ' [' * '] \"' RequestType:string\n    \" \" Resource:string\n    \" \" *\n    '\" ' ResponseCode:int\n    \" \" *\n    | where ResponseCode != 200\n    | project-away Time, RawData\n"
    
  3. 修改 KQL 轉換以包含依 RequestType 的額外篩選
    "transformKql": "  source\n    | where RawData contains \"GET\"\n     | extend TimeGenerated = todatetime(Time)\n    | parse RawData with \n    ClientIP:string\n    ' ' *\n    ' ' *\n    ' [' * '] \"' RequestType:string\n    \" \" Resource:string\n    \" \" *\n    '\" ' ResponseCode:int\n    \" \" *\n    | where ResponseCode != 200\n    | project-away Time, RawData\n"
    
  4. 儲存 DCR 內容經修改的檔案。

套用變更

最後一個步驟是將 DCR 更新回系統中。 這是透過對 ARM API 的 "PUT" HTTP 呼叫來完成,並在 HTTP 要求本文中傳送更新的 DCR 內容。

  1. 如果您使用 Azure Cloud Shell,請儲存檔案並關閉內嵌的編輯器,或將經修改的 DCR 檔案上傳回 Cloud Shell 環境
  2. 執行下列命令以從檔案載入 DCR 內容,並放置 HTTP 呼叫來更新系統中的 DCR。 以 DCR ResourceID 取代 <ResourceId>,並以教學課程上一部分中修改的檔案名稱取代 <FilePath>。 如果您在相同的 PowerShell 工作階段內讀取和寫入 DCR,則可以省略前兩行。
    $ResourceId = "<ResourceId>" # Resource ID of the DCR to edit
    $FilePath = "<FilePath>" # Store DCR content in this file
    $DCRContent = Get-Content $FilePath -Raw 
    Invoke-AzRestMethod -Path ("$ResourceId"+"?api-version=2022-06-01") -Method PUT -Payload $DCRContent 
    
  3. 成功呼叫時,您應該會收到狀態碼為 "200" 的回應,指出您的 DCR 現在已更新。
  4. 您現在可以瀏覽至 DCR,並透過「JSON 檢視」函式在入口網站上檢查其內容,或者您可以重複教學課程的第一個部分,將 DCR 內容擷取到檔案中。

合併所有元素

現在,當我們知道如何讀取和更新 DCR 的內容時,讓我們將所有項目放在公用程式指令碼中,其可用來一併執行這兩項作業。

param ([Parameter(Mandatory=$true)] $ResourceId)

# get DCR content and put into a file
$FilePath = "temp.dcr"
$DCR = Invoke-AzRestMethod -Path ("$ResourceId"+"?api-version=2022-06-01") -Method GET
$DCR.Content | ConvertFrom-Json | ConvertTo-Json -Depth 20 | Out-File $FilePath

# Open DCR in code editor
code $FilePath | Wait-Process

#Wait for confirmation to apply changes
$Output = Read-Host "Apply changes to DCR (Y/N)? "
if ("Y" -eq $Output.toupper())
{ 
	#write DCR content back from the file
	$DCRContent = Get-Content $FilePath -Raw
	Invoke-AzRestMethod -Path ("$ResourceId"+"?api-version=2022-06-01") -Method PUT -Payload $DCRContent		
}

#Delete temporary file
Remove-Item $FilePath

如何使用此公用程式

假設您已將指令碼儲存為名為 DCREditor.ps1 的檔案,且需要修改資源識別碼為 /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/foo/providers/Microsoft.Insights/dataCollectionRules/bar 的資料收集規則,您可以執行下列命令來完成此作業:

.\DCREditor.ps1 "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/foo/providers/Microsoft.Insights/dataCollectionRules/bar"

DCR 內容會在內嵌程式碼編輯器中開啟。 編輯完成後,在指令碼提示字元中輸入 "Y" 會變更套用回 DCR。

下一步