Settings an alert if available sessions is low



I want to configure an alert when available sessions in a host pool is too low.
I'm looking at Log Analytics of course to do this, I do however have problems finding the right query.
I can query the current amount of sessions, no problem ,but I need that to substract from the max I think. Any idea how I can query the available sessions ? (either per host or per host pool)

11 Replies
I would give Sepago a try for AVD monitoring: - it has a bunch of Monitors and dashboards.
I know the sepago solution and I think it was nice before Azure monitor workbook was there, but I'm not talking about monitoring with all kinds of dashboards and graphs, that is all nice but I'm not going to watch a screen the entire day, I just want to receive alerts when available sessions is low.
It think it all has to do with the right LA query, if someone can provide that it would help a lot.
best response confirmed by kumarallamraju (Microsoft)

@viniszHey, Vinisz

I don't have the Kusto query for you, but this could be an alternative option. look at this (full Credit goes to Travis, I just pulled the relevant parts out - replace the hostpool variables with your own), you should be able to turn the below into an Azure Automation runbook to trigger an email based on count:


# Set default error action
$defaultErrorAction = $ErrorActionPreference

# Enable Verbose logging
$VerbosePreference = 'SilentlyContinue'

$hostPoolName = 'avd-pooled'
$hostPoolRg = 'avd_prod'

$hostPool = Get-AzWvdHostPool -ResourceGroupName $hostPoolRg -HostPoolName $hostPoolName
$sessionHosts = Get-AzWvdSessionHost -ResourceGroupName $hostPoolRg -HostPoolName $hostPoolName | Where-Object { $_.AllowNewSession -eq $true }
$runningSessionHosts = $sessionHosts | Where-Object { $_.Status -eq "Available" }
# Get the Max Session Limit on the host pool
# This is the total number of sessions per session host
$maxSession = $hostPool.MaxSessionLimit
# Get current active user sessions
$currentSessions = 0
foreach ($sessionHost in $sessionHosts) {
$count = $sessionHost.session
$currentSessions += $count
$runningSessionHostsCount = $runningSessionHosts.count
$sessionHostTarget = [math]::Ceiling((($currentSessions) / $maxSession))
if ($runningSessionHostsCount -lt $sessionHostTarget) {
Write-Verbose "Running session host count $runningSessionHostsCount is less than session host target count $sessionHostTarget"

elseif ($runningSessionHostsCount -gt $sessionHostTarget) {
Write-Verbose "Running session hosts count $runningSessionHostsCount is greater than session host target count $sessionHostTarget"

else {
Write-Verbose "Running session host count $runningSessionHostsCount matches session host target count $sessionHostTarget, doing nothing"

You could use a KQL query like below. Update the computer name to match your session host prefix and update the counter value to a number slightly below the "max session limit" value on your host pool.

| where ObjectName == "Terminal Services"
| where CounterName == "Active Sessions"
| where Computer contains "avd"
| where CounterValue > 0
| summarize arg_max(TimeGenerated, *) by Computer
| project Computer, CounterName, CounterValue

@Luke Murray 

I'm no powershell guru, but there is not output at all from this script.
Also, it gives an error like:

elseif : The term 'elseif' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
 the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ elseif ($runningSessionHostsCount -gt $sessionHostTarget) {
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (elseif:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

@JasonMasten , Pretty nice query, but it outputs the amount of sessions per-host.
What I'm looking for is per hostpool and then "max sessions per pool - current sessions per pool"

Al dynamic of course. (just like it is displayed in the Azure WVD workbook under "utilization"

Is there no way to get underlying query of that one ?

Well, I can see this query is in the workbook, now, how do I get this working for monitor ?

"let WVDHosts = dynamic([{WVDHosts}]);\r\nlet hostPool = \"{HostPool:label}\";\r\nPerf\r\n| extend Computername = strcat(hostPool, '/', Computer)| where Computername in (WVDHosts)\r\n| where TimeGenerated {TimeRange:query}\r\n| where ObjectName startswith \"Terminal Services\" and CounterName == \"Total Sessions\"\r\n| summarize Val= max(CounterValue-2) by bin(TimeGenerated, 1m), Computer\r\n| summarize Val= sum(Val) by TimeGenerated\r\n| project Val = ({VSessionLimit}*{SessionHostCount}) - Val, TimeGenerated\r\n| make-series [\"Available Sessions\"] = min(Val) on TimeGenerated step {TimeRange:grain}\r\n| where array_length(TimeGenerated) > 0\r\n"
I know how to set the alerts, I just need the right query, that is what this post is about..

@vinisz did you every find the solution to this? I am currently trawling the internet for the right Kusto query.


Kind regards,

Well, not in the way I wanted it to work in the first time, but we are now doing this via a logic-app, which asks the host pools via the API and then the session hosts of that pool.
After that, some calculations with retrieved data and then it can check the available sessions.
At the end, we send that data to log-analytics for trending (in powerBI) and to put an alert on it (via log analytics alert) , still working for several months like this without any problems