Skip to content

Instantly share code, notes, and snippets.

@ciphertxt
Forked from shawnweisfeld/metrics.ps1
Created August 27, 2020 15:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ciphertxt/9ebf43fb49396aad5ae88c75196cd5db to your computer and use it in GitHub Desktop.
Save ciphertxt/9ebf43fb49396aad5ae88c75196cd5db to your computer and use it in GitHub Desktop.
## Monitor Azure Storage
### https://aka.ms/azuremonitor/storage-metrics
## Azure Monitor Metrics Overview
### https://docs.microsoft.com/en-us/azure/azure-monitor/platform/data-platform-metrics
## Azure Storage monitoring data reference
### https://docs.microsoft.com/en-us/azure/storage/common/monitor-storage-reference
## Azure Monitoring REST API walkthrough
### https://docs.microsoft.com/en-us/azure/azure-monitor/platform/rest-api-walkthrough
## Using the CLI
### https://docs.microsoft.com/en-us/cli/azure/monitor/metrics?view=azure-cli-latest
az login
$sub = "" # Azure Subscription GUID
$rg = "" # Name of resource group with storage account
$acct = "" # Name of storage account
## What metrics are available?
az monitor metrics list-definitions -h
## Account Level Metric Info
### https://docs.microsoft.com/en-us/azure/azure-monitor/platform/metrics-supported#microsoftstoragestorageaccounts
az monitor metrics list-definitions `
--resource "/subscriptions/$sub/resourceGroups/$rg/providers/Microsoft.Storage/storageAccounts/$acct" -o table
az monitor metrics list-definitions `
--resource "/subscriptions/$sub/resourceGroups/$rg/providers/Microsoft.Storage/storageAccounts/$acct" > acct-metrics.json
## Blob Service Metric Info
### https://docs.microsoft.com/en-us/azure/azure-monitor/platform/metrics-supported#microsoftstoragestorageaccountsblobservices
az monitor metrics list-definitions `
--resource "/subscriptions/$sub/resourceGroups/$rg/providers/Microsoft.Storage/storageAccounts/$acct/blobServices/default" -o table
az monitor metrics list-definitions `
--resource "/subscriptions/$sub/resourceGroups/$rg/providers/Microsoft.Storage/storageAccounts/$acct/blobServices/default" > blob-metrics.json
## What are the values for a given metric?
az monitor metrics list -h
## Get latency for the last day, averaged by hour
az monitor metrics list `
--resource "/subscriptions/$sub/resourceGroups/$rg/providers/Microsoft.Storage/storageAccounts/$acct" `
--end-time ([System.DateTime]::UtcNow).ToString("O") `
--offset 1d `
--interval 1h --metric SuccessE2ELatency -o table
## Get Used capacity for the last day (entire storage account)
az monitor metrics list `
--resource "/subscriptions/$sub/resourceGroups/$rg/providers/Microsoft.Storage/storageAccounts/$acct" `
--end-time ([System.DateTime]::UtcNow).ToString("O") `
--offset 1d `
--interval 1h --metric UsedCapacity -o table
## Get Used capacity by temp for the last day (just the blobs in the storage acccount)
az monitor metrics list `
--resource "/subscriptions/$sub/resourceGroups/$rg/providers/Microsoft.Storage/storageAccounts/$acct/blobServices/default" `
--end-time ([System.DateTime]::UtcNow).ToString("O") `
--offset 1d `
--interval 1h --metric BlobCapacity --dimension Tier -o table
## list blob capacity metrics for every storage account in every subscription that I have access to.
az login
# List all my subs
$subs = az account list | ConvertFrom-Json
foreach($sub in $subs)
{
# create a folder for each sub
$subPath = (($sub.name -replace '[^a-zA-Z0-9\s]') + " (" + $sub.id + ")")
If(!(test-path $subPath))
{
New-Item -ItemType Directory -Force -Path $subPath
}
# list all my resource groups in this sub
$rgs = az group list --subscription $sub.id | ConvertFrom-Json
foreach ($rg in $rgs)
{
# create a folder for each resource group
$rgPath = $subPath + '\' + $rg.name
If(!(test-path $rgPath))
{
New-Item -ItemType Directory -Force -Path $rgPath
}
# list all storage accounts in this resource group
$accounts = az storage account list --subscription $sub.id --resource-group $rg.name | ConvertFrom-Json
foreach ($acct in $accounts)
{
$resource = "/subscriptions/" + $sub.id + "/resourceGroups/" + $rg.name + "/providers/Microsoft.Storage/storageAccounts/" + $acct.name + "/blobServices/default"
$file = $rgPath + "\" + $acct.name + ".json"
# export the metrics to a json file
az monitor metrics list `
--resource $resource `
--end-time ([System.DateTime]::UtcNow).ToString("O") `
--offset 1d `
--interval 1h --metric BlobCapacity --dimension Tier > $file
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment