如何使用Powershell提取Azure中资源的所有指标?

iyr7buue  于 2022-11-25  发布在  Shell
关注(0)|答案(1)|浏览(158)

我目前正在运行以下Powershell脚本,从Azure中提取指标
'#登录到azure并设置上下文

$Resource = Get-AzResource -ResourceName <appservicename> -ResourceGroupName <resourcegroup> -       ResourceType "Microsoft.Web/sites"
 $ResourceID = $Resource.ResourceId

 $MetricsDefinition = Get-AzMetricDefinition -ResourceId $ResourceID
 $MetricsDefinitionHash = @{}
 $MetricsDefinition | % {
 $Name = $_.Name
 $Unit = $_.Unit
 $MetricsDefinitionHash.Add($Name,$Unit) 
 }
 $TotalResult = @()
 
  $Metrics = Get-AzMetric -ResourceId $ResourceID -StartTime (Get-date).AddHours(-24) -TimeGrain       00:01:00

  $Metrics | % {
  if($_.Data -ne $null)
  { 
  $Calc, $metricName, $Result = $Null
  $i=0;
  $MetricName = $_.Name
  $_.Data | % {
  $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
 }
 }

`
我预期的结果为MetricName MetricValue MetricUnit平均响应时间0.7866443秒平均内存工作集80747385.625字节内存工作集
Http5xx Http4xx Http406 Http404 Http403 Http401 Http3xx 0 Http2xx 0字节已发送4699.123字节已接收7.8
请求数9.4444 CPU时间0.1111
我留了一些空值,但你明白我的意思了。问题是我只得到cputime。有什么想法为什么我只得到cputime?

kq4fsx7k

kq4fsx7k1#

我已经尝试在我的环境中获得这些指标

$Resource = Get-AzResource -ResourceName “metricdemoapp” -ResourceGroupName “xxx”       -ResourceType "Microsoft.Web/sites"
 $ResourceID = $Resource.ResourceId

$Metrics =获取AzMetric-资源ID $资源ID-开始时间(获取日期).添加小时数(-24)-时间粒度00:01:00

第一个月第一个月

具有ID:/subscriptions/xxxxxx23f/resourceGroups/xxxxx/providers/Microsoft.Web/sites/metricdemoapp/providers/Microsoft.Insights/metrics/CpuTime

在我的案例中,检查门户中支持的指标后,Web应用程序服务

当尝试使用其他度量名称时,获得了所需的单位。

  • 其中,如下定义命令给出了所有指标的详细信息以及逐个详细信息。

$度量标准定义=获取-AzMetricDefinition-资源ID $资源ID $度量标准定义

Powershell:

$Resource = Get-AzResource -ResourceName “metricdemoapp” -ResourceGroupName “xxx”       -ResourceType "Microsoft.Web/sites"
 $ResourceID = $Resource.ResourceId

$MetricsDefinition = Get-AzMetricDefinition  -ResourceId $ResourceID
$MetricsDefinition

$i=0
foreach($metric in $MetricsDefinition)
{
$MetricName=$MetricsDefinition.Name.Value[$i]
$MetricDetails= Get-AzMetric -ResourceId $ResourceID  -MetricName  $MetricName
$MetricDataValue=$MetricDetails.Data
$Timestamp=$MetricDetails.Data.TimeStamp
$Average=$MetricDetails.Data.Average



$mCount=$MetricName.Value.Count
$count=$mCount-1

$MetricUnit=$metric.Unit
$MetricUnit

$MetricValue=$MetricName.Value
$MetricValue

$MetricResult = New-Object PSObject
 $MetricResult | add-member Noteproperty MetricName $MetricName

$MetricResult | add-member Noteproperty MetricUnit $MetricUnit

$MetricResult | add-member Noteproperty MetricDetails $MetricDetails

$MetricResult | add-member Noteproperty MetricDataValue $MetricDataValue

$MetricResult | add-member Noteproperty TimeStamp  $Timestamp

$MetricResult | add-member Noteproperty  Average $Average

 $MetricResult


$i++

}
    • 输出:**
      将类似于:
MetricName      : CpuTime
MetricUnit      : Seconds
MetricDetails   : Microsoft.Azure.Commands.Insights.OutputClasses.PSMetricNoDetails
MetricDataValue : {Microsoft.Azure.Commands.Insights.OutputClasses.PSMetricValue, Microsoft.Azure.Commands.Insights.OutputClasses.PSMetricValue, Microsoft.Azure.Commands.Insights.OutputClasses.PSMetricValue, 
                  Microsoft.Azure.Commands.Insights.OutputClasses.PSMetricValue…}
TimeStamp       : {11/24/2022 6:23:00 AM, 11/24/2022 6:24:00 AM, 11/24/2022 6:25:00 AM, 11/24/2022 6:26:00 AM…}
Average         : {$null, $null, $null, $null…}

并针对以下各项指标继续


指令集
其中AverageResponseTime是您可以看到的指标之一

相关问题