How to enable diagnostics in windows VM using Azure REST API

Azure diagnostics extension can provide monitor and diagnostic functions on Azure virtual machine. You can include the diagnostic feature when creating Azure virtual machine if you add the diagnostic extension to the template, which is introduced in this article.If you have created a Azure virtual machine without the diagnostic extension,you can enable it separately. Here is an blog which provides the steps on how to implement it in Azure portal. Today I will show you how to enable diagnostic in a existed windows virtual machine using Azure REST API.

Preparation:

  1. You must have a Azure subscription,you can get it here.
  2. an Azure windows virtual machine,you can refer here on how to create a virtual machine on Azure.

Get access token

You need to get Access_token to authenticate your REST call. You can get it by Azure REST API. To demonstrate how to negotiate with Azure server, here I use Restlet Client as our http tool.

Method: POST

URL: https://login.windows.net/{DirectoryID}/oauth2/token

Header: Content-Type: application/x-www-form-urlencoded

Body: grant_type=client_credentials&client_id={Application ID}&client_secret={key}&resource=https://management.core.windows.net/

You can get the DirectoryID from portal website->Azure Active Directory->properties, Application ID and key from portal website->Azure Active Directory->App registration->the application you register before.When the request succeed, you can get the Access_token:

Enable diagnostic by Azure REST API:

Now you can use the Azure REST API to enable diagnostic. Below is the request you should send to Azure.

Method

PUT

HEADERS:

Authorization:Bearer {Access_token}
Content-Type:application/json

Request URI

https://management.azure.com/subscriptions/{SubscriptionId}/resourceGroups/{ResourceGroupName}/providers/Microsoft.Compute/virtualMachines/{VMName}/extensions/IaaSDiagnostics?api-version=2016-04-30-preview

Request BODY

{
 "properties": {
 "publisher": "Microsoft.Azure.Diagnostics",
 "type": "IaaSDiagnostics",
 "typeHandlerVersion": "1.5",
 "autoUpgradeMinorVersion": true,
 "settings": {
 "StorageAccount": "{storageAccountName}",
 "WadCfg": "base64(xml file)"
 },
 "protectedSettings": {
 "storageAccountName": "{storageAccountName}",
 "storageAccountKey": "{storageAccountKey}"
 }
 },
 "type": "Microsoft.Compute/virtualMachines/extensions",
 "location": "{VM region}",
 "id": "/subscriptions/{SubscriptionId}/resourceGroups/{ResourceGroupName}/providers/Microsoft.Compute/virtualMachines/{VMName}/extensions/IaaSDiagnostics",
 "name": "IaaSDiagnostics"
}

The value of WadCfg mentioned above is  the value of xml file after encoding by base64,it is the initialization configuration of enabling diagnostic. For more details on diagnostics configuration, you can refer here. The following is an example of a minimal configuration file, providing for the capture and persistence of a single performance counter and the System event log:

<?xml version="1.0" encoding="utf-8"?>
<PublicConfig xmlns="http://schemas.microsoft.com/ServiceHosting/2010/10/DiagnosticsConfiguration">
 <WadCfg>
 <DiagnosticMonitorConfiguration overallQuotaInMB="25000">
 <PerformanceCounters scheduledTransferPeriod="PT1M">
 <PerformanceCounterConfiguration counterSpecifier="\Processor(_Total)\% Processor Time" sampleRate="PT1M" unit="percent"><annotation displayName="Processor - total processor time" locale="en-us"/></PerformanceCounterConfiguration>
 </PerformanceCounters>
 <WindowsEventLog scheduledTransferPeriod="PT1M"><DataSource name="System!*"/></WindowsEventLog>
 <Metrics resourceId="/subscriptions/{SubscriptionId}/resourceGroups/{ResourceGroupName}/providers/Microsoft.Compute/virtualMachines/{VMName}"><MetricAggregation scheduledTransferPeriod="PT1H"/><MetricAggregation scheduledTransferPeriod="PT1M"/></Metrics>
 </DiagnosticMonitorConfiguration>
 </WadCfg>
</PublicConfig>

After the configuration of subscriptionId、resourceGroupName and VMName,encode the xml file by base64,then get the value:

<ICZsdDtXY…mcmZ3Q7>

After successful calling of the above API, it returns 201 created and have a provisioningState field in the returned results, values are: creating. Below is the screenshot:

Wait about 1 minute, then call the API GET method, you can view the value of the provisioningState field, it means that you enable the diagnostic successfully if the value is succeed. Below is the screenshot:

It’s all the steps about how to enable diagnostic by Azure REST API. Here the most important thing is wadcfg configuration. It decides what metrics you can collect. But you don’t need to write it by yourself. You can get a default configuration file from Azure portal.

I hope this helps. Please contact us if you have any question.

 

Leave a Reply

Your email address will not be published. Required fields are marked *