Name

sn_agent.CheckInstanceTestBuilder

Description

Build Test Check Requests From Check Instances.

Script

var CheckInstanceTestBuilder = Class.create();
CheckInstanceTestBuilder.prototype = Object.extendsObject(AbstractTestCheckBuilder, {
  // Set the agent id to run the test. Allowed only in case the tested check instance and policy are proxy.
  withProxyAgentId: function(agentId) {
      this.agentId = agentId;
      return this;
  },

  _build: function() {
      var retJson = {
          test_result_id: null,
          error: null
      };
      var checkInstanceJson = this.getCheckGr(this.CHECK_INSTANCES);
      if (!gs.nil(checkInstanceJson.error)) {
          retJson.error = checkInstanceJson.error;
          return retJson;
      }
      if (checkInstanceJson.checkGr.getValue("background") == "1") {
          retJson.error = this.CHECK_BACKGROUND_ERR;
          return retJson;
      }
      var policyGr = checkInstanceJson.checkGr.getElement("monitoring_policy").getRefRecord();
      var ciValidationJson = this.validateCiAndPolicy(policyGr);
      if (!gs.nil(ciValidationJson.error)) {
          retJson.error = ciValidationJson.error;
          return retJson;
      }
      var err = this.validateProxy(policyGr, ciValidationJson.table_name);
      if (!gs.nil(err)) {
          retJson.error = err;
          return retJson;
      }
      if (gs.nil(this.agentId) && !(new AgentNowHandler().hasAgent(this.ciId))) {
          retJson.error = this.CI_NO_AGENT_ERR;
          return retJson;
      }
      err = this.validateCreds(false, policyGr);
      if (!gs.nil(err)) {
          retJson.error = err;
          return retJson;
      }
      retJson.test_result_id = this.monSync.internalAddTestResults(this.checkId, "false", this.ciId, ciValidationJson.table_name, this.agentId, this.credId, this.credAliasId, checkInstanceJson.checkGr.getValue("command"), policyGr.getUniqueValue(), "");
      if (gs.nil(retJson.test_result_id)) {
          retJson.error = this.ERR;
          retJson.test_result_id = null;
          return retJson;
      }
      if (retJson.test_result_id == this.monSync.NO_AGENT) {
          retJson.error = this.CI_NO_AGENT_ERR;
          retJson.test_result_id = null;
      }
      return retJson;
  },

  validateCiAndPolicy: function(policyGr) {
      var retJson = {
          table_name: null,
          error: null
      };
      var tableName = null;
      if (policyGr.getValue("monitored_ci_type_script") == "1") {
          var cisScriptGr = new GlideScopedEvaluator().evaluateScript(policyGr, "monitored_ci_script");
          if (gs.nil(cisScriptGr)) {
              retJson.error = this.CI_SCRIPT_NIL_ERR;
              return retJson;
          }
          tableName = cisScriptGr.getTableName();
          cisScriptGr.addQuery("sys_id", this.ciId);
          cisScriptGr.query();
          if (cisScriptGr.getRowCount() < 1) {
              retJson.error = this.CI_NOT_IN_SCRIPT_ERR;
              return retJson;
          }
      } else if (policyGr.getValue("monitored_ci_type_group") == "1") {
          var cisGroupGr = new PolicyClientsGeneratorNG().getCisFromGroup(policyGr.getValue("monitored_ci_group"));
          if (gs.nil(cisGroupGr)) {
              retJson.error = this.CI_GROUP_NIL_ERR;
              return retJson;
          }
          tableName = cisGroupGr.getTableName();
          cisGroupGr.addQuery("sys_id", this.ciId);
          cisGroupGr.query();
          if (cisGroupGr.getRowCount() < 1) {
              retJson.error = this.CI_NOT_IN_GROUP_ERR;
              return retJson;
          }
      } else { // policyGr.getValue("monitored_ci_type_filter") == "1"
          tableName = policyGr.getValue("table");
          var cis = new GlideRecord(tableName);
          var filter = policyGr.getValue("filter");
          if (!gs.nil(filter))
              cis.addEncodedQuery(filter);
          cis.addQuery("sys_id", this.ciId);
          cis.query();
          if (cis.getRowCount() < 1) {
              retJson.error = this.CI_NOT_IN_FILTER_ERR;
              return retJson;
          }
      }
      retJson.table_name = tableName;
      return retJson;
  },

  validateProxy: function(policyGr, tableName) {
      var isProxy = false;
      if (policyGr.getValue("proxy_script_advanced") == "1") {
          isProxy = true;
          if (gs.nil(this.agentId)) {
              var ciGr = new GlideRecord(tableName);
              ciGr.get(this.ciId); // this methd should be called after validateCiAndPolicy (like in _build...)
              var evaluator = new GlideScopedEvaluator();
              evaluator.putVariable("answer", null);
              evaluator.putVariable("currentCiResult", ciGr);
              var proxyAgents = evaluator.evaluateScript(policyGr, "proxy_script");
              if (gs.nil(proxyAgents)) {
                  proxyAgents = evaluator.getVariable("answer");
                  if (gs.nil(proxyAgents))
                      return this.POLICY_PROXY_SCRIPT_NIL_RESULT_ERR;
              }
              var agentsGr = new GlideRecord(this.AGENTS);
              agentsGr.addQuery("agent_id", "IN", proxyAgents);
              // take only proxy that is not down/restarting and not silent
              agentsGr.addEncodedQuery(this.PROXY_AGENT_ENCODED_QUERY);
              agentsGr.query();
              if (agentsGr.next()) {
                  this.agentId = agentsGr.getUniqueValue();
                  return null;
              } else
                  return this.POLICY_PROXY_SCRIPT_NO_AGENTS_ERR;
          }
      } else if (policyGr.getValue("proxy_advanced") == "1") {
          isProxy = true;
          if (gs.nil(this.agentId)) {
              var agentCisGr = new GlideRecord(this.AGENT_CIS);
              var filter = policyGr.getValue("proxy_filter");
              if (!gs.nil(filter))
                  agentCisGr.addEncodedQuery(filter);
              // take only proxy that is not down/restarting and not silent
              agentCisGr.addEncodedQuery(this.PROXY_AGENT_CI_ENCODED_QUERY);
              agentCisGr.query();
              if (agentCisGr.next()) {
                  this.agentId = agentCisGr.getElement("agent_extended_info").getRefRecord().getUniqueValue();
                  return null;
              } else
                  return this.POLICY_PROXY_FILTER_NO_AGENTS_ERR;
          }
      } else if (policyGr.getValue("single_proxy_agent") == "1") {
          isProxy = true;
          if (gs.nil(this.agentId)) {
              var agentCiGr = new GlideRecord(this.AGENT_CIS);
              if (!agentCiGr.get(policyGr.getValue("proxy_agent")))
                  return this.POLICY_PROXY_AGENT_NOT_FOUND_ERR;
              var agentInfoGr = agentCiGr.getElement("agent_extended_info").getRefRecord();
              var status = agentInfoGr.getValue("status");
              if ((status != "0" && status != "1") || agentInfoGr.getValue("data_collection") != "0")
                  return this.POLICY_PROXY_AGENT_DOWN_OR_SILENT_ERR;
              this.agentId = agentInfoGr.getUniqueValue();
              return null;
          }
      }
      if (!gs.nil(this.agentId)) {
          if (!isProxy)
              return this.PROXY_AGENT_SPECIFIED_ERR;
          var agentGr = new GlideRecord(this.AGENTS);
          agentGr.addQuery("agent_id", this.agentId);
          agentGr.addEncodedQuery(this.PROXY_AGENT_ENCODED_QUERY);
          agentGr.query();
          if (agentGr.next())
              this.agentId = agentGr.getUniqueValue();
          else
              return this.INVALID_PROXY_AGENT_ERR;
      }
      return null;
  },

  agentId: null,

  CI_SCRIPT_NIL_ERR: "given check instance is part of a policy which selects CIs to monitor using a script, but the script returned a nil GlideRecord",
  CI_GROUP_NIL_ERR: "given check instance is part of a policy which selects CIs to monitor using a group, but the group produced a nil GlideRecord",
  CI_NOT_IN_SCRIPT_ERR: "given CI sys_id is not part of the GlideRecord result returned by the CIs selection script defined on the policy containing the given check instance",
  CI_NOT_IN_GROUP_ERR: "given CI sys_id is not part of the GlideRecord result returned by the group defined on the policy containing the given check instance",
  CI_NOT_IN_FILTER_ERR: "given CI sys_id is not part of the result set produced by the table name and filter defined on the policy containing the given check instance",
  PROXY_AGENT_SPECIFIED_ERR: "proxy agent specified although given check instance is not part of a proxy policy",
  POLICY_PROXY_AGENT_NOT_FOUND_ERR: "invalid proxy agent reference specified by the policy of the given check instance",
  POLICY_PROXY_AGENT_DOWN_OR_SILENT_ERR: "proxy agent referenced by the policy of the given check instance is silent/down",
  POLICY_PROXY_FILTER_NO_AGENTS_ERR: "no up agents returned from the proxy filter defined on the policy of the given check instance",
  POLICY_PROXY_SCRIPT_NIL_RESULT_ERR: "nil result returned from the proxy script defined on the policy of the given check instance",
  POLICY_PROXY_SCRIPT_NO_AGENTS_ERR: "no up agents returned from the proxy script defined on the policy of the given check instance",
  INVALID_PROXY_AGENT_ERR: "given proxy agent_id does not belong to an up and not silent agent",

  CHECK_INSTANCES: "sn_agent_check",
  AGENT_CIS: "sn_agent_cmdb_ci_agent",
  AGENTS: "sn_agent_ci_extended_info",
  PROXY_AGENT_ENCODED_QUERY: "statusIN0,1^data_collection=0",
  PROXY_AGENT_CI_ENCODED_QUERY: "agent_extended_info.statusIN0,1^agent_extended_info.data_collection=0",

  initialize: function() {},

  type: 'CheckInstanceTestBuilder'
});

Sys ID

bfd3fd2d5370301062d1ddeeff7b1292

Offical Documentation

Official Docs: