Hello Luke Rosser,
Welcome to the Microsoft Q&A and thank you for posting your questions here.
Problem
I understand that you are in need of programmatical solution to monitor and manage VMs in an Azure Virtual Desktop host pool. Your goal is to optimize resource usage by shutting down VMs that have no active user sessions and are underutilized in terms of CPU usage.
Solution
Having this idea is great, you have many options like developing an app to do functions, using Azure API for specific needs, Using Microsoft SCCM and the solution I am providing right here. To achieving your goal of determining the number of active sessions on VMs in an Azure Virtual Desktop (AVD) host pool and configuring alerting and automation, you can leverage Azure Monitor metrics and Log Analytics queries. Please, read carefully the step-by-step and utilize the links provided for more detail references:
Enable Diagnostics and Metrics Collection
- Navigate to your AVD host pool in the Azure portal.
- Under the "Monitoring" section, select "Diagnostics settings."
- Add a diagnostic setting and ensure you send logs and metrics to a Log Analytics workspace.
- Ensure that the relevant metrics for your VMs are collected. You need the "Active Sessions" metric and CPU usage metrics.
Links: https://learn.microsoft.com/en-us/azure/azure-monitor/essentials/diagnostic-settings and https://learn.microsoft.com/en-us/azure/virtual-desktop/monitor-azure-monitor
Create a Log Analytics Query to Determine Active Sessions
- You can use Log Analytics to query the number of active sessions on each VM in your host pool and here is the sample snippet code:
-
In the above query you have options to modify, but above retrieves active session counts from theWVDConnections | where TimeGenerated > ago(5m) | summarize ActiveSessions = count() by HostPoolName, SessionHostName | join kind=inner ( Perf | where TimeGenerated > ago(5m) | where CounterName == "% Processor Time" | summarize avg(CounterValue) by Computer ) on $left.SessionHostName == $right.Computer | project HostPoolName, SessionHostName, ActiveSessions, AvgCpuUsage = avg_CounterValue
WVDConnections
table, Then, joins with CPU usage data from thePerf
table and projects the host pool name, session host name, active session count, and average CPU usage.
Links: https://learn.microsoft.com/en-us/azure/azure-monitor/logs/log-analytics-overview and https://learn.microsoft.com/en-us/azure/data-explorer/kql-quick-reference
Configure Alerts Based on Query Results
At this point, you will:
- Go to the Azure portal, navigate to "Monitor" > "Alerts" > "New alert rule."
- Set the scope to the Log Analytics workspace containing your data.
- Define the condition using the Log Analytics query you created.
- Set up an alert logic that triggers when
ActiveSessions
is 0 andAvgCpuUsage
is below your threshold (e.g., 5%). - Configure the action group to trigger your automation.
Link: https://learn.microsoft.com/en-us/azure/azure-monitor/alerts/alerts-log and https://learn.microsoft.com/en-us/azure/azure-monitor/alerts/alerts-metric
Automate VM Shutdown
You will be using an Azure Logic App or Azure Automation runbook anyone that's easier for you to shut down the VMs. The outline using Azure Logic Apps and workflow will be similar to:
- Create a new Logic App in the Azure portal.
- Add a trigger based on the alert created in the previous step.
- Use the "Azure Resource Manager" connector to list VMs in the host pool.
- Add a condition to check if
ActiveSessions
is 0 andAvgCpuUsage
is below the threshold. - Add an action to shut down the VMs using the "Azure Virtual Machines" connector.
- The workflow:
- Create a Trigger when an alert is fired.
- Get the list of VMs from the host pool.
- Check if
ActiveSessions
is 0 andAvgCpuUsage
is below the threshold and If true, shut down the VM.
Link: https://learn.microsoft.com/en-us/azure/logic-apps/quickstart-create-first-logic-app-workflow and https://learn.microsoft.com/en-us/azure/logic-apps
Accept Answer
I hope this is helpful! Do not hesitate to let me know if you have any other questions.
** Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful ** so that others in the community facing similar issues can easily find the solution.
Best Regards,
Sina Salam