I’ve been looking for at way to evenly distribute agents between Gateway Servers (or Management Servers for that matter, but I’ll stick to GWs this time) for some time but haven’t really got to fixing it myself until now.
The situation is basically that we’re monitoring customers through gateway servers connected to our central Operations Manager environment. To have a bit of redundancy we always put two (or more) gateway servers per site (or customer, really) and they, in turn, talks to a couple of central management servers. I guess a drawing would be nice, but I have no Visio on this computer. The gateways are manually configured to talk to different management servers and have the others configured for fail-over (through powershell) and since we’re talking about no more than a few handfuls (say 20-ish) it’s not a problem handling it that way.
Agents, on the other hand, are a different matter. Even though we try to spread them out somewhat evenly at deployment between the gateway servers at each site we still end up looking at a 3:2 ratio after a while and since agents do not automatically fail-over between gateway servers we need a way to fix that too.
So I wrote a little powershell script that takes a bunch of gateway servers (or management servers) as parameters, gathers all connected agents, spreads the agents evenly between the servers and configures the others as fail-over servers while at it.
It’s all pretty crude, but it works and you can download it from here: DistributeAgents.ps1
Save it somewhere on disk and call it from the Operations Manager Shell like this:
C:DistributeAgents.ps1 gateway01.customer.local,gateway02.customer.local,gateway03.customer.local
Yes, you should replace “C:” with whatever path you decided to save the script to and “gatewayXX.customer.local” with a real servername.
Ok, I’m a powershell freshman and I’m pretty sure you could do this a prettier way, but here’s the script:
Param([array]$CSVServerList)
$arrServerObject = @()
$arrAgentObject = @()
foreach($Server in $CSVServerList)
{
$arrServerObject += Get-ManagementServer | where {$_.Name -eq $Server}
echo "Looking for $Server"
}
$ServerCount = $arrServerObject.Count
if ($ServerCount -gt 1)
{
echo "Found $ServerCount management servers"
} else {
echo "Found only 1 (or less) management servers. Aborting..."
Exit
}
echo "Getting agents..."
foreach ($Server in $arrServerObject)
{
$arrAgentObject += Get-Agent | where {$_.PrimaryManagementServerName -eq $Server.Name}
}
$AgentCount = $arrAgentObject.Count
if ($AgentCount -gt 1)
{
echo "Found $AgentCount agents"
Start-Sleep -m 200
} else {
echo "Found only 1 (or less) agents. Aborting..."
Exit
}
$i = 0
foreach ($Agent in $arrAgentObject)
{
if ($i -ge $ServerCount)
{
$i = 0
}
$arrTemp = @($arrServerObject | Where-Object {$_ -ne $arrServerObject[$i]})
# $FailoverServers = $arrTemp -join ","
Set-ManagementServer -AgentManagedComputer: $Agent -PrimaryManagementServer: $arrServerObject[$i] -FailoverServer: $arrTemp
$arrTemp = $null
$i++
}
I have used it on a couple of occasions now and have only discovered a problem with an error when one of the servers don’t have any agents at all (probably a new one), but the script still works so I haven’t really dived into it.
Now, as with all scripts you download on the ‘net it’s up to you to test it in a lab before shooting wildly among your in-production systems. I really can’t give any warranties that it won’t FSU royally at your place.
