Skip to content

Instantly share code, notes, and snippets.

@RichardSlater
Created February 11, 2013 11:03
Show Gist options
  • Save RichardSlater/4753866 to your computer and use it in GitHub Desktop.
Save RichardSlater/4753866 to your computer and use it in GitHub Desktop.
Retrieve the latest storage capacity metrics for multiple storage accounts and display in a range of units.
# *************************************************************************************************
# * Configure-CapacityMetrics *
# *************************************************************************************************
# * Description: Configures Capacity metrics *
# * Author: Richard Slater <[email protected]> *
# * Date: 2013.02.02 *
# * Prerequisites: WAPPSCmdlets (https://www.windowsazure.com/en-us/manage/downloads/) *
# *************************************************************************************************
Set-StorageServicePropertiesForAnalytics -ServiceName "Blob" -StorageAccountName "<Storage Account Name>" -StorageAccountKey "<Storage Account Key>" -MetricsEnabled -MetricsRetentionPolicyDays 7 -MetricsRetentionPolicyEnabled
# *************************************************************************************************
# * Get-CapacityMetrics (With TeamCity Support) *
# *************************************************************************************************
# * Description: Downloads and displays capacity metrics from Windows Azure Blob Storage *
# * Author: Richard Slater <[email protected]> *
# * Date: 2013.02.02 *
# * Prerequisites: WAPPSCmdlets (https://www.windowsazure.com/en-us/manage/downloads/) *
# *************************************************************************************************
Param(
[Parameter(mandatory=$false)]
[switch]$TeamCity
)
if ((Get-PSSnapin | ?{$_.Name -eq "WAPPSCmdlets"}) -eq $null)
{
Add-PsSnapin WAPPSCmdlets
}
$logPath = Split-Path $script:MyInvocation.MyCommand.Path
if (-not $TeamCity) { Write-Host "Will download storage capacity metrics for the following storage accounts with associated storage account keys:" }
$buckets = @{
"<Storage Account Name>" = "<Storage Key>";
}
if (-not $TeamCity) {
$buckets
Write-Host "Starting download of metrics..."
}
$results = @()
foreach ($bucket in @($buckets.Keys))
{
if (-not $TeamCity) { Write-Host "Collecting Capacity Metrics from $bucket" -ForegroundColor Green }
$logName = Join-Path $logPath ($bucket + ".log")
Get-StorageAnalyticsMetrics -DataType "Capacity" -ServiceName "Blob" -LocalPath $logName -StorageAccountName $bucket -StorageAccountKey $buckets[$bucket] | Out-Null
$results += (Import-Csv $logName | Where-Object { $_.Category -eq "data" } | Select-Object -Last 1 @{Name="Bucket";Expression={$bucket}}, Time, "Capacity (bytes)", "Container count", "Object count")
}
if (-not $TeamCity) {
Write-Host "Download complete."
Write-Host
Write-Host "Capacity in blob storage is as follows:"
$results | Select-Object Bucket, "Capacity (bytes)", `
@{Name="Capacity (KB)";Expression={[Math]::Round($_."Capacity (bytes)" / 1KB, 2)}}, `
@{Name="Capacity (MB)";Expression={[Math]::Round($_."Capacity (bytes)" / 1MB, 2)}}, `
@{Name="Capacity (GB)";Expression={[Math]::Round($_."Capacity (bytes)" / 1GB, 2)}}, `
@{Name="Capacity (TB)";Expression={[Math]::Round($_."Capacity (bytes)" / 1TB, 2)}}
}
else
{
$totalCapacity = 0;
foreach ($bucket in $results)
{
Write-Host ("##teamcity[buildStatisticValue key='{0}_storage_bytes' value='{1}']" -f $bucket.Bucket, $bucket."Capacity (bytes)")
$totalCapacity = $totalCapacity + $bucket."Capacity (bytes)"
}
Write-Host ("##teamcity[buildStatus status='SUCCESS' text='Total across all storage accounts {0}TB']" -f [Math]::Round($totalCapacity / 1TB, 2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment