Name

global.GetDiscoverySchedulerConfigPerProbe

Description

No description available

Script

var GetDiscoverySchedulerConfigPerProbe = Class.create();
GetDiscoverySchedulerConfigPerProbe.prototype = {
  initialize: function() {},

  exclusionList: ['description', 'probe', 'name', 'active'],
  CACHE_NAME: 'running_process_filter_cache',

  getConfig: function(probe, agentCorrelator) {
      var dSchedulerSysId = this.getDiscoveryScheulderFromAgentCorrelator(agentCorrelator);

      return this.getConfigFromCache(probe, dSchedulerSysId);
  },

  getDiscoveryScheulderFromAgentCorrelator: function(agentCorrelator) {
      if (!agentCorrelator)
          return undefined;

      var dStatusGr = new GlideRecord('discovery_status');
      dStatusGr.addQuery('dscheduler', agentCorrelator);
      dStatusGr.query();
      if (dStatusGr.next())
          return dStatusGr.getValue('sys_id');

      return undefined;
  },

  getConfigFromCache: function(probe, dSchedulerSysId) {
      var filters = sn_scoped_cache.ScopedCacheManager.get(this.CACHE_NAME, probe);
      if (!filters) {
          // If there was no cache then get the config from the database and cache it.
          // We do NOT want to cahce per discovery schedule, we cache all configuration and filter in memory.
          filters = this.getConfigFromDatabase(probe);
          sn_scoped_cache.ScopedCacheManager.put(this.CACHE_NAME, probe, JSON.stringify(filters));
      }

      // Make sure filters is an object.
      if (typeof filters == 'string')
          filters = JSON.parse(filters);

      // Now filter the results based on discovery scheduled sys id.
      filters = filters.filter(function(filterDef) {
          return (!filterDef.discovery_schedule || filterDef.discovery_schedule == dSchedulerSysId);
      });

      return JSON.stringify(filters);
  },

  getConfigFromDatabase: function(probe) {
      // Get all config per this probe, regardless of discovery schedule
      var filtersGr = new GlideRecord('running_process_filter');
      filtersGr.addQuery('probe', probe);
      filtersGr.addActiveQuery();
      filtersGr.query();
      var filters = [];

      while (filtersGr.next()) {
          var filter = this.getQueriesAsJS(filtersGr.getValue('filter'));
          var ds = filtersGr.getValue('discovery_schedule');
          if (ds)
              filter.discovery_schedule = ds;
          filter.is_case_sensitive_value = '' + filtersGr.getValue('is_case_sensitive_value');

          filters.push(filter);
      }

      return filters;
  },

  getQueriesAsJS: function(filter) {
      var qs = new GlideQueryString('cmdb_running_process', filter);

      // Will populate the terms
      qs.deserialize();
      var terms = qs.getTerms();

      /*
      	Creating a JSON that should look like this
      	{
      		"newQueries": [
      		{
      			"andOperation1": {
      			"orOperation0": {
      				"field": "name",
      				"op": "STARTSWITH",
      				"value": "a"
      			}
      			}
      		}
      		],
      		"is_case_sensitive_value": "1"
      	}
      */
      var qJs = {};
      qJs.newQueries = [];
      var andIndex = 0;
      var orIndex = 0;
      var newQuery = {};

      for (var i = 0; i < terms.size(); i++) {
          var term = terms.get(i);

          if (term.isNewQuery()) {
              if (Object.keys(newQuery).length > 0)
                  qJs.newQueries.push(newQuery);
              andIndex = 0;
              orIndex = 0;
              newQuery = {};
          }

          if (!term.isOR())
              andIndex++;
          else
              orIndex++;

          if (!newQuery['andOperation' + andIndex])
              newQuery['andOperation' + andIndex] = {};

          if (!newQuery['andOperation' + andIndex]['orOperation' + orIndex])
              newQuery['andOperation' + andIndex]['orOperation' + orIndex] = {};

          newQuery['andOperation' + andIndex]['orOperation' + orIndex].field = term.getField();
          newQuery['andOperation' + andIndex]['orOperation' + orIndex].op = term.getOperator();
          newQuery['andOperation' + andIndex]['orOperation' + orIndex].value = term.getValue();
      }

      if (Object.keys(newQuery).length > 0)
          qJs.newQueries.push(newQuery);

      return qJs;
  },

  type: 'GetDiscoverySchedulerConfigPerProbe'
};

Sys ID

7e954c70fff9a5108ec45897d53bf1fe

Offical Documentation

Official Docs: