How to get Azure Storage metrics through REST API?





I have spent sometime on getting Storage Metrics from Azure REST APIs.

Though Azure documentation is a bit tricky at first instance. I was able to figure out Storage space utilized under each of the storage account.


Note: Below storage metrics are at Storage Account/Blog/Table/Queue level.


AVAILABILITY:

The following example shows how to read metric data at account level:

 GET "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{storageAccountName}/providers/microsoft.insights/metrics?metricnames=Availability&api-version=2018-01-01&aggregation=Average"  

Response:


 {  
  "cost": 0,  
  "timespan": "2017-09-07T17:27:41Z/2017-09-07T18:27:41Z",  
  "interval": "PT1H",  
  "value": [  
   {  
    "id": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{storageAccountName}/providers/Microsoft.Insights/metrics/Availability",  
    "type": "Microsoft.Insights/metrics",  
    "name": {  
     "value": "Availability",  
     "localizedValue": "Availability"  
    },  
    "unit": "Percent",  
    "timeseries": [  
     {  
      "metadatavalues": [],  
      "data": [  
       {  
        "timeStamp": "2017-09-07T17:27:00Z",  
        "average": 100.0  
       }  
      ]  
     }  
    ]  
   }  
  ]  
 }  


USED CAPACITY


The following example shows how to read storage utilization at account level:

 GET "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{storageAccountName}/providers/microsoft.insights/metrics?metricnames=UsedCapacity&api-version=2018-01-01&aggregation=Average"  
This API will give us how much storage is used by the Storage account given in the above URL.
Storage will be shown in Bytes format.
We can cross check this value against Azure Monitor (Metrics).

Pre-requisites:

1. Get OAuth2 Token from Azure Rest API for management.azure.com
This require: Client id (app id from azure AD), Client secret
2. Pass Token to above URLs as Bearer Token Authentication.
3. If you face any permissions issue verify below:
    a. app permissions in Azure AD (Active Directory). Read/Contributor.
    b. API Version may be wrong.
    c. Metric Name should be correct.

Comments