chinna2580
chinna2580

Reputation: 2125

how do i get performance counters from azure and write into perfmon?

i have added some of the new custom metrics in the monitoring of a website in the azure portal, Now i want to retrieve that performance data using power shell and create a new counter in my local machine,how can i achieve this

Upvotes: 2

Views: 352

Answers (1)

chinna2580
chinna2580

Reputation: 2125

Azure provided its Service management Rest API to get the metrics which ever we need using GET,POST,PUT..etc this provides the power shell to collect all the metrics using the x-ms version etc and this link provides the way to create the performance counters

So we can use Service management API to get the data corresponding to azure applications,this is how i does it.

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
 }

Upvotes: 1

Related Questions