Forum Discussion

Tanvir's avatar
7 years ago

Custom query for dynamic group

Hi

I want create dynamic groups based on server role like print servers, AD servers, file servers etc. Could you please help me to to find out how can i create a custom query for the groupings or custom query if a service is running.

Thank you

Tanvir

  • Hi @Tanvir,

    Apologies for the delay in a response - take a look at some of the PropertySources we ship with the product - if they're not in your account you should be able to pull them down from the repository. Here's an example of the underlying code for the Domain Controllers PropertySource, which looks for the "NTDS" service to be running, and then adds a category to the device (which you can then use to group the devices together.) So you could clone this, change the service it looks for, and change the category it assigns...

    import com.santaba.agent.groovyapi.win32.WMI
    import com.santaba.agent.groovyapi.win32.WMISession
    
    // Set hostname
    def hostname = hostProps.get('system.hostname');
    
    // Form the full query.
    def wmiQuery = "Select name,state from Win32_Service Where Name = 'NTDS'";
    
    try
    {
        // using default namespace
        def session = WMI.open(hostname); 
        def result = session.queryFirst("CIMv2", wmiQuery, 10); 
    
        // Did we get anything?
        if (result.STATE == "Running")
        {
            // Yes, apply the properties
            println 'system.categories=MicrosoftDomainController'
        }
    }
    
    catch(Exception e)
    {
        println e
        return 1;
    }
    
    // Exit by returning 0.
    return 0;

    Best,

    Kerry