Name

global.FindCloudLoadBalancer

Description

Find Cloud Load Balancer CI based on FQDN or IP address

Script

var FindCloudLoadBalancer = Class.create();
FindCloudLoadBalancer.prototype = {
  initialize: function() {
      this.maxSysIdsInQuery = GlideProperties.get('sa.max_sys_ids_in_query', 300);
      this.discoveryHostUtils = new DiscoveryHostUtils();
  },
  /*
   *  Strategy 1: find cmdb_ci_cloud_load_balancer CI where the fqdn field equals
   *              the host_name on the input endpoint
   *  Strategy 2: find cmdb_ci_dns_name where the name field equals the host_name on the endpoint.getContentType()
   *               then follow used by relation to cmdb_ci_cloud_load_balancer
   *  Strategy 3: find record of cmdb_ci_cloud_lb_ipaddress where the name is the IP on the endpoint and follow
   *              Owned by related to the cmdb_ci_cloud_load_balancer
   */
  getHostForEndpoint: function(endpointGr) {
      var hostname = endpointGr.getValue('host_name');
      var lst = [];
      if (hostname != null) {
          gs.debug("FindCloudLoadBalancer: find an active cmdb_ci_cloud_load_balancer with FQDN {0}", hostname);
          // In case the hostname is on cloud load balancer, return the cloud load balancer object
          var gr = new GlideRecord('cmdb_ci_cloud_load_balancer');
          gr.addQuery('fqdn', hostname);
          gr.query();
          while (gr.next()) {
              // In case host parameter is different than the host name (meaning usually it is IP), this enpdoint  is leading to VM in the cloud
              if (endpointGr.host != hostname) {
                  return null;
              }
              //verify the host is active (has valid OperationalStatus && InstallStatus && DiscoverySource)
              if (this.isCloudLBActive(gr))
                  return gr.getUniqueValue();
          }

          var dnsGr = new GlideRecord('cmdb_ci_dns_name');
          dnsGr.addQuery('name', hostname);
          dnsGr.setLimit(this.maxSysIdsInQuery);
          dnsGr.query();
          while (dnsGr.next()) {
              //verify the host is active (has valid OperationalStatus && InstallStatus && DiscoverySource) 
              if (this.checkIsValidGr(dnsGr))
                  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'));
          }
      }
      // If nothing found so far, try to find based on cmdb_ci_cloud_lb_ipaddress
      var ip = endpointGr.ip_address;
      if (lst.length == 0 && JSUtil.notNil(ip)) {
          gs.debug("FindCloudLoadBalancer: following cmdb_ci_cloud_lb_ipaddress candidates with IP {0} and a Owned by relation to an active cmdb_ci_cloud_load_balancer", ip);
          var cloudIpGr = new GlideRecord('cmdb_ci_cloud_lb_ipaddress');
          var qc = cloudIpGr.addQuery('name', ip);
          qc.addOrCondition('ip_address', ip);
          cloudIpGr.query();
          while (cloudIpGr.next()) {
              // Follow owned by relation to the load balancer
              //verify the host is active (has valid OperationalStatus && InstallStatus && DiscoverySource)
              if (this.checkIsValidGr(cloudIpGr)) {
                  relGr = new GlideRecord('cmdb_rel_ci');
                  relGr.addQuery('type', '25242fb2377a9200738d021a54990e88'); // Owned by
                  relGr.addQuery('child', cloudIpGr.getUniqueValue());
                  relGr.addJoinQuery('cmdb_ci_cloud_load_balancer', 'parent', 'sys_id');
                  relGr.query();
                  while (relGr.next()) {
                      var parentId = relGr.getValue('parent');
                      var lbGr = new GlideRecord('cmdb_ci_cloud_load_balancer');
                      lbGr.get(parentId);
                      //verify the host is active (has valid OperationalStatus && InstallStatus && DiscoverySource)
                      if (this.isCloudLBActive(lbGr))
                          return parentId;
                  }
              }
          }
      } else
      if (lst.length > 0) {
          gs.debug("FindCloudLoadBalancer: following cmdb_ci_dns_name candidates with name {0} and a used by relation to an active cmdb_ci_cloud_load_balancer", hostname);
          var cloudLbGr = new GlideRecord("cmdb_ci_cloud_load_balancer");
          cloudLbGr.addQuery("sys_id", lst);
          cloudLbGr.query();
          while (cloudLbGr.next()) {
              //verify the host is active (has valid OperationalStatus && InstallStatus && DiscoverySource)
              if (this.isCloudLBActive(cloudLbGr))
                  return cloudLbGr.getUniqueValue();
          }
      }
      return null;
  },

  isCloudLBActive: function(cloudLBRecord) {
      if (cloudLBRecord) {
          return cloudLBRecord.state != 'terminated' && this.discoveryHostUtils.isHostActive(cloudLBRecord);
      }
      return null;
  },

  checkIsValidGr: function(gr) {
      return global.JSUtil.toBoolean(gs.getProperty("sa_find_host_scripts.check_host_active.enabled", "true")) ? this.discoveryHostUtils.isHostActive(gr) : true;
  },

  type: 'FindCloudLoadBalancer'
};

Sys ID

57aa70e3c32032003e76741e81d3ae55

Offical Documentation

Official Docs: