Dynamic group queries are a great way to automatically organize your nodes into node groups, but what should you do when you have a complex query?

The Problem

Dynamic Group Queries are a great tool in UpGuard to promote automation. However, they are limited in that you can only have a single query performed at a time.

For example, you may want a dynamic node group based on a service installed on a node (Windows Firewall, in this example):

services:Windows Firewall EXACT

What if, however, you wanted to test to see if one service exists, but another service is absent? There are unfortunately no complex group queries that can combine these two requirements.

The Solution

To get around this, we’ll use a bit of Powershell to create a configuration item that we can use for our dynamic query.

In this example, we’ll create a dynamic node group that includes nodes that have the Windows Firewall service installed, but does not have the SQL Server (MSSQLSERVER) service installed.

First, we start by creating a Powershell scan option with the following parameters:

  • Description: Complex Group Membership
  • Key Name: Leave this empty
  • Query:
$result = "no"
if (Get-Service -Name "MpsSvc")
{
    if (-not (Get-Service -Name "MSSQLSERVER"))
    {
        $result = "yes"
    }
}
$result

You can adjust the query to check for any number of items on a node to determine if this node should be in the group or not. If it should be in the node group, then this script will output yes, otherwise, it outputs no.

complex-dynamic-node-group-01

So now we can create a dynamic group query that uses this Powershell script to determine group membership:

PowerShell:Complex Group Membership WITH Value:yes EXACT

You can also do this on Linux nodes with a shell script.

Tags: