Pull out Azure metrics from each resource

I want to tell you a little bit about Azure metrics and how they can be usefull for you.
For instance, if organization already has some monitoring system and don’t want to use Azure alerting for monitoring purpose it can use data from Powershell.
Information taken from Powershell can be send via some API to monitoring system and that’s all. You’ve got custom solution without using alerting directly from Azure Portal.
Let’s dive into script and definition of each part.
As usual login to Azure subscription on which you want to opearte.

Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionName 'SubscriptionName'

In section below you must define for which resource you want to gather metrics. If more than one resource should be gathered it’s also not a problem, however script must be adjusted.
Provide name, resource group and resurce type (Check http://mitchdenny.com/known-azure-resource-types/ blog  to see most used resource types.). Later on ResourceId property should be selected.

$Resource = Get-AzureRmResource -ResourceName "WebAppName" -ResourceGroupName "WebAppResourceGroup" -ResourceType "Microsoft.Web/sites"
$ResourceID = $Resource.ResourceId

What is interesting for us are metrics which are associated with resource. We are taking all metrics definition and saving them in variable.
To hashtable only name(as a key) and unit(as a value) are added. It will be used in next part of code.

$MetricsDefinition = Get-AzureRmMetricDefinition -ResourceId $ResourceID
$MetricsDefinitionHash = @{}
$MetricsDefinition | % {
$Name = $_.Name
$Unit = $_.Unit
$MetricsDefinitionHash.Add($Name,$Unit)
}

In TotalResult array we will keep final information about metrics.

$TotalResult = @()

To $Metrics we save all information about metrics. Used command will pull out only metric values with granulation of one minute. Sometimes it can happen that there will be no value for specific metric, for instance HTTP 404 errors.

$Metrics = Get-AzureRmMetric -ResourceId $ResourceID -StartTime (Get-date).AddHours(-1) -TimeGrain 00:01:00

Once we have metrics we can procedd of calculation of each and the result is added to array. As you can see here we used hash table defined in previous section to check unit for specific metric.

$Metrics | % {
if($_.MetricValues -ne $null)
{

$Calc, $metricName, $Result = $Null
$i=0;
$MetricName = $_.Name
$_.MetricValues | % {
$calc += $_.Average
$i++
}

$Result = $Calc/$i
$MetricUnit = $MetricsDefinitionHash["$MetricName"]

$MetricResult = New-Object PSObject
$MetricResult | add-member Noteproperty MetricName $metricName
$MetricResult | add-member Noteproperty MetricValue $Result
$MetricResult | add-member Noteproperty MetricUnit $MetricUnit
$TotalResult += $MetricResult
}
}

In TotalResult array you should see the result of script. It should looks similar as below.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.