title | description | author | services | ms.topic | ms.date | ms.author |
---|---|---|---|---|---|---|
Collect Windows VM metrics in Azure Monitor with template |
Send guest OS metrics to the Azure Monitor metric database store by using a Resource Manager template for a Windows virtual machine |
anirudhcavale |
azure-monitor |
conceptual |
05/04/2020 |
bwren |
Send guest OS metrics to the Azure Monitor metric store by using an Azure Resource Manager template for a Windows virtual machine
Performance data from the guest OS of Azure virtual machines is not collected automatically like other platform metrics. Install the Azure Monitor diagnostics extension to collect guest OS metrics into the metrics database so it can be used with all features of Azure Monitor Metrics, including near-real time alerting, charting, routing, and access from a REST API. This article describes the process for sending Guest OS performance metrics for a Windows virtual machine to the metrics database using a Resource Manager template.
Note
For details on configuring the diagnostics extension to collect guest OS metrics using the Azure portal, see Install and configure Windows Azure diagnostics extension (WAD).
If you're new to Resource Manager templates, learn about template deployments and their structure and syntax.
-
Your subscription must be registered with Microsoft.Insights.
-
You need to have either Azure PowerShell or Azure Cloud Shell installed.
-
Your VM resource must be in a region that supports custom metrics.
The Azure Diagnostics extension uses a feature called "data sinks" to route metrics and logs to different locations. The following steps show how to use a Resource Manager template and PowerShell to deploy a VM by using the new "Azure Monitor" data sink.
For this example, you can use a publicly available sample template. The starting templates are at https://github.com/Azure/azure-quickstart-templates/tree/master/quickstarts/microsoft.compute/vm-simple-windows.
-
Azuredeploy.json is a preconfigured Resource Manager template for the deployment of a virtual machine.
-
Azuredeploy.parameters.json is a parameters file that stores information such as what user name and password you would like to set for your VM. During deployment, the Resource Manager template uses the parameters that are set in this file.
Download and save both files locally.
Open the azuredeploy.parameters.json file
-
Enter values for adminUsername and adminPassword for the VM. These parameters are used for remote access to the VM. To avoid having your VM hijacked, DO NOT use the values in this template. Bots scan the internet for user names and passwords in public GitHub repositories. They are likely to be testing VMs with these defaults.
-
Create a unique dnsname for the VM.
Open the azuredeploy.json file
Add a storage account ID to the variables section of the template after the entry for storageAccountName.
// Find these lines.
"variables": {
"storageAccountName": "[concat(uniquestring(resourceGroup().id), 'sawinvm')]",
// Add this line directly below.
"accountid": "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]",
Add this Managed Service Identity (MSI) extension to the template at the top of the resources section. The extension ensures that Azure Monitor accepts the metrics that are being emitted.
//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
}
}
},
Add the identity configuration to the VM resource to ensure that Azure assigns a system identity to the MSI extension. This step ensures that the VM can emit guest metrics about itself to Azure Monitor.
// 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": {
...
Add the following configuration to enable the Diagnostics extension on a Windows virtual machine. For a simple Resource Manager-based virtual machine, we can add the extension configuration to the resources array for the virtual machine. The line "sinks"— "AzMonSink" and the corresponding "SinksConfig" later in the section—enable the extension to emit metrics directly to Azure Monitor. Feel free to add or remove performance counters as needed.
"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
Save and close both files.
Note
You must be running the Azure Diagnostics extension version 1.5 or higher AND have the autoUpgradeMinorVersion: property set to ‘true’ in your Resource Manager template. Azure then loads the proper extension when it starts the VM. If you don't have these settings in your template, change them and redeploy the template.
To deploy the Resource Manager template, we leverage Azure PowerShell.
-
Launch PowerShell.
-
Log in to Azure using
Login-AzAccount
. -
Get your list of subscriptions by using
Get-AzSubscription
. -
Set the subscription that you're using to create/update the virtual machine in:
Select-AzSubscription -SubscriptionName "<Name of the subscription>"
-
To create a new resource group for the VM that's being deployed, run the following command:
New-AzResourceGroup -Name "<Name of Resource Group>" -Location "<Azure Region>"
[!NOTE] Remember to use an Azure region that is enabled for custom metrics.
-
Run the following commands to deploy the VM using the Resource Manager template.
[!NOTE] If you wish to update an existing VM, simply add -Mode Incremental to the end of the following command.
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>"
-
After your deployment succeeds, the VM should be in the Azure portal, emitting metrics to Azure Monitor.
[!NOTE] You might run into errors around the selected vmSkuSize. If this happens, go back to your azuredeploy.json file, and update the default value of the vmSkuSize parameter. In this case, we recommend trying "Standard_DS1_v2").
-
Log in to the Azure portal.
-
On the left menu, select Monitor.
-
On the Monitor page, select Metrics.
-
Change the aggregation period to Last 30 minutes.
-
In the resource drop-down menu, select the VM that you created. If you didn't change the name in the template, it should be SimpleWinVM2.
-
In the namespaces drop-down menu, select azure.vm.windows.guest
-
In the metrics drop down menu, select Memory%Committed Bytes in Use.
- Learn more about custom metrics.