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.

 

Comments
Tera Contributor

Hi Johnny,



Great code btw.   I just wanted to check with you that you've declared and set the regexp variable but did not use it after.   Later down, I see you have a reference to the pattern variable which was never declared nor set.   Can you please clarify if this was a typo?


Kilo Guru

Yep, that was a typo and thank you for catching it. I have corrected it now.


Kilo Guru

Hi Johnny - I'm new to the community and having a problem seeing the attachment with the working script - also - how did you learn about the role of the script include in the discovery process.

ServiceNow Employee
ServiceNow Employee

JJ, to be honest, I learned most of what I know about Discovery from reading the Probes and Sensors, as well as any Script Include that starts with the word "Discovery". This is how I prefer to learn, and it took a lot of time and desire, but I fell in love with Discovery back in 2013 when I first met this platform and that passion drove me to pull it apart and to learn it.

Back then, there wasn't a great deal of learning available for free. Luckily for everyone, today there is a great deal of self-paced learning available and much of it is for fun and for free. Check out this outstanding simulator for Discovery Fundamentals. It doesn't deep dive into the Script Includes, but it will give anyone interested a solid foundation for learning the concepts of Discovery.

Keep in touch with us here on the Community, we love talking about Discovery and even answering questions.

ServiceNow Employee
ServiceNow Employee

The attachment does appear to be missing. I will have to poke around to see if I can find it.

The steps above would allow you to create your own version though.

Tera Contributor

there are two subnets example assume 172.*.*.*/24 and 172.30.*.* are in a A Location discovery schedule and in this subnet there are Citrix workstation servers more than 188 which are not static ip address and they are dynamic Ip keep changes, problem here is we have a discovery issues of citrix workstations and we would like to remove them from Servicenow discovery schedule and add as IP exclusion but since they are not static How I can exclude them from discovery schedule of A Location. is there a way we can exclude the Hostnames to avoid discovery from servicenow whcih can help to resolve discovery errors.