johnnyjava
Kilo Guru

In preparing to write this up, I feel I may becoming the King of Excludes. I've written about Quick Excludes for Discovery Schedules to easily add excludes based on IP Address data for devices already in the CMDB from other integration sources. Today I'm going to show you another way to exclude based on interjecting a hostname regex matching pattern to keep Discovery from adding Hosts to the CDMB based on the name of the device.

 

Take a regex such as the following:

 

//hostnames ending in p01 or P2 or similar

var regexp = new Pattern("[pP][0-9]$");

 

This simple pattern is designed to match on strings with a naming convention that indicates production environment server names. But where to place it?

 

One idea would be to block the insert against the cmdb_ci_hardware table using a Business Rule. An attempt to do this using gr.setAbortAction(true) could work, but what about all the time spent exploring those devices? Discovery Probes and Sensors do a lot of work to interrogate the target servers before and after record insertion. If we aren't interested in all that delicious data - for whatever reason - why should we spend that time in the Exploration phase?

 

The Identification phase is where Discovery decides if a CI record already exists and decides to write the data into a new record or update a matching record. If we can subvert this process, then we can efficiently stop Exploration and even nicely log what we are up to. One day we might want to add those devices, and it would be nice to leave it clear why these devices aren't showing up in the CMDB.

 

The Sensors involved are calling the DiscoveryJSONIDSensor and DiscoveryIDSensor Script Includes. See here the code we will add to the DiscoverJSONIDSensor:

 

//add this at line 89 of DiscoveryJSONIDSensor and DiscoveryIDSensor Script Includes

var regexp = new Pattern("[pP][0-9]$"); //hostnames ending in p01 or P2 or similar

var host_name = this.ciData.data.host_name;

if (!host_name)

      host_name = this.ciData.data.name;

 

if (!regexp.test(host_name)){

      this.explore = false;

      this.setTriggerProbes(false);        

      logger.info('Non-prod machine: '+ host_name +'. Not exploring device', this.type, this.getEccQueueId());

      return false;

} else {

      logger.info('Production Machine: '+ host_name +'. Continue to explore device', this.type, this.getEccQueueId());

}

 

I've attached the fully patched version of that script for reference. See here the output in the Discovery Logs, showing neatly what we have done:

 

Resulting Discovery Logs.png

 

I imagine we could also inject properties into the ciData.data object like environment. I haven't tried that yet, but I would encourage everyone to familiarize themselves with the Discovery Script Includes, including the CI class which is the basis of the ciData object. I welcome any and all comments, questions and concerns.

 

5 Comments