gpt4 book ai didi

powershell - 如何从 azure 获取性能计数器并写入 perfmon?

转载 作者:行者123 更新时间:2023-12-03 00:01:30 24 4
gpt4 key购买 nike

我在 Azure 门户中的网站监控中添加了一些新的自定义指标,现在我想使用 power shell 检索该性能数据并在本地计算机中创建一个新计数器,我该如何实现这一点

最佳答案

Azure 提供了服务管理 Rest API 来使用 GET、POST、PUT 等获取我们需要的指标 this提供 Power shell 来使用 x-ms 版本等收集所有指标和 this链接提供了创建性能计数器的方法

所以我们可以使用服务管理API来获取Azure应用程序对应的数据,我就是这样做的。

function activecons()
{
$WMIComputer = Get-WmiObject win32_operatingsystem -ComputerName mycomputer
$curdate=Convert-TimeToUTC -DateTime ($WMIComputer.ConvertToDateTime($WMIComputer.LocalDateTime))
#Write-Host $curdate
add-type -AssemblyName System.Net.Http
add-type -AssemblyName System.Net.Http.WebRequest
$sub = Get-AzureSubscription "subscription name"
$certificate = Get-Item Cert:\CurrentUser\My\urthumbprint
$subscriptionID = $sub.SubscriptionId
$handler = New-Object System.Net.Http.WebRequestHandler

# Add the management cert to the client certificates collection
$handler.ClientCertificates.Add($certificate)
$httpClient = New-Object System.Net.Http.HttpClient($handler)

# Set the service management API version
$httpClient.DefaultRequestHeaders.Add("x-ms-version", "2014-04-01")
#Write-Host "suresh"
$mediaType = New-Object System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/xml")
$httpClient.DefaultRequestHeaders.Accept.Add($mediaType)
$listServicesUri = "https://management.core.windows.net/$subscriptionID/services/WebSpaces/SoutheastAsiawebspace/sites/o penemrmysql/metrics?names=Http2xx,&StartTime=$curdate"
$listServicesTask = $httpClient.GetAsync($listServicesUri)
$listServicesTask.Wait()
if($listServicesTask.Result.IsSuccessStatusCode -eq "True")
{
# Get the results from the API as XML
[xml] $result = $listServicesTask.Result.Content.ReadAsStringAsync().Result
foreach($svc in $result.MetricResponses.MetricResponse)
{
#Write-Host $svc.Data.DisplayName "" $svc.Data.Unit
$global:active= $svc.Data.Values.MetricSample.Total -as [int]
# $activeconnections=$svc.Data.Values.MetricSample.Total -as [int]
# Write-Host $svc.Data.Values.MetricSample.Maximum "suresh"
# Write-Host $svc.Data.StartTime
#Write-Host $active
#return $active


}
}
}
#$curdate=activecons
#$global:active | Get-Member
#Write-Host $global:active

$categoryName = "Azure-HTTP2xx"
$categoryHelp = "A Performance object for HTTP Synthetic Testing"
$categoryType = [System.Diagnostics.PerformanceCounterCategoryType]::MultiInstance

$categoryExists = [System.Diagnostics.PerformanceCounterCategory]::Exists($categoryName)


If (-Not $categoryExists)
{
$objCCDC = New-Object System.Diagnostics.CounterCreationDataCollection

$objCCD1 = New-Object System.Diagnostics.CounterCreationData
$objCCD1.CounterName = "Total No. of Active connections"
$objCCD1.CounterType = "NumberOfItems32"
$objCCD1.CounterHelp = "Number of ms executing the HTTP Synthetic"
$objCCDC.Add($objCCD1) | Out-Null


[System.Diagnostics.PerformanceCounterCategory]::Create($categoryName, $categoryHelp, $categoryType, $objCCDC) | Out-Null
}

$perfInst1a = New-Object System.Diagnostics.PerformanceCounter($categoryName, "Total No. of Active connections", "openemrmysql", $false)
while(1)
{
$curdate=activecons
$rawvalue=$global:active
Write-host "suresh's "+$rawvalue
$perfInst1a.RawValue=$rawvalue
Start-Sleep -Seconds 30
}

关于powershell - 如何从 azure 获取性能计数器并写入 perfmon?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26842749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com