Name

sn_agent.AccCheckDefsAPI

Description

API for managing Check Definitions.

Script

var AccCheckDefsAPI = Class.create();

// Returns an array of JSONs, each one containing the following information about the check definition:
// name, command, plugins, timeout, is proxy valid, is background, check type, check group and sys_id.
//
// An encoded query and a limit of checks can be given. Use null or undefined for both if they
// are not required. If no limit is given or the given limit is above 20,000, then the maximum
// limit of 20,000 will be used.
//
// The 3rd argument to this method, withParams, specifies if to return, per check, a JSON
// ("params") containing information about the check parameters (if any).
//
// Output example:
//
// [
//   {
//     "name":  "check",
//     "command":  "echo check",
//     "plugins":  ["plugin"],
//     "timeout":  60,
//     "proxy_valid":  false,
//     "backgroud":  false,
//     "check_type":  "Type",
//     "check_group":  "Group",
//   }
// ]
AccCheckDefsAPI.getChecksList = function(encodedQuery, limit, withParams) {
  var checkDefs = [];
  var checkDefGr = new GlideRecord(AccCheckDefsAPI.CHECK_DEFINITION);
  if (!gs.nil(encodedQuery))
      checkDefGr.addEncodedQuery(encodedQuery);
  if (gs.nil(limit) || limit < 1)
      checkDefGr.setLimit(AccCheckDefsAPI.DEFAULT_LIST_LIMIT);
  else
      checkDefGr.setLimit(limit);
  checkDefGr.query();
  while (checkDefGr.next())
      checkDefs.push(AccCheckDefsAPI.getCheckDefJson(checkDefGr, withParams));
  return checkDefs;
};

// Given a check definition id, return the following information about the check definition with that id:
// name, command, plugins, timeout, is proxy valid, is background, check type, check group and sys_id.
//
// This method returns a JSON with 2 attributes: an error message ("error") and a JSON with the above information
// about the requested check definition ("check"). if no check exists with the given id then the error message will not
// be null and the check JSON will be null. if it does exist than output.error == null and output.check != null.
//
// The 2nd argument to this method, withParams, specifies if to return a JSON ("params") containing information
// about the check parameters (if any).
//
// Output example:
//
// {
//   "error": null,
//   "check": {
//     "name":  "check",
//     "command":  "echo check",
//     "plugins":  ["plugin"],
//     "timeout":  60,
//     "proxy_valid":  false,
//     "backgroud":  false,
//     "check_type":  "Type",
//     "check_group":  "Group",
//   }
// }
AccCheckDefsAPI.getCheck = function(checkDefId, withParams) {
  var retJson = {
      check: null,
      error: null
  };
  var checkDefGr = new GlideRecord(AccCheckDefsAPI.CHECK_DEFINITION);
  if (checkDefGr.get(checkDefId))
      retJson.check = AccCheckDefsAPI.getCheckDefJson(checkDefGr, withParams);
  else
      retJson.error = AccCheckDefsAPI.RECORD_NOT_FOUND;
  return retJson;
};

// Given a check definition id and a JSON which maps check definition fields to their values, update the fields of the check
// with that id with the given values.
AccCheckDefsAPI.updateCheck = function(checkDefId, updateJson) {
  return AccCheckDefsAPI.updateRecordInTable(AccCheckDefsAPI.CHECK_DEFINITION, checkDefId, updateJson);
};

// Given a check parameter definition id and a JSON which maps parameter fields to their values, update the fields of the parameter
// with that id with the given values.
AccCheckDefsAPI.updateCheckParam = function(checkDefParamId, updateJson) {
  return AccCheckDefsAPI.updateRecordInTable(AccCheckDefsAPI.CHECK_PARAMETER_DEFINITION, checkDefParamId, updateJson);
};

// Given a check secure parameter definition id and a JSON which maps parameter fields to their values, update the fields of the parameter
// with that id with the given values.
AccCheckDefsAPI.updateCheckSecureParam = function(checkDefSecureParamId, updateJson) {
  return AccCheckDefsAPI.updateRecordInTable(AccCheckDefsAPI.CHECK_SECURE_PARAMETER_DEFINITION, checkDefSecureParamId, updateJson);
};

AccCheckDefsAPI.CHECK_DEFINITION = "sn_agent_check_def";
AccCheckDefsAPI.CHECK_PARAMETER_DEFINITION = "sn_agent_check_param_def";
AccCheckDefsAPI.CHECK_SECURE_PARAMETER_DEFINITION = "sn_agent_check_secure_param_def";
AccCheckDefsAPI.DEFAULT_LIST_LIMIT = 20000;
AccCheckDefsAPI.RECORD_NOT_FOUND = "no record found with given sys_id";

AccCheckDefsAPI.getCheckDefJson = function(checkDefGr, withParams) {
  var checkDef = {
      name: checkDefGr.getValue("name"),
      command: checkDefGr.getValue("command"),
      plugins: null,
      timeout: parseInt(checkDefGr.getValue("timeout")),
      proxy_valid: checkDefGr.getDisplayValue("is_proxy_valid") == "true",
      background: checkDefGr.getDisplayValue("background") == "true",
      check_type: checkDefGr.getDisplayValue("check_script_type"),
      check_group: checkDefGr.getDisplayValue("check_group"),
      sys_id: checkDefGr.getUniqueValue()
  };
  var assetsStr = checkDefGr.getDisplayValue("assets");
  if (gs.nil(assetsStr))
      checkDef.plugins = [];
  else
      checkDef.plugins = assetsStr.split(",");
  if (withParams) {
      checkDef.params = [];
      var checkDefParamsGr = new GlideRecord(AccCheckDefsAPI.CHECK_PARAMETER_DEFINITION);
      checkDefParamsGr.addQuery("check_def", checkDefGr.getUniqueValue());
      checkDefParamsGr.query();
      while (checkDefParamsGr.next())
          checkDef.params.push({
              name: checkDefParamsGr.getValue("name"),
              active: checkDefParamsGr.getDisplayValue("active") == "true",
              mandatory: checkDefParamsGr.getDisplayValue("mandatory") == "true",
              default_value: checkDefParamsGr.getValue("default_value"),
              sys_id: checkDefParamsGr.getUniqueValue()
          });
      checkDef.secure_params = [];
      var checkDefSecureParamsGr = new GlideRecord(AccCheckDefsAPI.CHECK_SECURE_PARAMETER_DEFINITION);
      checkDefSecureParamsGr.addQuery("check_def", checkDefGr.getUniqueValue());
      checkDefSecureParamsGr.query();
      while (checkDefSecureParamsGr.next())
          checkDef.secure_params.push({
              name: checkDefSecureParamsGr.getValue("name"),
              active: checkDefSecureParamsGr.getDisplayValue("active") == "true",
              order: parseInt(checkDefSecureParamsGr.getValue("order")),
              sys_id: checkDefSecureParamsGr.getUniqueValue()
          });
  }
  return checkDef;
};

AccCheckDefsAPI.updateRecordInTable = function(table, recordId, updateJson) {
  var updateGr = new GlideRecord(table);
  if (!updateGr.get(recordId))
      return AccCheckDefsAPI.RECORD_NOT_FOUND;
  for (var field in updateJson)
      updateGr.setValue(field, updateJson[field]);
  updateGr.update();
  var updateErr = updateGr.getLastErrorMessage();
  if (!gs.nil(updateErr))
      return updateErr;
  return null;
};

AccCheckDefsAPI.prototype = {
  type: 'AccCheckDefsAPI'
};

Sys ID

e845b5875328301062d1ddeeff7b1293

Offical Documentation

Official Docs: