How to Get Diagnostics Info for Azure/PHP Applications–Part 2

June 7, 2012 update: The Microsoft Windows Azure team has released a new Windows Azure SDK for PHP. This release is part of an effort to keep PHP client libraries up to date with new Windows Azure features and to make PHP a first-class citizen in Windows Azure. The latest client libraries are on GitHub: https://github.com/WindowsAzure/azure-sdk-for-php. While the SDK hosted on CodePlex will continue to work for the foreseeable future, it is strongly recommended that new PHP/Windows Azure application use the SDK hosted on GitHub.

The work done by Maarten Balliauw and other contributors in building the SDK hosted on CodePlex was critical in unifying the PHP developer experience for Windows Azure. The Windows Azure team is grateful to these contributors for their pioneering work and looks forward to their continued support (and yours!) in adding to the new SDK on GitHub.

Thanks,

      
The Windows Azure Team


In part 1 of this two part series, I showed how you can use a configuration file (the diagnostics.wadcfg file) to configure Azure diagnostics. However, that approach requires that you know all the diagnostic information that you want to collect before you deploy your application. In this post, I’ll show how you can use the Windows Azure SDK for PHP API to configure diagnostic information after an application has been deployed to Windows Azure.

Recall from part 1 that the Windows Azure Diagnostics Monitor persists your diagnostics configuration in your Azure Blob storage (in a container called wad-control-container). One configuration file is stored for each role instance your are running (the name of each file is of the format <deployment_id>/<role_name>/<role_instance_name>). You could, of course, download the configuration files (they are XML files), edit them, and upload them to your Blob storage. The Diagnostics Monitor would, on it’s configured schedule, check for updates and apply them. However, it would be much more fun to write code to do this for us. So, from here, I’ll walk you through a PHP script that turns on the collection of 3 performance counters. I’ll provide the complete script at the end, and provide some guidance as to how you can turn on other diagnostics. (I’ll assume that you have read part 1 of this series.)

First, I’ll include the classes I’ll use:

 require_once 'Microsoft/WindowsAzure/Storage/Blob.php'; 
 require_once 'Microsoft/WindowsAzure/Diagnostics/Manager.php';
 require_once 'Microsoft/WindowsAzure/Management/Client.php';

Next, I’ll define several constants I’ll use in my script:

 // Define constants for using Blob and Client classes.
 define("STORAGE_ACCOUNT_NAME", "your_storage_account_name");
 define("STORAGE_ACCOUNT_KEY", "your_storage_account_key");
 define("ROLE_NAME", "PhpOnAzure.Web");
 define("SID", 'your_azure_subscription_id');
 define("CERTIFICATE", 'path\to\your\management\certificate.pem');
 define("PASSPHRASE", 'your_certificate_passphrase');
 define("DNS_PREFIX", 'dns_prefix_for_your_hosted_service');
 define("SLOT", 'production'); //production or staging

A couple notes about these constants:

Now I’ll use the Client, Blob, and Manager classes to get deployment information and current diagnostic configuration (from my blob storage). I’ll also define an array that contains the performance counters I want to collect:

 // Get deployment
 $client = new Microsoft_WindowsAzure_Management_Client(SID, CERTIFICATE, PASSPHRASE);
 $deployment = $client->getDeploymentBySlot(DNS_PREFIX, SLOT);
  
 // Create diagnostics manager
 $blob = new Microsoft_WindowsAzure_Storage_Blob('blob.core.windows.net', STORAGE_ACCOUNT_NAME, STORAGE_ACCOUNT_KEY);
 $manager = new Microsoft_WindowsAzure_Diagnostics_Manager($blob);
  
 // Specify performance counters to collect.
 $counters = array('\Processor(_Total)\% Processor Time', '\Memory\Available Mbytes', '\TCPv4\Connections Established' );

And finally, I’ll loop through the role instances for my deployment and turn on the performance counters I defined above.

 // Create and set a configuration for each Web role.
 foreach($deployment->roleinstancelist as $index => $value) {
     if($value['rolename'] == ROLE_NAME) {
         $role_id = $deployment->privateid."/".ROLE_NAME."/".ROLE_NAME."_IN_".$index;
         $configuration = $manager->getConfigurationForRoleInstance($role_id); 
         foreach($counters as $c) { 
             $configuration->DataSources->PerformanceCounters->addSubscription($c, "60");
         } 
         $configuration->DataSources->OverallQuotaInMB = 10;
         $configuration->DataSources->PerformanceCounters->BufferQuotaInMB = 10;
         $configuration->DataSources->PerformanceCounters->ScheduledTransferPeriodInMinutes = 1; 
         $manager->setConfigurationForRoleInstance($role_id,$configuration);
     }
 }

A couple of notes about the code above:

  • This code assumes that you might have multiple roles for a deployment (e.g. a worker role). It will only turn on performance counters for the specified ROLE_NAME.
  • The getConfigurationForRoleInstance method on the Manager class requires a “role Id” parameter. The role Id is of this format: <deployment id>/<role name>/<role name>_IN_<instance index>.
  • The DataSources property on the Configuration class has several properties that you can set:  overallquotainmb, logs, diagnosticinfrastructurelogs, performancecounters, windowseventlog, and directories.

That’s it. Within a few minutes after executing that script you should begin seeing performance counter data written to the WADPerformanceCountersTable table in your Windows Azure Table storage.

Collecting other diagnostic information is similar. However, you’ll need to look more closely at these classes in the SDK to figure out the details:

  • ConfigurationDiagnosticsInfrasctuctureLogs
  • ConfigurationDirectories
  • ConfigurationLogs
  • ConfigurationWindowsEventLog

I’d be happy to dive into the details of using any of those classes…just post a comment!

Thanks.

-Brian

Share this on Twitter

Oh yes, and here’s the complete script, as promised:

 <?php
 require_once 'Microsoft/WindowsAzure/Storage/Blob.php'; 
 require_once 'Microsoft/WindowsAzure/Diagnostics/Manager.php';
 require_once 'Microsoft/WindowsAzure/Management/Client.php';
  
 // Define constants for using Blob and Client classes.
 define("STORAGE_ACCOUNT_NAME", "your_storage_account_name");
 define("STORAGE_ACCOUNT_KEY", "your_storage_account_key");
 define("ROLE_NAME", "PhpOnAzure.Web");
 define("SID", 'your_azure_subscription_id');
 define("CERTIFICATE", 'path\to\your\management\certificate.pem');
 define("PASSPHRASE", 'your_certificate_passphrase');
 define("DNS_PREFIX", 'dns_prefix_for_your_hosted_service');
 define("SLOT", 'production'); //production or staging
  
 // Get deployment
 $client = new Microsoft_WindowsAzure_Management_Client(SID, CERTIFICATE, PASSPHRASE);
 $deployment = $client->getDeploymentBySlot(DNS_PREFIX, SLOT);
  
 // Create diagnostics manager
 $blob = new Microsoft_WindowsAzure_Storage_Blob('blob.core.windows.net', STORAGE_ACCOUNT_NAME, STORAGE_ACCOUNT_KEY);
 $manager = new Microsoft_WindowsAzure_Diagnostics_Manager($blob);
  
 // Specify performance counters to collect.
 $counters = array('\Processor(_Total)\% Processor Time', '\Memory\Available Mbytes', '\TCPv4\Connections Established' ); 
  
 // Create and set a configuration for each Web role.
 foreach($deployment->roleinstancelist as $index => $value) {
     if($value['rolename'] == ROLE_NAME) {
         $role_id = $deployment->privateid."/".ROLE_NAME."/".ROLE_NAME."_IN_".$index;
         $configuration = $manager->getConfigurationForRoleInstance($role_id); 
         foreach($counters as $c) { 
             $configuration->DataSources->PerformanceCounters->addSubscription($c, "60");
         } 
         $configuration->DataSources->OverallQuotaInMB = 10;
         $configuration->DataSources->PerformanceCounters->BufferQuotaInMB = 10;
         $configuration->DataSources->PerformanceCounters->ScheduledTransferPeriodInMinutes = 1; 
         $manager->setConfigurationForRoleInstance($role_id,$configuration);
     }
 }
 ?>