The CreatorCon Call for Content is officially open! Get started here.

Discovery - Populate custom attribute based on discovery schedule

LikeARabbit
Giga Expert

In our environment we have a handful of different departments who own infrastructure that will be brought in to the CMDB via Discovery.

Obviously we'll see server/device names in the CMDB but we'd like a way to quickly filter CMDB records based on the department that "owns" them. I've added a reference (to our department table) in the CMDB that we're using to manually specify which department owns the record. This is fine for now as we're only scanning a couple hundred servers (one department) but ideally I want to automate this.

Because we will be assigning Discovery Schedules based on departments it makes sense (or at least I think it does) to populate the department field for the CI based on which department is specified in the Discovery Schedule. I've already added a reference to department (table) in the discovery_schedule table, it's obvious though that I need to add a business rule (or something else) to add that value to the CI at time of discovery. I'm drawing a blank on this next step.

Any suggestions?

Thanks!

3 REPLIES 3

Erik Gardner1
Giga Guru

Hi,

 

As you say, I would do this with an Business Rule on cmdb_ci_server (After, Insert and Update and Condition: last_discovered Changes).

 

1. Query the discovery_device_history table and match with our CI

2. Dot-walk discovery_device_history record to get the Schedule name

3. Query the cmn_department table for a name that matches our schedule

4. Set Department on our CI with the found Department

 

(function executeRule(current, previous /*null when async*/) {


	//Query the Discovery Device History table for records which matches our CI
	var deviceHistory =  new GlideRecord('discovery_device_history');
	deviceHistory.addEncodedQuery("cmdb_ci=" + current.sys_id);
	deviceHistory.orderBy('sys_updated');
	deviceHistory.setLimit(1);
	deviceHistory.query();
	
	//If we find something, query the Department table for a record matching our Schedule
	if (deviceHistory.hasNext()) {
		deviceHistory.next();
		var schName = "Latest Discovery Schedule Name: " + deviceHistory.status.dscheduler.getDisplayValue(); //Dot-walking the Discovery Device History record here to find it's Schedule 
		//gs.addInfoMessage(schName);	
		
		var dep = new GlideRecord('cmn_department');
		dep.addEncodedQuery('nameLIKE' + deviceHistory.status.dscheduler.getDisplayValue());
		dep.query();
		
		if (dep.getRowCount() > 1) {gs.addErrorMessage("Too many Departments match! Aborting... ");}
		
		//setting the Department of our CI with our result
		else if (dep.hasNext() && dep.getRowCount() == 1){
			dep.next();
			current.department = dep.sys_id;
		}
	}
	
	current.update();

})(current, previous);

Henrik Jutterst
Tera Guru

I found this post when looking for similar question and after some digging and help from a collegue we found this KB article from ServiceNow, describing what/how this can be done.

 

How to pass discovery schedule fields to a CI record via discovery 

 

 

Here's my example and this worked for me:

HenrikJutterst_0-1723812802856.png




// Reference:
// https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0820001
//

var cmdb_ci = current.cmdb_ci;
if (cmdb_ci) {

    var discoveryScheduleName = current.status.dscheduler.name.toString();
    gs.info("Script Action: Discovery Device Complete - Custom filed. " + discoveryScheduleName);

    // be able to set values to CI based on what Discovery Schedule was running
    if (discoveryScheduleName.toUpperCase().includes("PRODUCTION")) {
		cmdb_ci.short_description = cmdb_ci.short_description + '.';
		cmdb_ci.update();

    } else if (discoveryScheduleName.toUpperCase().includes("VERIFICATION")) {
		cmdb_ci.short_description = cmdb_ci.short_description + '.';
		cmdb_ci.update();

    } else {
        gs.info("No matching Discovery Schedule found.");
    }
}

 

 

This did the job for me.

Just found this post for a more GUI based version to do this.
Have a look here:
Discovery Schedule Attributes 

Awesome work by Doug Schulze and team.