Jump to content
DarrenWhite99

How Wake On LAN selects the agent to send WOL packets

Recommended Posts

I was researching this and thought it was worth sharing.

When you use the Script "Net Wake On LAN" function, how is the agent chosen? This is important when planning or troubleshooting agent wakeup, because WOL will fail if the agent chosen cannot deliver the packets to the target because they are in a different subnet/vlan/etc.

When you use the "Net Wake On LAN" script function, it calls an internal function called GetMasterComputer() to determine which agent should send the Wake packets. In that function it chooses the Master by running this query: 
Select Computers.ComputerID From Computers Join AgentComputerData Using (ComputerID)
Where AgentComputerData.Master <> 2 and Computers.Uptime > 0 and TIMESTAMPDIFF(Second,Computers.LastContact,NOW())<300
AND Computers.ComputerID<>"CURRENT COMPUTER ID" AND Computers.LocationID="COMPUTER LOCATION ID"
ORDER BY (Computers.Flags & 128) DESC, agentcomputerdata.Master desc,IF((Flags & 16)>0 OR (Flags & 128)>0,30,300)-TIMESTAMPDIFF(SECOND,Computers.LastContact,NOW()) 
LIMIT 1;


(If Router Based FasTalk is enabled, it will use "AND Computers.RouterAddress=(Select RouterAddress from Computers where ComputerID="CURRENT COMPUTER ID"` to select candidates instead of checking the Location.)
So Based on the Location or the Agent Public IP it selects a computer from all computers that have checked in within the past 5 minutes and are not blocked from being Masters. It prioritizes them and returns the top result, in this order:
1. Probe Computers, 2. Computers Assigned as Masters, 3. How many seconds are left until the agent is due to check in next

So if the Probe for a location is online it will always be chosen. Even if the probe is on a different subnet than the agent. (If multiple probes were matched, the top choice will be the probe expected to check in the soonest.)
Agents that are assigned as a master (normally only servers) will be selected next. Even if the agent is not in the subnet/vlan where servers reside. (Among the masters, the agent expected to check in the soonest will be chosen.)
Finally any remaining candidate agents are ranked by how soon they are expected to check in, and the one with the shortest period remaining is selected.

At no point does the function compare the LAN IP Address or Netmask.  If you have agents from multiple subnets/vlans (or even multiple physical locations) combined into one Location in Automate, you may have problems using WOL.  (Or also, getting FasTalk to successfully prompt other agents to check in sooner if they need to) Make sure that your design for agent assignment to locations considers the communications between all of the agents. If there are agents that cannot speak to each other (Or at least, if the Probe and Master Agents cannot reach them) then they should be moved into a separate location.

It is possible to override the internal logic and select your own agent to use. The same function accepts a parameter, the MAC address to send a Wake Packet to. If you specify a MAC Address then instead of picking an agent to send the command, the current agent will be asked to send a packet to that MAC address. By using your own query to select the best agent to use for Wake Up, you can set @computerid@ to the other agent ID and provide your MAC address as the parameter. Now the computer you specifically selected will issue the WOL command. (Don't forget to change @computerid@ back before continuing)

Here is an example query to select a candidate agent, but the agent IP/subnet is given priority when choosing the agent. The actual interface netmask is considered in this query, so if the network is larger or smaller than /24, the "neighbors" will still be correctly calculated. 
SELECT c.computerid FROM computers AS c JOIN AgentComputerData AS acd USING (ComputerID)
JOIN computers AS c2 ON c2.computerid='%computerid%'
JOIN inv_NetworkCard AS nc2 ON nc2.computerid=c2.computerid AND nc2.Status = 'Up'
JOIN inv_NetworkCard AS nc ON nc.computerid=c.computerid AND nc.Status = 'Up' 
WHERE c.computerid<>c2.computerid AND c.clientid=c2.clientid AND acd.Master <> 2 AND c.Uptime > 0 AND TIMESTAMPDIFF(SECOND,c.LastContact,NOW())<300
AND INET_ATON(nc2.Netmask)>=(((1<<32)-1)^((1<<24)-1)) AND NOT INET_NTOA(INET_ATON(nc2.IPAddress) & INET_ATON(nc2.Netmask)) IN ('0.0.0.0','127.0.0.0','169.254.0.0','224.0.0.0')
AND (INET_ATON(nc2.IPAddress) & INET_ATON(nc2.Netmask))=(INET_ATON(c2.localaddress) & INET_ATON(nc2.Netmask)) 
AND INET_ATON(nc.Netmask)>=(((1<<32)-1)^((1<<24)-1)) AND NOT INET_NTOA(INET_ATON(nc.IPAddress) & INET_ATON(nc.Netmask)) IN ('0.0.0.0','127.0.0.0','169.254.0.0','224.0.0.0')
AND (INET_ATON(nc.IPAddress) & INET_ATON(nc.Netmask))=(INET_ATON(c.localAddress) & INET_ATON(nc.Netmask)) 
ORDER BY nc.netmask=nc2.netmask DESC,
(INET_ATON(nc.IPAddress) & INET_ATON(nc.Netmask))=(INET_ATON(nc2.IPAddress) & INET_ATON(nc2.Netmask)) DESC,
(C.Flags & 128) DESC, acd.Master DESC, (IF((c.Flags & 16)>0 OR (c.Flags & 128)>0,30,300)-TIMESTAMPDIFF(SECOND,c.LastContact,NOW())) DIV 10, 
c.routeraddress=c2.routeraddress DESC, c.locationid=c2.locationid DESC, c.clientid=c2.clientid DESC LIMIT 1;

NOTE: It is possible that there is no available online agent to try using for WOL. You need to get the value, and confirm it is >0.

Here is a simpler version that just assumes that if the IP is in the same /24 range, it is a good fit: 

SELECT c.computerid FROM computers AS c JOIN AgentComputerData AS acd USING (ComputerID)
JOIN computers AS c2 ON c2.computerid='%computerid%'
WHERE c.computerid<>c2.computerid AND c.clientid=c2.clientid AND acd.Master <> 2 AND c.Uptime > 0 AND TIMESTAMPDIFF(SECOND,c.LastContact,NOW())<300
ORDER BY SUBSTRING_INDEX(c.localaddress,'.',3)=SUBSTRING_INDEX(c2.localaddress,'.',3) DESC, 
(C.Flags & 128) DESC, acd.Master DESC, 
(IF((c.Flags & 16)>0 OR (c.Flags & 128)>0,30,300)-TIMESTAMPDIFF(SECOND,c.LastContact,NOW())) DIV 10, 
c.routeraddress=c2.routeraddress DESC, c.locationid=c2.locationid DESC, c.clientid=c2.clientid DESC LIMIT 1;

In both queries I limited the results to the same client. Both queries prioritize matching router addresses, and finally matching locations. If you have agents from multiple clients that exist in the same network, and all so agents from other clients should be considered, just remove "AND c.clientid=c2.clientid" from the WHERE condition. If you want to restrict matches only to the same location. add "AND c.locationid=c2.locationid" into the WHERE condition.

  • Thanks 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×