Name

global.FindHostStrategies

Description

No description available

Script

var FindHostStrategies = Class.create(); 
FindHostStrategies.prototype = {
  initialize: function() {
  	this.maxSysIdsInQuery = GlideProperties.get('sa.max_sys_ids_in_query', 300);
  },

  /**
  * get sys_class_name of the CI hosted on the host we found in findLogicalDataCenterFromHostname
  */
  getCiClassName: function() {
  	if (JSUtil.notNil(this.ci_class))
  		return this.ci_class;
  	return null;
  },

  /*
  * The logic is as follows:
  * Plan A:
  *     1) find cis where fqdn.equals(hostname)
  *     2) from those cis traverse "hosted on::hosts" relations and look for children that extend cmdb_ci_logical_datacenter
  * Plan B:
  *     1) find cmdb_ci_dns_name where name.equals(hostname)
  *     2) from those cis traverse "uses::used by" relations and take the parents
  *     3) from parents repeat step 2 of plan A
  */
  findLogicalDataCenterFromHostname: function(hostname) {
  	if (JSUtil.nil(hostname))
  		return null;
  	
  	// Plan A
  	var lst = [];
  	var cisGr = new GlideRecord('cmdb_ci');
  	cisGr.addQuery('fqdn', hostname);
  	var avoidClassesList = gs.getProperty("sm.host.identification.find_ldc_hosts_to_exclude","cmdb_ci_dns_name,cmdb_ci_vmware_instance,cmdb_ci_vm_instance");
  	cisGr.addQuery('sys_class_name','NOT IN',avoidClassesList);
  	cisGr.setLimit(this.maxSysIdsInQuery);
  	cisGr.query();
  	while (cisGr.next()) {
  		lst.push(cisGr.getUniqueValue());
  	}

  	var ldc = this.findLogicalDataCenterFromRelations(lst);
  	if (ldc)
  		return ldc;

  	// Plan B
  	lst = [];
  	var dnsGr = new GlideRecord('cmdb_ci_dns_name');
  	dnsGr.addQuery('name', hostname);
  	dnsGr.setLimit(this.maxSysIdsInQuery);
  	dnsGr.query();
  	while (dnsGr.next()) {
  		lst.push(dnsGr.getUniqueValue());
  	}

  	relGr = new GlideRecord('cmdb_rel_ci');
  	relGr.addQuery('type', 'cb5592603751200032ff8c00dfbe5d17'); // uses::used by
  	relGr.addQuery('child', lst);
  	relGr.setLimit(this.maxSysIdsInQuery);
  	relGr.query();
  	lst = [];
  	while (relGr.next()) {
  		lst.push(relGr.getValue('parent'));
  	}

  	return this.findLogicalDataCenterFromRelations(lst);
  },

  findLogicalDataCenterFromRelations: function(parents) {
  	var candidates = [];

  	var relGr = new GlideRecord('cmdb_rel_ci');
  	relGr.addQuery('type', '5f985e0ec0a8010e00a9714f2a172815'); // hosted on::hosts
  	relGr.addQuery('parent', parents);
  	relGr.addJoinQuery('cmdb_ci_logical_datacenter', 'child', 'sys_id');
  	relGr.query();

  	while (relGr.next()) {
  		candidates.push(relGr.getValue('child'));
  		if (candidates.length == 1)
  			this.ci_class = relGr.parent.sys_class_name;
          else {
  			if (this.ci_class != relGr.parent.sys_class_name)
  				this.ci_class = null;
  		}
  	}

  	// order all candidates by update time and return the most recent
  	var cisGr = new GlideRecord('cmdb_ci_logical_datacenter');
  	cisGr.addQuery('sys_id', candidates);
  	cisGr.orderByDesc('sys_updated_on');
  	cisGr.query();
  	if (cisGr.next())
  		return cisGr.getUniqueValue();

  	return null;
  },

  /*
  * The logic is as follows:
  *     1) find ip address cis where ip_address.equals(ip)
  *     2) from those cis traverse "Owns::Owned by" relations and take the parents
  *     3) from parents traverse "hosted on::hosts" relations and look for children that extend cmdb_ci_logical_datacenter
  */
  findLogicalDataCenterFromIp: function(ip) {
  	if (JSUtil.nil(ip))
  		return null;

  	if (ip == '127.0.0.1')
  		return null;

  	var lst = [];
  	var cisGr = new GlideRecord('cmdb_ci_ip_address');
  	cisGr.addQuery('ip_address', ip);
  	cisGr.setLimit(this.maxSysIdsInQuery);
  	cisGr.query();
  	while (cisGr.next()) {
  		lst.push(cisGr.getUniqueValue());
  	}

  	var relGr = new GlideRecord('cmdb_rel_ci');
  	relGr.addQuery('type', '25242fb2377a9200738d021a54990e88'); // Owns::Owned by
  	relGr.addQuery('child', lst);
  	relGr.setLimit(this.maxSysIdsInQuery);
  	relGr.query();
  	lst = [];
  	while (relGr.next()) {
  		lst.push(relGr.getValue('parent'));
  	}

  	return this.findLogicalDataCenterFromRelations(lst);
  },

  	/*
  * The logic is as follows:
  *     1) find cmdb_ci_dns_alias having the name specified
  *     2) from those cis traverse "Used: Used by" relations and take the children. those children should be cmdb_ci_dns_name
  *     3) take the name of the dns_name and call findLogicalDataCenterFromHostname
  */
  findLogicalDataCenterFromDnsAlias: function(aliasName) {
  	if (JSUtil.nil(aliasName))
  		return null;

  	var lst = [];
  	var cisGr = new GlideRecord('cmdb_ci_dns_alias');
  	cisGr.addQuery('name', aliasName);
  	cisGr.setLimit(this.maxSysIdsInQuery);
  	cisGr.query();
  	while (cisGr.next()) {
  		lst.push(cisGr.getUniqueValue());
  	}

  	var relGr = new GlideRecord('cmdb_rel_ci');
  	relGr.addQuery('type', '6afd799338a02000c18673032c71b87b'); // Used by: Uses
  	relGr.addQuery('child', lst);
  	relGr.setLimit(this.maxSysIdsInQuery);
  	relGr.query();
  	while (relGr.next()) {
  		var dnsNameGr = new GlideRecord('cmdb_ci_dns_name');
  		if (dnsNameGr.get(relGr.parent)) {
  	        var ldc = this.findLogicalDataCenterFromHostname(dnsNameGr.name);
  		    if (ldc)
  			    return ldc;
  			ldc = this.findLogicalDataCenterFromIp(dnsNameGr.ip_address);
  		    if (ldc)
  			    return ldc;
  		}
  	}
  	return null;
  },

  type: 'FindHostStrategies'
};

Sys ID

73416022c35322000f511624a1d3aee5

Offical Documentation

Official Docs: