Name

sn_agent.AgentDiscoveryAgentlessCompatibilityUtil

Description

No description available

Script

var AgentDiscoveryAgentlessCompatibilityUtil = Class.create();
AgentDiscoveryAgentlessCompatibilityUtil.prototype = {
  
  initialize: function() {
  },
  
  /***
   * shouldProcessAgentDiscoveryPayload(sys_id_of_ci, sys_class_name)
   *
   * The following function returns whether we should process an Agent Discovery only if an Agentless discovery was not performed recently by looking at these values:
   * 1) Is the property sn_agent.disco_disable_ci_clobber_of_agentless_disco true or false (default to true) - if false the method will return true
   * 2) return value of ciWasRecentlyDiscoveredByAgentlessDisco() method
   *
   * @param String sys_id (required) - the sys_id of a CI in the CMDB
   * @param String sys_class_name (required) - the class name of the CI i.e. cmdb_ci_linux_server
   * 
   * @return boolean - should we run agent discovery
   */
  shouldProcessAgentDiscoveryPayload: shouldProcessAgentDiscoveryPayload,
  
  /***
   * shouldProcessAgentDiscoveryPayloadOnCi(sys_id_of_ci, sys_class_name, checkRecentAgentDiscovery)
   *
   * The following function returns whether we should process an Agent Discovery, if an Agentless or Agent discovery was not performed recently by looking at these values:
   * 1) Is the property sn_agent.disco_disable_ci_clobber_of_agentless_disco true or false (default to true) - if false the method will return true
   * 2) return value of ciWasRecentlyDiscoveredByAgentlessDisco() method
   * 3) return value of ciWasRecentlyDiscoveredByEndpointDiscovery() method
   *
   * @param String sys_id (required) - the sys_id of a CI in the CMDB
   * @param String sys_class_name (required) - the class name of the CI i.e. cmdb_ci_linux_server
   * @param boolean checkRecentAgentDiscovery (required) - should we check if the ci was discovered by agent discover recently
   * 
   * @return boolean - should we run agent discovery
   */
  shouldProcessAgentDiscoveryPayloadOnCi: shouldProcessAgentDiscoveryPayloadOnCi,

  /***
   * ciWasRecentlyDiscoveredByAgentlessDisco(sys_id_of_ci, sys_class_name)
   *
   * The following function uses a sys_id and sys_class_name of a CI to determine if
   * a CI was recently discovered via agentless discovery based on the property for
   * endpoint_disco_ci_clobber_of_agentless_disco_threshold_days. Please see property 
   * definition for more info.
   *
   * @param String sys_id (required) - the sys_id of a CI in the CMDB
   * @param String sys_class_name (required) - the class name of the CI i.e. cmdb_ci_linux_server
   * 
   * @return boolean - was the CI recently discovered via agentless discovery (true/false)
   */
  ciWasRecentlyDiscoveredByAgentlessDisco: ciWasRecentlyDiscoveredByAgentlessDisco,
  
  /***
   * ciWasRecentlyDiscoveredByEndpointDiscovery(sys_id_of_ci, sys_class_name)
   *
   * Function is used to determine if the CI has recently been discovered via
   * ACC-V based on the property for endpoint_disco_minimum_threshold_for_rediscovery_minutes.
   * Please see system property definition for more info.
   *
   * @param String sys_id (required) - the sys_id of a CI in the CMDB
   * @param String sys_class_name (required) - the class name of the CI i.e. cmdb_ci_linux_server
   *
   * @return boolean - was the CI recently discovered via endpoint discovery (true/false)
   */
  ciWasRecentlyDiscoveredByEndpointDiscovery: ciWasRecentlyDiscoveredByEndpointDiscovery,
  
  /***
   * shouldExecuteEndpointDiscoveryOnCI(sys_id_of_ci, sys_class_name)
   *
   * Function that will determine if we should execute endpoint discovery
   * on the CI based on the sys_id. 
   *
   * @param String sys_id (required) - the sys_id of a CI in the CMDB
   * @param String sys_class_name (required) - the class name of the CI i.e. cmdb_ci_linux_server
   *
   * @return boolean - should we discover CI using endpoint discovery (true/false)
   */
  shouldExecuteEndpointDiscoveryOnCi: shouldExecuteEndpointDiscoveryOnCi,
  
  /***
   * __invokeCheckForEndpointDiscovery(agent_sys_id)
   *
   * Private helper function to invoke endpoint discovery on agent
   *
   * @param String agent_sys_id (required) - the sys_id of the agent to run endpoint discovery on
   */
  __invokeCheckForEndpointDiscovery: __invokeCheckForEndpointDiscovery,
  
  /***
   * selectivelyInvokeEndpointDiscoveryForAgent(agent_sys_id)
   *
   * Invoke endpoint discovery based on agent sys_id. Will invoke if there is no CI associated with
   * agent or if longer enough interval has passed since discovery.
   *
   * @param String agent_sys_id (required) - the sys_id of the agent to run endpoint discovery on
   */
  selectivelyInvokeEndpointDiscoveryForAgent: selectivelyInvokeEndpointDiscoveryForAgent,

  /***
   * __loadCiValues()
   *
   * Load the CI values i.e., sys_id and class of the CI through dot-walking, if dot-walking doesn't work,
   * then load the values from the database using __loadCiValuesFromDB
   *
   */
  __loadCiValues: __loadCiValues,

  /***
   * __loadCiValuesFromDB()
   *
   * If dot-walking doesn't work in __loadCiValues(), invoke __loadCiValuesFromDB() to fetch the value
   * by querying the database
   *
   */
  __loadCiValuesFromDB: __loadCiValuesFromDB,

  /***
   * __doesAgentHaveHostCiAssociated(agentGr)
   *
   * The following is a helper function to determine based on the 
   * agent glide record if there exists a non agent CI that could
   * be associated with the agent.
   *
   * @param GlideRecord agentGr (require) - a glide record object that is the agent we want to check
   */
  __doesAgentHaveHostCiAssociated: __doesAgentHaveHostCiAssociated,

  type: 'AgentDiscoveryAgentlessCompatibilityUtil'
};

var AGENTLESS_DISCO_SOURCES = ['ServiceNow', 'ServiceWatch'];
var AGENT_CLIENT_COLLECTOR = 'AgentClientCollector';
var ACC_VISIBILITY = 'ACC-Visibility';
var au = new global.ArrayUtil();
var computerCI = {};

function ciWasRecentlyDiscoveredByAgentlessDisco(sys_id_of_ci, sys_class_name) {
  var ciGr = new GlideRecord(sys_class_name);
  if (!ciGr.get('sys_id', sys_id_of_ci)) {
  	gs.debug("Agent Client Collector Framework: In AgentDiscoveryAgentlessCompatibilityUtil.ciWasRecentlyDiscoveredByAgentlessDisco()"
  			+ " could not find configuration item by sys_id, returning false");
  	return false;
  }

  if (au.indexOf(AGENTLESS_DISCO_SOURCES, ciGr.getValue('discovery_source')) == -1) {
  	gs.debug("Agent Client Collector Framework: In AgentDiscoveryAgentlessCompatibilityUtil.ciWasRecentlyDiscoveredByAgentlessDisco()"
  			+ " Discovery Source of configuration item is not one of the Agentless discovery source types");
  	return false;
  }

  //retrieve the system property for threshold days
  var sysProp = gs.getProperty('sn_agent.disco_ci_clobber_of_agentless_disco_threshold_days', 14);

  var dt = new GlideDateTime();
  dt.addDaysUTC(-parseInt(sysProp));

  if (dt.onOrAfter(new GlideDateTime(ciGr.getValue('last_discovered')))) {
  	gs.debug("Agent Client Collector Framework: In AgentDiscoveryAgentlessCompatibilityUtil.ciWasRecentlyDiscoveredByAgentlessDisco()"
  			+ " configuration item was not recently discovered by agent based discovery, returning false");
  	return false;
  } else {
  	gs.debug("Agent Client Collector Framework: In AgentDiscoveryAgentlessCompatibilityUtil.ciWasRecentlyDiscoveredByAgentlessDisco()"
  			+ " configuration item was recently discovered by agent based discovery, returning true");
  	return true;
  }
}

function ciWasRecentlyDiscoveredByEndpointDiscovery(sys_id_of_ci, sys_class_name) {
  var ciGr = new GlideRecord(sys_class_name);
  if (!ciGr.get('sys_id', sys_id_of_ci)) {
  	gs.debug("Agent Client Collector Framework: In AgentDiscoveryAgentlessCompatibilityUtil.ciWasRecentlyDiscoveredByEndpointDiscovery()"
  			+ " could not find configuration item by sys_id, returning false");
  	return false;
  }

  if (ciGr.getValue('discovery_source') !== AGENT_CLIENT_COLLECTOR && ciGr.getValue('discovery_source') !== ACC_VISIBILITY) {
  	gs.debug("Agent Client Collector Framework: In AgentDiscoveryAgentlessCompatibilityUtil.ciWasRecentlyDiscoveredByEndpointDiscovery()"
  			+ " configuration item's discovery source is not Agent Client Collector or ACC-Visibility, returning false");
  	return false;
  }

  //retrieve the system property for rediscovery threshold minutes
  var sysProp = gs.getProperty('sn_agent.disco_minimum_threshold_for_rediscovery_minutes', 60);

  var dt = new GlideDateTime();
  dt.addSeconds(-(parseInt(sysProp) * 60));

  if (dt.onOrAfter(new GlideDateTime(ciGr.getValue('last_discovered')))) {
  	gs.debug("Agent Client Collector Framework: In AgentDiscoveryAgentlessCompatibilityUtil.ciWasRecentlyDiscoveredByEndpointDiscovery()"
  			+ " configuration item was not recently discovered by endpoint discovery, returning false");
  	return false;
  } else {
  	gs.debug("Agent Client Collector Framework: In AgentDiscoveryAgentlessCompatibilityUtil.ciWasRecentlyDiscoveredByEndpointDiscovery()"
  			+ " configuration item was recently discovered by endpoint discovery, returning true");
  	return true;
  }
}

function shouldProcessAgentDiscoveryPayload(sys_id_of_ci, sys_class_name) {
  return shouldProcessAgentDiscoveryPayloadOnCi(sys_id_of_ci, sys_class_name, false);
}

function shouldExecuteEndpointDiscoveryOnCi(sys_id_of_ci, sys_class_name) {
  return shouldProcessAgentDiscoveryPayloadOnCi(sys_id_of_ci, sys_class_name, true);
}

function shouldProcessAgentDiscoveryPayloadOnCi(sys_id_of_ci, sys_class_name, checkRecentAgentDiscovery) {
  var sysProp = gs.getProperty('sn_agent.disco_disable_ci_clobber_of_agentless_disco', true) == 'true';

  if (!sysProp) {
  	gs.debug("Agent Client Collector Framework: In AgentDiscoveryAgentlessCompatibilityUtil.shouldExecuteEndpointDiscoveryOnCi() ACC-V property for clobbering configuration items is set to false. Checking if configuration item was recently discovered by Endpoint discovery");
  	
  	// If we want to check if ci was recently discovered by agent discovery
  	if (checkRecentAgentDiscovery)
  		return !ciWasRecentlyDiscoveredByEndpointDiscovery(sys_id_of_ci, sys_class_name);
  	
  	return true;
  } else {
  	gs.debug("Agent Client Collector Framework: In AgentDiscoveryAgentlessCompatibilityUtil.shouldExecuteEndpointDiscoveryOnCi() ACC-V property for clobbering configuration items is set to true, checking if configuration item was recently discovery by agentless discovery");
  	
  	if (ciWasRecentlyDiscoveredByAgentlessDisco(sys_id_of_ci, sys_class_name)) {
  		gs.debug("Agent Client Collector Framework: Configuration item was recently discovered by agentless discovery, returning false");
  		
  		return false;
  	} else {
  		gs.debug("Agent Client Collector Framework: In AgentDiscoveryAgentlessCompatibilityUtil.shouldExecuteEndpointDiscoveryOnCi() Configuration item was not recently discovered by agentless discovery. Checking if configuration item was recently discovered by endpoint discovery");
  		
  		if (checkRecentAgentDiscovery)
  			return !ciWasRecentlyDiscoveredByEndpointDiscovery(sys_id_of_ci, sys_class_name);
  		
  		return true;
  	}
  }
}

function __invokeCheckForEndpointDiscovery(agent_sys_id) {
  gs.debug("Agent Client Collector Framework: In AgentDiscoveryAgentlessCompatibilityUtil.__invokeCheckForEndpointDiscovery() " +
  		"Invoking check for endpoint discovery on agent with sys_id: " + agent_sys_id);
  var agentGr = new GlideRecord('sn_agent_cmdb_ci_agent');
  agentGr.addQuery('sys_id', agent_sys_id);
  agentGr.query();

  // checkDefId below is the sys_id of the endpoint discovery check definition
  var checkObj = AgentDiscoverySharedUtils.getDiscoveryCheckToRun();
  var discoveryCheck = {
  	'checkDefId': checkObj.checkSysId
  };
  var agentNowHandler = new AgentNowHandler();
  agentNowHandler.runCheckForCis(agentGr, discoveryCheck, 0);
}

function selectivelyInvokeEndpointDiscoveryForAgent(agent_sys_id) {
  var agentGr = new GlideRecord('sn_agent_cmdb_ci_agent');
  agentGr.addQuery('sys_id', agent_sys_id);
  agentGr.query();
  if (!agentGr.next()) {
  	gs.error("Agent Client Collector Framework: In AgentDiscoveryAgentlessCompatibilityUtil.selectivelyInvokeEndpointDiscoveryForAgent()"
  			+ " Selected agent sys id does not match agent agent record");
  	return;
  }

  if (!__doesAgentHaveHostCiAssociated(agentGr)) {
  	gs.debug("Agent Client Collector Framework: In AgentDiscoveryAgentlessCompatibilityUtil.selectivelyInvokeEndpointDiscoveryForAgent()"
  			+ " Agent does not have host configuration item associated invoking endpoint discovery");
  	__invokeCheckForEndpointDiscovery(agent_sys_id);
  } else {
      this.computerCI = agentGr.agent_extended_info.cmdb_ci;
      __loadCiValues();

      gs.debug("Agent Client Collector Framework: In AgentDiscoveryAgentlessCompatibilityUtil.selectivelyInvokeEndpointDiscoveryForAgent()" +
          " Checking if endpoint discovery should be executed based on configuration item with sys_id: " + this.computerCI.sys_id);

      if (this.computerCI.sys_id && this.computerCI.sys_class_name && shouldExecuteEndpointDiscoveryOnCi(this.computerCI.sys_id, this.computerCI.sys_class_name))
          __invokeCheckForEndpointDiscovery(agent_sys_id);
  }
}

function __loadCiValues() {
  if (this.computerCI.sys_id && this.computerCI.sys_class_name)
      return;
  else
      __loadCiValuesFromDB();
}

function __loadCiValuesFromDB() {
  var computerGR = new GlideRecord('cmdb_ci_computer');
  if (!computerGR.get('sys_id', this.computerCI))
      return;
  this.computerCI.sys_class_name = computerGR.getValue('sys_class_name');
  this.computerCI.sys_id = computerGR.getValue('sys_id');
}
/***
* The following is a helper function to determine based on the
* agent glide record if there exists a non agent CI that could
* be associated with the agent.
*/
function __doesAgentHaveHostCiAssociated(agentGr) {
  if (agentGr.agent_extended_info && agentGr.agent_extended_info.cmdb_ci) {
  	gs.debug("Agent Client Collector Framework: In AgentDiscoveryAgentlessCompatibilityUtil.__doesAgentHaveHostCiAssociated() " +
  			"Agent record has cmdb_ci record associated with it, returning true");
  	return true;
  }

  return false;
}

Sys ID

83b5a06fff0960108ec45897d53bf119

Offical Documentation

Official Docs: