Azure Virtual Desktop active sessions metric

Luke Rosser 0 Reputation points
2024-07-05T14:36:57.8166667+00:00

I'm looking to be able to programatically determine the number of active sessions on VMs in an azure virtual desktop host pool, ideally with a metric or log analytics query so that I can configure alerting and actions.

The goal is to shutdown VMs that are not in use, by combining 0 active sessions and low CPU usage to trigger a shutdown through some automation.

If anyone knows of a way to do either of these that'd be great, as so far I've only been able to find the active sessions through the portal.

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
2,971 questions
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,480 questions
Azure Virtual Desktop
Azure Virtual Desktop
A Microsoft desktop and app virtualization service that runs on Azure. Previously known as Windows Virtual Desktop.
1,431 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 6,581 Reputation points
    2024-07-05T16:08:35.2833333+00:00

    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://video2.skills-academy.com/en-us/azure/azure-monitor/essentials/diagnostic-settings and https://video2.skills-academy.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:
    •   WVDConnections
        | 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
        
      
      In the above query you have options to modify, but above retrieves active session counts from the WVDConnections table, Then, joins with CPU usage data from the Perf table and projects the host pool name, session host name, active session count, and average CPU usage.

    Links: https://video2.skills-academy.com/en-us/azure/azure-monitor/logs/log-analytics-overview and https://video2.skills-academy.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 and AvgCpuUsage is below your threshold (e.g., 5%).
    • Configure the action group to trigger your automation.

    Link: https://video2.skills-academy.com/en-us/azure/azure-monitor/alerts/alerts-log and https://video2.skills-academy.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 and AvgCpuUsage 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 and AvgCpuUsage is below the threshold and If true, shut down the VM.

    Link: https://video2.skills-academy.com/en-us/azure/logic-apps/quickstart-create-first-logic-app-workflow and https://video2.skills-academy.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