Freigeben über


Sample command-line utility to enable Storage Analytics on Windows Azure

The Windows Azure Storage team has has recently announced the availability of a widely requested feature: Windows Azure Storage Analytics to track, analyze, and debug your usage of storage (Blobs, Tables and Queues). Here are a few additional resources:

I wanted to play with it a little bit too, so I came up with a simple command-line tool to interact with the Storage Analytics configuration; there is no GUI today to play with the different options, and you have to use the REST API to interact with the configuration properties. Steve’s example allows you to modify the configuration properties via a Web interface, but I wanted to create a simple-command line tool you could run directly from your machine.

Usage:

Download the MSI installer package and run it on your machine. It should end up somewhere like C:\Program Files (x86)\Microsoft\Storage Analytics command-line tool\ . You can now open a command prompt and change to that directory.

The first thing you want to do is to retrieve the Storage Analytics configuration:

StorageAnalytics.exe -a <account> -k <key> -t blob -get

You can replace “-t blob” with “-t table” if you want to work on Table Storage instead of Blob Storage.

This will display your current configuration as an XML file, like this:

 <StorageServiceProperties>
  <Logging>
    <Version>1.0</Version>
    <Read>true</Read>
    <Write>true</Write>
    <Delete>false</Delete>
    <RetentionPolicy>
      <Enabled>false</Enabled>
    </RetentionPolicy>
  </Logging>
  <Metrics>
    <Version>1.0</Version>
    <Enabled>true</Enabled>
    <IncludeAPIs>true</IncludeAPIs>
    <RetentionPolicy>
      <Enabled>false</Enabled>
    </RetentionPolicy>
  </Metrics>
</StorageServiceProperties>

You can redirect the XML output to a local file by appending “> file.xml” to the command line:

StorageAnalytics.exe -a <account> -k <key> -t blob –get > C:\temp\blob.xml

You can then edit the XML file to change the configuration.

To upload the modfied file back to Windows Azure, this is the syntax you need:

StorageAnalytics.exe -a <account> -k <key> -t blob -set C:\temp\blob.xml

Be careful with the XML syntax (read the documentation). Windows Azure will return an error code if your XML is incorrect.

Finally, I have added an option to list the available logs (you have to wait for a little while before they appear):

StorageAnalytics.exe -a <account> -k <key> -t blob -list

This should show you something like this:

 <EnumerationResults ContainerName="https://tcontepub.blob.core.windows.net/$logs/">
  <Blobs>
    <Blob>
      <Name>blob/2011/08/10/1600/000000.log</Name>
      <Url>https://tcontepub.blob.core.windows.net/$logs/blob/2011/08/10/1600/000000.log</Url>
      <Properties>
        <Last-Modified>Wed, 10 Aug 2011 16:21:01 GMT</Last-Modified>
        <Etag>0x8CE2590A11EE39A</Etag>
        <Content-Length>586</Content-Length>
        <Content-Type>application/octet-stream</Content-Type>
        <Content-Encoding />
        <Content-Language />
        <Content-MD5 />
        <Cache-Control />
        <BlobType>BlockBlob</BlobType>
        <LeaseStatus>unlocked</LeaseStatus>
      </Properties>
    </Blob>
    <Blob>
      <Name>blob/2011/08/10/1600/000001.log</Name>
      <Url>https://tcontepub.blob.core.windows.net/$logs/blob/2011/08/10/1600/000001.log</Url>
      <Properties>
        <Last-Modified>Wed, 10 Aug 2011 16:29:47 GMT</Last-Modified>
        <Etag>0x8CE2591DADFAEDF</Etag>
        <Content-Length>294</Content-Length>
        <Content-Type>application/octet-stream</Content-Type>
        <Content-Encoding />
        <Content-Language />
        <Content-MD5 />
        <Cache-Control />
        <BlobType>BlockBlob</BlobType>
        <LeaseStatus>unlocked</LeaseStatus>
      </Properties>
    </Blob>

… and so on.

I hope you will find this tool useful as is, however I think it you will certainly want to adapt it to your needs!

Here is the source code for the whole tool (a single Program.cs C# file). It uses the Windows Azure StorageClient library in order to sign outgoing HTTP requests, and the excellent NDesk.Options library to parse command-line parameters (included via NuGet).

You can also download a ZIP archive of the complete Visual Studio 2010 SP1 solution. You may need to change the location of the StorageClient DLL: C:\Program Files\Windows Azure SDK\v1.4\ref\Microsoft.WindowsAzure.StorageClient.dll.

You can open the gist directly on GitHub if you prefer.

Cheers!

Thomas.