question

MCCZ-3624 avatar image
0 Votes"
MCCZ-3624 asked ·

How to group metrics by domain

For a web app serving multiple customer domains, I would like to gather metrics like Data Out and Requests per domain (abc.contoso.com, def.example.com).

For example, I would like to know that abc.contoso.com generated 25 GB Data Out and served 5 000 requests in March while def.example.com generated 5 GB Data Out and served 4 000 requests.

Is there a way to get these info? How?

azure-webappsazure-monitor
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ManuPhilip avatar image
1 Vote"
ManuPhilip answered ·

Hi,
Here is a simple PS script, which shows the graphical view of the stats you are looking for. Note that, this is a very basic script works for a single website and you need to enhance it further to work on multiple sites

Note: Set the appropriate values for the parameters as per your setup. ResourceName "manutest" ResourceGroupName "manurg1"

Give Start and End Time accordingly


 # inputs
 $WarningPreference = 'SilentlyContinue'
 $TimeGrain = [timespan]::FromMinutes(1)
 $Start = [datetime]::Now.AddMinutes(-40)
 $End = [datetime]::Now.AddMinutes(-1)
    
 # capture resource metrics
 $Resource = Get-AzResource -ResourceName "manutest" -ResourceGroupName "manurg1" -ResourceType "Microsoft.Web/sites"
 $ResourceID = $Resource.ResourceId
    
 $MetricName = 'BytesSent'
 $Splat = @{
     ResourceId = $ResourceID 
     MetricName = $MetricName
     TimeGrain = $TimeGrain 
     StartTime = $Start
     EndTime = $End
 }
 $Data = Get-AzMetric @Splat
 $Datpoints = $data.data.average.foreach({[int]$_})
    
 # plot the graph
 Show-Graph -Datapoints $Datpoints -GraphTitle $MetricName -YAxisStep 5 
 Show-Graph -Datapoints $Datpoints -GraphTitle $MetricName -YAxisStep 5 -Type Line
 Show-Graph -Datapoints $Datpoints -GraphTitle $MetricName -YAxisStep 5 -Type Scatter
    
 $MetricName = 'BytesReceived'
 $Splat = @{
     ResourceId = $ResourceID 
     MetricName = $MetricName
     TimeGrain = $TimeGrain 
     StartTime = $Start
     EndTime = $End
 }
 $Data = Get-AzMetric @Splat
 $Datpoints = $data.data.average.foreach({[int]$_})
    
 # plot the graph
 Show-Graph -Datapoints $Datpoints -GraphTitle $MetricName -YAxisStep 5 
 Show-Graph -Datapoints $Datpoints -GraphTitle $MetricName -YAxisStep 5 -Type Line
 Show-Graph -Datapoints $Datpoints -GraphTitle $MetricName -YAxisStep 5 -Type Scatter
    
 $MetricName = 'Requests'
 $Splat = @{
     ResourceId = $ResourceID 
     MetricName = $MetricName
     TimeGrain = $TimeGrain 
     StartTime = $Start
     EndTime = $End
 }
 $Data = Get-AzMetric @Splat
 $Datpoints = $data.data.average.foreach({[int]$_})
    
 # plot the graph
 Show-Graph -Datapoints $Datpoints -GraphTitle $MetricName -YAxisStep 5 
 Show-Graph -Datapoints $Datpoints -GraphTitle $MetricName -YAxisStep 5 -Type Line
 Show-Graph -Datapoints $Datpoints -GraphTitle $MetricName -YAxisStep 5 -Type Scatter


Regards,
Manu

· 1 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Manu, thanks for your script. Unfortunately, I do not see any groupings per domain served by the app.


0 Votes 0 · ·
ManuPhilip avatar image
0 Votes"
ManuPhilip answered ·

Hello,

I have modified the script to loop through all the domains in a selected Resource Group as per your request and providing here

Note that the following variables to be given when asked

  1. Input the Resource Group Name

  2. Input the number of day's report you are looking for (View looks better when you select minimum days)

Code is prepared as below: Save it as a PS1 file and you are ready to go :)

 $RgName = Read-Host "Please enter the Resource Group Name that the Websites belongs to"
 $Days= Read-Host "Please enter Last #Days needed to be checked in the report"
 $ResourceNames = Get-AzResource -ResourceGroupName $RgName -ResourceType "Microsoft.Web/sites"
    
 foreach ($ResourceName in $ResourceNames)
 {
 $WebappName = $ResourceName.Name 
 $URLName=(Get-AzWebApp -Name $WebappName).DefaultHostName
    
 # inputs
  $WarningPreference = 'SilentlyContinue'
  $TimeGrain = [timespan]::FromHours(1)
    
  $Start = [datetime]::Now.AddDays(-$Days)
  $End = [datetime]::Now
     
  # capture resource metrics
  $Resource = Get-AzResource -ResourceName $WebappName -ResourceGroupName $RgName -ResourceType "Microsoft.Web/sites"
  $ResourceID = $Resource.ResourceId
     
  $MetricName = 'BytesSent'
  $Splat = @{
      ResourceId = $ResourceID 
      MetricName = $MetricName
      TimeGrain = $TimeGrain 
      StartTime = $Start
      EndTime = $End
  }
  $Data = Get-AzMetric @Splat
  $Datpoints = $data.data.average.foreach({[int]$_})
     
  # plot the graph
  Show-Graph -Datapoints $Datpoints -GraphTitle "$MetricName : $URLName" -YAxisStep 5 
  Show-Graph -Datapoints $Datpoints -GraphTitle "$MetricName : $URLName" -YAxisStep 5 -Type Line
  Show-Graph -Datapoints $Datpoints -GraphTitle "$MetricName : $URLName" -YAxisStep 5 -Type Scatter
     
  $MetricName = 'BytesReceived'
  $Splat = @{
      ResourceId = $ResourceID 
      MetricName = $MetricName
      TimeGrain = $TimeGrain 
      StartTime = $Start
      EndTime = $End
  }
  $Data = Get-AzMetric @Splat
  $Datpoints = $data.data.average.foreach({[int]$_})
     
  # plot the graph
  Show-Graph -Datapoints $Datpoints -GraphTitle "$MetricName : $URLName" -YAxisStep 5 
  Show-Graph -Datapoints $Datpoints -GraphTitle "$MetricName : $URLName" -YAxisStep 5 -Type Line
  Show-Graph -Datapoints $Datpoints -GraphTitle "$MetricName : $URLName" -YAxisStep 5 -Type Scatter
     
  $MetricName = 'Requests'
  $Splat = @{
      ResourceId = $ResourceID 
      MetricName = $MetricName
      TimeGrain = $TimeGrain 
      StartTime = $Start
      EndTime = $End
  }
  $Data = Get-AzMetric @Splat
  $Datpoints = $data.data.average.foreach({[int]$_})
     
  # plot the graph
  Show-Graph -Datapoints $Datpoints -GraphTitle "$MetricName : $URLName" -YAxisStep 5 
  Show-Graph -Datapoints $Datpoints -GraphTitle "$MetricName : $URLName" -YAxisStep 5 -Type Bar
  Show-Graph -Datapoints $Datpoints -GraphTitle "$MetricName : $URLName" -YAxisStep 5 -Type Scatter
  }



· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MCCZ-3624 avatar image
0 Votes"
MCCZ-3624 answered ·

Hi Manu,

thanks for your updated answer. I understand it iterates through all the web apps in a given resource group nor through all the domains served by a single web app. As there still seems to be a misunderstanding, I'll try to reiterate the question using an example.

I have a single web app, let's say ContosoCms whose purpose is to serve webs on three domains - www.contoso.com, www.example.com and support.example.com. We will scale up the ContosoCms based on the total traffic not based on the number of domains it serves. As of now there is just a single instance of the web app serving all three domains.

For this single web app, I need to group Data Out and Requests metrics based on the domain it was served on.

 Served domain by the single ContosoCms web app | Data Out | Requests
 www.contoso.com                                |   300 MB |  2 532 requests
 www.example.com                                |   234 MB |  2 544 requests
 support.example.com                            | 4 500 MB | 53 423 requests
· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ManuPhilip avatar image
0 Votes"
ManuPhilip answered ·

Hello,

I have further modified the script to print the required statistics from different websites within the shell itself. Please check the enhanced code.
When asked, give the name of the WebAppName where the websites belongs to

 $WebAppName = Read-Host "Please enter the WebApp Name that the Website(s) belongs to"
 $Days= Read-Host "Please enter Last #Days needed to be checked in the report"
 $WebApp= Get-AzWebApp -Name $WebAppName
 $RgName=$WebApp.ResourceGroup
    
 $ResourceNames = Get-AzResource -ResourceGroupName $RgName -ResourceType "Microsoft.Web/sites"
    
 foreach ($ResourceName in $ResourceNames)
 {
 $WebappName = $ResourceName.Name 
 $URLName=(Get-AzWebApp -Name $WebappName).DefaultHostName
    
 # inputs
  $WarningPreference = 'SilentlyContinue'
  $TimeGrain = [timespan]::FromDays(1)
    
  $Start = [datetime]::Now.AddDays(-$Days)
  $End = [datetime]::Now
     
  # capture resource metrics
  $Resource = Get-AzResource -ResourceName $WebappName -ResourceGroupName $RgName -ResourceType "Microsoft.Web/sites"
  $ResourceID = $Resource.ResourceId
     
  $MetricName = 'Requests'
  $Splat = @{
      ResourceId = $ResourceID 
      MetricName = $MetricName
      TimeGrain = $TimeGrain 
      StartTime = $Start
      EndTime = $End
  }
  $Data = Get-AzMetric @Splat
  $Datpoints = $data.data.average.foreach({[int]$_})
  Write-Output $URLName
  Write-Output $MetricName
  Write-Output $data.data | format-table TimeStamp, Total -AutoSize
    
  $MetricName = 'BytesReceived'
  $Splat = @{
      ResourceId = $ResourceID 
      MetricName = $MetricName
      TimeGrain = $TimeGrain 
      StartTime = $Start
      EndTime = $End
  }
  $Data = Get-AzMetric @Splat
  $Datpoints = $data.data.average.foreach({[int]$_})
  Write-Output $URLName
  Write-Output $MetricName
  Write-Output $data.data | format-table TimeStamp, Total -AutoSize
    
  $MetricName = 'BytesSent'
  $Splat = @{
      ResourceId = $ResourceID 
      MetricName = $MetricName
      TimeGrain = $TimeGrain 
      StartTime = $Start
      EndTime = $End
  }
  $Data = Get-AzMetric @Splat
  $Datpoints = $data.data.average.foreach({[int]$_})
  Write-Output $URLName
  Write-Output $MetricName
  Write-Output $data.data | format-table TimeStamp, Total -AutoSize
 }

Regards,
Manu

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.