如何检查我在每个Azure存储帐户中使用了多少空间(按我的订阅资源组划分)。
我无法通过PowerShell、CLI、门户等方式检查Azure存储帐户中使用的空间。
如何检查我在每个Azure存储帐户中使用了多少空间(按我的订阅资源组划分)。
我无法通过PowerShell、CLI、门户等方式检查Azure存储帐户中使用的空间。
在大量搜索之后,我发现这篇文章非常相关:
使用Azure Monitor来检查存储账户的容量。
步骤:这是一个我使用的.net core
脚本,用于列出存储帐户使用情况,使用过去一小时平均指标值。
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Microsoft.Azure.Management.CosmosDB.Fluent.Models;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.Monitor;
using Microsoft.Azure.Management.Monitor.Models;
using Microsoft.Rest.Azure.Authentication;
namespace storagelist
{
class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
// to generate my.azureauth file run the follow command:
// az ad sp create-for-rbac --sdk-auth > my.azureauth
var azure = Azure.Authenticate("my.azureauth").WithDefaultSubscription();
var accounts = azure.StorageAccounts.List();
// can get values from my.azureauth
var tenantId = "";
var clientId = "";
var clientSecret = "";
var serviceCreds = await ApplicationTokenProvider.LoginSilentAsync(tenantId, clientId, clientSecret);
MonitorManagementClient readOnlyClient = new MonitorManagementClient(serviceCreds);
var oneHour = System.TimeSpan.FromHours(1);
var startDate = DateTime.Now.AddHours(-oneHour.Hours).ToUniversalTime().ToString("o");
string endDate = DateTime.Now.ToUniversalTime().ToString("o");
string timeSpan = startDate + "/" + endDate;
List<string> fileContents = new List<string>();
foreach (var storage in accounts)
{
var response = await readOnlyClient.Metrics.ListAsync(
resourceUri: storage.Id,
timespan: timeSpan,
interval: oneHour,
metricnames: "UsedCapacity",
aggregation: "Average",
resultType: ResultType.Data,
cancellationToken: CancellationToken.None);
foreach (var metric in response.Value)
{
foreach (var series in metric.Timeseries)
{
foreach (var point in series.Data)
{
if (point.Average.HasValue)
{
fileContents.Add($"{storage.Id}, {point.Average.Value}");
break;
}
}
break;
}
break;
}
}
await File.WriteAllLinesAsync("./storage.csv", fileContents);
}
}
}
$sub = Get-AzSubscription | select Name
$sub | foreach {
Set-AzContext -Subscription $_.Name
$currentSub = $_.Name
$RGs = Get-AzResourceGroup | select ResourceGroupName
$RGs | foreach {
$CurrentRG = $_.ResourceGroupName
$StorageAccounts = Get-AzStorageAccount -ResourceGroupName $CurrentRG | select StorageAccountName
$StorageAccounts | foreach {
$StorageAccount = $_.StorageAccountName
$CurrentSAID = (Get-AzStorageAccount -ResourceGroupName $CurrentRG -AccountName $StorageAccount).Id
$usedCapacity = (Get-AzMetric -ResourceId $CurrentSAID -MetricName "UsedCapacity").Data
$usedCapacityInMB = $usedCapacity.Average / 1024 / 1024
"$StorageAccount,$usedCapacityInMB,$CurrentRG,$currentSub" >> ".\storageAccountsUsedCapacity.csv"
}
}
}
使用Cloud Shell是目前为止最好的解决方案之一:
代码
param($resourceGroup, $storageAccountName)
# usage
# Get-StorageAccountSize -resourceGroup <resource-group> -storageAccountName <storage-account-name>
# Connect to Azure
Connect-AzureRmAccount
# Get a reference to the storage account and the context
$storageAccount = Get-AzureRmStorageAccount `
-ResourceGroupName $resourceGroup `
-Name $storageAccountName
$ctx = $storageAccount.Context
# Get All Blob Containers
$AllContainers = Get-AzureStorageContainer -Context $ctx
$AllContainersCount = $AllContainers.Count
Write-Host "We found '$($AllContainersCount)' containers. Processing size for each one"
# Zero counters
$TotalLength = 0
$TotalContainers = 0
# Loop to go over each container and calculate size
Foreach ($Container in $AllContainers){
$TotalContainers = $TotalContainers + 1
Write-Host "Processing Container '$($TotalContainers)'/'$($AllContainersCount)'"
$listOfBLobs = Get-AzureStorageBlob -Container $Container.Name -Context $ctx
# zero out our total
$length = 0
# this loops through the list of blobs and retrieves the length for each blob and adds it to the total
$listOfBlobs | ForEach-Object {$length = $length + $_.Length}
$TotalLength = $TotalLength + $length
}
# end container loop
#Convert length to GB
$TotalLengthGB = $TotalLength /1024 /1024 /1024
# Result output
Write-Host "Total Length = " $TotallengthGB "GB"
https://gist.github.com/iamsunny/8718fb29146363af11da95e5eb82f245
from azure.mgmt.monitor import MonitorManagementClient
from azure.mgmt.subscription import SubscriptionClient
from msrestazure.azure_active_directory import ServicePrincipalCredentials
from azure.mgmt.resourcegraph import ResourceGraphClient
from azure.mgmt.resourcegraph.models import QueryRequest
credentials = ServicePrincipalCredentials(client_id, secret, tenant=tenant_id)
sub_object = SubscriptionClient(credentials)
rgraph_object = ResourceGraphClient(credentials)
storageaccount_pattern = "resources | where type == 'microsoft.storage/storageaccounts' | project id"
subs = [sub.as_dict() for sub in sub_object.subscriptions.list()]
subs_list = []
for sub in subs:
subs_list.append(sub.get('subscription_id'))
request_storageaccount = QueryRequest(subscriptions=subs_list, query=storageaccount_pattern)
rgraph_storageaccount = rgraph_object.resources(request_storageaccount).as_dict()
resource_ids = []
for element in rgraph_storageaccount['data']:
resource_ids.append(element['id'])
count_used_storage = 0
for resource_id in resource_ids:
sub = (resource_id.split('/'))[2]
monitor_object = MonitorManagementClient(credentials, subscription_id=sub)
metrics_data = monitor_object.metrics.list(resource_id)
for item in metrics_data.value:
for timeserie in item.timeseries:
for data in timeserie.data:
try:
count_used_storage = count_used_storage + data.average
except:
pass
print(count_used_storage)
对于大约400个订阅和1100个存储账户的脚本,需要约600秒的时间。
但是对于一个订阅,速度要快得多 :)
$azstorcontext = New-AzureStorageContext -StorageAccountName storageaccounthere -StorageAccountKey storageaccountkeyhere
$sizesOverall = @()
$containers = Get-AzureStorageContainer -Context $azstorcontext
foreach ($container in $containers)
{
Write-Output $container.Name
$contblobs = get-azurestorageblob -container $container.name -Context $azstorcontext
Write-Output " Blobs: $($contblobs.count)"
$containersize = ($contblobs | Measure-Object -Sum Length).Sum
Write-Output " Container Size: $containersize) (bytes)"
$sizesOverall
}