0

I need to get some metrics for Azure an app service . Values are the CPU (in %) and Memory consumed ( in %). I want to report if for example the CPU is high for a certain period of time , in order to give an indication that the app service is consuming high memory or resources and then send an email .

I am currently using

Get-AzMetricDefinition -ResourceId 
"/subscriptions/<subscriptionid>/resourceGroups/Default-Web- 
 EastUS/providers/microsoft.web/sites/website2" -DetailedOutput -MetricName 
"BytesSent,CpuTime"

But from this how can i get the % CPU and % Memory. Or is there a different Powershell command ?

Tracey
  • 81
  • 6

1 Answers1

1

To get the CPU usage for an Azure app service using PowerShell:

There is an Azure PowerShell command called Get-AZMetric to get all the metrics data from a particular resource (Eg: App Service). However, CPU % is not a supported argument for Get-AZMetric, if you copy the resourceID of the Web App properities as it is a cloud service ID.

We can achieve it for overall AppServicePlan as CPU % supports here.

If the app services hosted in premium, standard and basic plans, CPU% will comes because of scaling out feature whereas CPU_Time is helpful when hosted in free or the shared app service plans.

get-azmetric -resourceID "/subscriptions/<subscriptionID>/resourceGroups/<resourceGroupName>/providers/Microsoft.Web/sites/<AppService>" -MetricName "CpuTime"  -DetailedOutput

enter image description here

After investigating, I've found a way to achieve it with the help of AppServicePlan by taking resourceID as shown:

enter image description here

 az monitor metrics list --resource "/subscriptions/<subscriptionID>/resourceGroups/<resourceGroupName>/providers/Microsoft.Web/serverfarms/<AppservicePlan>" --metrics "CpuPercentage","MemoryPercentage"

Metrics for CPU Percentage:

enter image description here

Metrics for Memory Percentage:

enter image description here

Note:

  1. Check the supported metrics through portal by clicking on "Diagnose and solve problems" if needed.

enter image description here

  1. Issues with AppService environments
Jahnavi
  • 3,076
  • 1
  • 3
  • 10
  • Jus a few question in regards to this . It retrieves the CPU and Memory Percentage on the App Service Plan. The json data that comes back has about 15-20 rows , so i assume it must extract a number of averages ? Also where can i view these values in the Portal ? Is it under the App Service Plan ? I need to identify the app that is consuming cpu and memory and eventually is auto restarted. I want to capture the app that is consuming high CPU and Memory for lets say 15-20 mins and send an email before it restarts or stops . Is that possible with the commands you gave above in Powershell ? – Tracey Nov 15 '22 at 19:59
  • 1. It is extracting the percentage averages of CPU & Memory. 2. Yes, you can check all these metrics under serviceplan. `AppServicePlan -> Metrics`. 3. To identify the app that is consuming CPU and memory: I suggest you to associate one webapp with one AppServicePlan. Then if we retrieve the metrics of serviceplan, it is nothing but we are extracting the webapp metrics. @Tracey – Jahnavi Nov 16 '22 at 03:55
  • Yes I agree with that , but I have at most 3 app services in each app service plan. At the moment al i need to know is if the cpu and memory are high for the app service plan for a period of time. For example the cpu is at 95% for the last 30min. That means there is a bottleneck and if i can read that in powershell , then i can send an email for admin to look at in the portal. – Tracey Nov 16 '22 at 03:58
  • Also in the Json response to the command above , under timeseries -> data , there are a number of average fields ? Timestamp from 02:54-03:53 , so i assume it has taken the last hour worth of results . So if the average field is null then ignore , else I can take the value , the values range from 0.08 - 17.5 . So i assume that is min CPU % of 0.08 to max 17.5 ? – Tracey Nov 16 '22 at 04:02
  • Yes we can list out the data by using filter parameter for a particular time. [Refer here](https://learn.microsoft.com/en-us/cli/azure/monitor/metrics?view=azure-cli-latest) – Jahnavi Nov 16 '22 at 04:02
  • And if within that hour of data if it is consistently at lets say 80% , then I can assume that there is an app in the App Service Plan causing an issue , so i need to grab all the json data from the command returned ? – Tracey Nov 16 '22 at 04:03
  • Yes exactly. That is correct. – Jahnavi Nov 16 '22 at 04:04
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/249651/discussion-between-tracey-and-jahnavi). – Tracey Nov 16 '22 at 04:05