Name

sn_agent.PopulateProxyAgentMonitoredCIsAjax

Description

To populate the sn_agent_policy_proxy_agent_monitored_ci_mapping table

Script

var PopulateProxyAgentMonitoredCIsAjax = Class.create();
PopulateProxyAgentMonitoredCIsAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

  //Method to populate sn_agent_policy_proxy_agent_monitored_ci_mapping table to show the distribution of CIs to proxy agents
  populate: function() {
      var sysId = this.getParameter('sysparm_populate_id');
      var type = this.getParameter('sysparm_populate_type');
      return this.populateDistribution(sysId, type);

  },

  populateDistribution: function(sysId, type) {
      var agentId;
      var policies = [];

      //If we see any issue in population of monitoredCIs distribution as the sn_policy_client_json is not reflected properly for customers then we can ask them to create sn_agent.populate_proxy_agent_monitored_ci_distribution_always system property  with value true so that the distribution will be populated again to show in the UI.
      var populateAlways = gs.getProperty("sn_agent.populate_proxy_agent_monitored_ci_distribution_always", "false");

      if (type == "policy") {
          //If user clicks on UIAction on Policy page to show the distribution need to populate monitoredCIs just for that policy
          policies.push(sysId);
      } else if (type == "agent") {

          agentId = sysId;
          //If user clicks on UIAction on Agent Page, to show the distribution of CIs need to populate monitoredCIs for all the policies that this agent monitors
          var policyProxyAgentMappingGr = new GlideRecord("sn_agent_policy_proxy_agent_mapping");
          policyProxyAgentMappingGr.addQuery("agent", agentId);
          policyProxyAgentMappingGr.query();
          while (policyProxyAgentMappingGr.next()) {
              if (policies.indexOf(policyProxyAgentMappingGr.policy.trim()) == -1) {
                  policies.push(policyProxyAgentMappingGr.policy.trim());
              }
          }
      }

      //Populate the monitoredCIs for the given policies.
      policies.forEach(function(policy) {
          var policyClientsGr = new GlideRecord('sn_agent_policy_clients');
          policyClientsGr.addQuery("policy", policy);
  		policyClientsGr.addQuery('policy.active', 'true');
          policyClientsGr.query();

          //TODO: Added while thinking that multiple rows exists for a policy with different domains
          while (policyClientsGr.next()) {
              //check whether we need to repopulate or not. if hash is same dont need to populate again
              var existingMappingGr = new GlideRecord("sn_agent_policy_proxy_agent_monitored_ci_mapping");
              existingMappingGr.addQuery("policy", policyClientsGr.policy);
              existingMappingGr.addQuery("sys_domain", policyClientsGr.sys_domain);
              existingMappingGr.query();
              if (existingMappingGr.next()) {
                  if ((!populateAlways) && (existingMappingGr.filter_result_hash == policyClientsGr.filter_result_hash)) {
                      //As the hash for this policy is same, the monitoredCIs are already populated for this policy, no need to populate again
                      continue;
                  } else {
                      //As hash changed, populate for this policy again.
                      existingMappingGr.deleteMultiple();
                  }
              }
              var sortedClientCis = JSON.parse(policyClientsGr.getValue("clients_json"));
              for (var agentId in sortedClientCis) {
                  for (var monitoredCI in sortedClientCis[agentId]) {
                      var monitoredCIsGr = new GlideRecord("sn_agent_policy_proxy_agent_monitored_ci_mapping");
                      monitoredCIsGr.setValue("policy", policyClientsGr.policy);
                      monitoredCIsGr.setValue("agent", agentId);
                      monitoredCIsGr.setValue("monitored_ci", monitoredCI);
                      monitoredCIsGr.setValue("filter_result_hash", policyClientsGr.filter_result_hash);
                      monitoredCIsGr.setValue("sys_domain", policyClientsGr.sys_domain);
                      monitoredCIsGr.insert();
                  }
              }
          }
      });
      return "success";
  }
});

Sys ID

4d0c0e3d5bc11110ea1d1b800481c71e

Offical Documentation

Official Docs: