使用 Windows VM 的 ARM 範本,將客體 OS 計量傳送至 Azure 監視器計量存放區

如同其他平台計量,系統不會自動收集 Azure 虛擬機器 (VM) 客體 OS 的效能資料。 安裝 Azure 監視器診斷延伸模組,將客體 OS 計量收集到計量資料庫中,以便搭配 Azure 監視器計量的所有功能使用。 這些功能包括近乎即時的警示、圖表、路由,以及從 REST API 存取。 本文說明使用 Azure Resource Manager 範本 (ARM 範本) 將 Windows VM 的客體 OS 效能計量傳送至計量資料庫的程序。

注意

如需使用 Azure 入口網站設定診斷延伸模組來收集客體 OS 計量的詳細資訊,請參閱安裝和設定 Windows Azure 診斷 (WAD) 延伸模組

如果您剛開始使用 ARM 範本,請了解範本部署與其結構和語法。

必要條件

設定 Azure 監視器作為資料接收器

Azure 診斷擴充功能會使用稱為資料接收器的功能,將計量與記錄路由傳送至不同的位置。 下列步驟示範如何使用新的 Azure 監視器資料接收器,使用 ARM 範本與 PowerShell 來部署 VM。

ARM 範本

針對此範例,您可以使用公開提供的範例範本。 起始範本位於 GitHub

  • Azuredeploy.json:用於部署 VM 的預先設定 ARM 範本。
  • Azuredeploy.parameters.json:參數檔案,儲存您要為 VM 設定的使用者名稱與密碼等資訊。 在部署期間,ARM 範本會使用此檔案中設定的參數。

下載這兩個檔案並儲存在本機。

修改 azuredeploy.parameters.json

  1. 開啟 azuredeploy.parameters.json 檔案。

  2. 為 VM 輸入 adminUsernameadminPassword 的值。 這些參數可用來從遠端存取 VM。 若要避免該 VM 被駭,請「勿」使用此範本中的值。 Bot 會掃描網際網路,取得公用 GitHub 存放庫中的使用者名稱與密碼。 使用這些預設值的 VM 可能是測試 VM。

  3. 為 VM 建立唯一 dnsname

修改 azuredeploy.json

  1. 開啟 azuredeploy.json 檔案。

  2. storageAccountName 項目後面,將儲存體帳戶識別碼新增至範本的 variables 區段。

    // Find these lines.
    "variables": {
        "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'sawinvm')]",
    
    // Add this line directly below.
        "accountid": "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]",
    
  3. 將此受控服務識別 (MSI) 延伸模組新增至 resources 區段頂端的範本。 此擴充功能可確保 Azure 監視器接受所發出的計量。

    //Find this code.
    "resources": [
    // Add this code directly below.
        {
            "type": "Microsoft.Compute/virtualMachines/extensions",
            "name": "[concat(variables('vmName'), '/', 'WADExtensionSetup')]",
            "apiVersion": "2017-12-01",
            "location": "[resourceGroup().location]",
            "dependsOn": [
                "[concat('Microsoft.Compute/virtualMachines/', variables('vmName'))]" ],
            "properties": {
                "publisher": "Microsoft.ManagedIdentity",
                "type": "ManagedIdentityExtensionForWindows",
                "typeHandlerVersion": "1.0",
                "autoUpgradeMinorVersion": true,
                "settings": {
                    "port": 50342
                }
            }
        },
    
  4. identity 設定新增至 VM 資源,確保 Azure 會將系統身分識別指定給 MSI 延伸模組。 這個步驟可確保 VM 能對 Azure 監視器發出與本身相關的客體計量。

    // Find this section
                    "subnet": {
                "id": "[variables('subnetRef')]"
                }
            }
            }
        ]
        }
    },
    {
        "apiVersion": "2017-03-30",
        "type": "Microsoft.Compute/virtualMachines",
        "name": "[variables('vmName')]",
        "location": "[resourceGroup().location]",
        // add these 3 lines below
        "identity": {
        "type": "SystemAssigned"
        },
        //end of added lines
        "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
        "[resourceId('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
        ],
        "properties": {
        "hardwareProfile": {
        ...
    
  5. 新增下列設定以在 Windows VM 上啟用診斷延伸模組。 針對簡易的 Resource Manager 型 VM,將延伸模組設定新增至 VM 的資源陣列。 "sinks": "AzMonSink" 這一行與本節稍後的對應 "SinksConfig" 可讓延伸模組直接對 Azure 監視器發出計量。 請視需要新增或移除效能計數器。

            "networkProfile": {
                "networkInterfaces": [
                {
                    "id": "[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]"
                }
                ]
            },
    "diagnosticsProfile": {
        "bootDiagnostics": {
        "enabled": true,
        "storageUri": "[reference(resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))).primaryEndpoints.blob]"
        }
    }
    },
    //Start of section to add
    "resources": [
    {
                "type": "Microsoft.Compute/virtualMachines/extensions",
                "name": "[concat(variables('vmName'), '/', 'Microsoft.Insights.VMDiagnosticsSettings')]",
                "apiVersion": "2017-12-01",
                "location": "[resourceGroup().location]",
                "dependsOn": [
                "[concat('Microsoft.Compute/virtualMachines/', variables('vmName'))]"
                ],
                "properties": {
                "publisher": "Microsoft.Azure.Diagnostics",
                "type": "IaaSDiagnostics",
                "typeHandlerVersion": "1.12",
                "autoUpgradeMinorVersion": true,
                "settings": {
                    "WadCfg": {
                    "DiagnosticMonitorConfiguration": {
        "overallQuotaInMB": 4096,
        "DiagnosticInfrastructureLogs": {
                        "scheduledTransferLogLevelFilter": "Error"
            },
                        "Directories": {
                        "scheduledTransferPeriod": "PT1M",
        "IISLogs": {
                            "containerName": "wad-iis-logfiles"
                        },
                        "FailedRequestLogs": {
                            "containerName": "wad-failedrequestlogs"
                        }
                        },
                        "PerformanceCounters": {
                        "scheduledTransferPeriod": "PT1M",
                        "sinks": "AzMonSink",
                        "PerformanceCounterConfiguration": [
                            {
                            "counterSpecifier": "\\Memory\\Available Bytes",
                            "sampleRate": "PT15S"
                            },
                            {
                            "counterSpecifier": "\\Memory\\% Committed Bytes In Use",
                            "sampleRate": "PT15S"
                            },
                            {
                            "counterSpecifier": "\\Memory\\Committed Bytes",
                            "sampleRate": "PT15S"
                            }
                        ]
                        },
                        "WindowsEventLog": {
                        "scheduledTransferPeriod": "PT1M",
                        "DataSource": [
                            {
                            "name": "Application!*"
                            }
                        ]
                        },
                        "Logs": {
                        "scheduledTransferPeriod": "PT1M",
                        "scheduledTransferLogLevelFilter": "Error"
                        }
                    },
                    "SinksConfig": {
                        "Sink": [
                        {
                            "name" : "AzMonSink",
                            "AzureMonitor" : {}
                        }
                        ]
                    }
                    },
                    "StorageAccount": "[variables('storageAccountName')]"
                },
                "protectedSettings": {
                    "storageAccountName": "[variables('storageAccountName')]",
                    "storageAccountKey": "[listKeys(variables('accountid'),'2015-06-15').key1]",
                    "storageAccountEndPoint": "https://core.windows.net/"
                }
                }
            }
            ]
    //End of section to add
    
  6. 儲存並關閉這兩個檔案。

部署 ARM 範本

注意

您必須執行 Azure 診斷延伸模組 1.5 版或更高版本,「且」在 ARM 範本中將 autoUpgradeMinorVersion: 屬性設定為 true。 接著,Azure 會在啟動 VM 時載入適當的擴充功能。 如果您的範本中沒有這些設定,請進行變更,並重新部署該範本。

我們使用 Azure PowerShell 來部署 ARM 範本。

  1. 啟動 PowerShell。

  2. 使用 Login-AzAccount 登入 Azure。

  3. 使用 Get-AzSubscription 取得訂用帳戶清單。

  4. 設定您用來建立/更新 VM 的訂用帳戶:

    Select-AzSubscription -SubscriptionName "<Name of the subscription>"
    
  5. 若要為部署的 VM 建立新的資源群組,請執行下列命令:

     New-AzResourceGroup -Name "<Name of Resource Group>" -Location "<Azure Region>"
    
  6. 執行下列命令以使用 ARM 範本部署 VM。

    注意

    如果您想要更新現有 VM,請將 -Mode Incremental 新增至下列命令的結尾。

    New-AzResourceGroupDeployment -Name "<NameThisDeployment>" -ResourceGroupName "<Name of the Resource Group>" -TemplateFile "<File path of your Resource Manager template>" -TemplateParameterFile "<File path of your parameters file>"
    
  7. 部署成功之後,VM 應位於 Azure 入口網站中,向 Azure 監視器發出計量。

    注意

    您可能會遇到和所選 vmSkuSize 相關的錯誤。 如果發生這種情況,請回到 azuredeploy.json 檔案,然後更新 vmSkuSize 參數的預設值。 在此案例中,建議您嘗試使用 "Standard_DS1_v2")。

繪製計量圖表

  1. 登入 Azure 入口網站。

  2. 在左側功能表上,選取 [監視]

  3. 在 [監視] 頁面上,選取 [計量]

    Screenshot that shows the Metrics page.

  4. 將彙總期間變更為 [過去 30 分鐘]

  5. 在 [資源] 下拉式功能表中,選取您所建立的 VM。 如果您沒有變更範本中的名稱,它應該是 SimpleWinVM2

  6. 在 [命名空間] 下拉式功能表中,選取 [azure.vm.windows.guestmetrics]

  7. 在 [計量] 下拉式清單中,選取 [Memory%Committed Bytes in Use]

下一步

深入了解自訂計量