I want to retrieve the size of an azure storage account without using the portal (metrics). How can I get the metrics of the storage account through azure CLI or bash script? Is there a way to make this through azure CLI or any bash scripts?
Looking at the AZ CLI commands, I believe there's no command currently available that will give you this information directly.
What you will need to do is make use of az rest
and invoke Metrics - List
REST API and parse the response.
Here's the command you would want to execute:
az rest --uri https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Storage/storageAccounts/<storage-account-name>/providers/Microsoft.Insights/metrics?api-version=2018-01-01&metricnames=UsedCapacity&aggregation=Average
You would get a response like the following:
{
"cost": 59,
"interval": "PT1H",
"namespace": "Microsoft.Storage/storageAccounts",
"resourceregion": "resource-location",
"timespan": "2021-10-27T05:12:06Z/2021-10-27T06:12:06Z",
"value": [
{
"displayDescription": "The amount of storage used by the storage account. For standard storage accounts, it's the sum of capacity used by blob, table, file, and queue. For premium storage accounts and Blob storage accounts, it is the same as BlobCapacity or FileCapacity.",
"errorCode": "Success",
"id": "/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Storage/storageAccounts/<storage-account-name>/providers/Microsoft.Insights/metrics/UsedCapacity",
"name": {
"localizedValue": "Used capacity",
"value": "UsedCapacity"
},
"resourceGroup": "cerebrata",
"timeseries": [
{
"data": [
{
"average": 9078827149.0,//This is the value you would want to extract
"timeStamp": "2021-10-27T05:12:00Z"
}
],
"metadatavalues": []
}
],
"type": "Microsoft.Insights/metrics",
"unit": "Bytes"
}
]
}