Custom task to query computer groups with least number of machines in them

Hey all! I’m pretty new to the whole relevance language/action scripts, custom fixlets, etc. etc., so I would love any help I can get!

I am trying to create a custom fixlet/task action script that will query my current computer groups to see which one has the fewest number of servers in them, and then add the relevant computer to that group. (Bonus, if they have the same number of computers in that group, add it alphabetically.)

I realize this is a pretty simple question but I have scoured these forums and cannot seem to find anything even close! I appreciate any help I can get. Thanks in advance! :slight_smile:

Skeleton code (no syntax):
For each computer group
…check the number of computers in that group
If less than the group before, keep it
Else discard and move to the next
Once it goes through all of them and finds the smallest group
Put computer into that group

Hi Emily, and welcome to the forums. I can’t say I have an answer for you, but maybe I can raise visibility for your question since no one has addressed it yet.

Could you clarify what purpose this is for? Is this to do some pseudo load balancing between groups? I assume there are groups that you don’t want included in this, so do the target groups have a naming convention? And the target devices would be devices that aren’t already in one of these groups?

Also - I don’t have any experience with this but I suspect if you want to do something like this it would have to be via the REST API. Perhaps someone with more knowledge can chime in. I found some hits on the forum here that also may be helpful:

Hi Sean, thanks so much for the reply! Thanks for the links as well, I have looked at those, but they aren’t quite what we need. We have a large number of servers and the way we are doing things are by manually putting the ones we have into a set number of computer groups that we are using as scan groups. The idea was to then have a fixlet/task that we could run every couple of weeks to grab any new servers that don’t have a group yet, and put them into the computer group with the fewest number of servers in them. Ex. If group_1 has 452 servers and the rest of the groups have 453 in them, we’d like to add that computer to group_1.

Since posting, I have figured out the statement to see how many servers are in a particular group, as well as a statement to check which computers don’t have a group yet!

So all I need to figure out now is the action statement to put a computer into a group, and the syntax to compare the number in each group to each other.

Great to hear. For the remaining part, I am pretty sure you will have to use session relevance and the REST API. But since I don’t have any experience with it yet, let’s summon @leewei to see if he can help.

What makes this a difficult request is that “Group Membership” is determined by the local Client. It’s not something that the Server decides.

Each client only knows about itself, and decides if it think it should be a member of a given “Automatic” group.

One possible way to achieve what you seem to want is to use the ComputerID for a given computer, and MOD it with the number of groups you have, and let the servers sort themselves that way.

I currently use this technique to spread systems on the Internet between our DMZ Relays. It doesn’t result in a perfect leveling between the Relays, but it spreads the clients between the servers fairly evenly.

I’m away from my desk right now, but will post the Relevance for it when I get back to my desk if you want to give it a try.

1 Like

@TimRice Thank you so much for responding and for the help. That is an interesting way of doing things that I hadn’t thought of. That could be a solution for us to try. I would love to see the relevance for that if you have a minute to post it!

The following example assumes that you have 5 different Computer Groups. Each group would have the relevance of “(Computer ID MOD 5) = x” where x = 0-4 representing the MODULUS (remainder) that results from dividing the Computer ID by 5. Each group would use ONE of the following clauses.

Q: (Computer ID MOD 5) = 0 /* ComputerID divided by 5 results in a remainder of "0" */
Q: (Computer ID MOD 5) = 1 /* ComputerID divided by 5 results in a remainder of "1" */
Q: (Computer ID MOD 5) = 2 /* ComputerID divided by 5 results in a remainder of "2" */
Q: (Computer ID MOD 5) = 3 /* ComputerID divided by 5 results in a remainder of "3" */
Q: (Computer ID MOD 5) = 4 /* ComputerID divided by 5 results in a remainder of "4" */

If you want to test them in QnA (Fixlet Debugger) on your computer, be sure that it is configured to use the “Local Client Evaluator” so that the “Computer ID” object for your local computer is available.

2 Likes

@TimRice, beautiful solution to load balance the computers into different groups.
@emilyb28, if the number of computers you have is of reasonable size data set, I can see that this will evenly spread the numbers out.

1 Like

Thank you so much for all your help @TimRice and @leewei and @Sean - we just implemented this solution into our system and it works like a charm! This is exactly what I was looking for. I really appreciate it!