Name

sn_agent.MonitoringAgentsInfo

Description

No description available

Script

var MonitoringAgentsInfo = Class.create();
MonitoringAgentsInfo.prototype = {
  initialize: function() {
      this.agentsInfo = {};
      this.agentsPerMid = {};
      this.duplicatedAgents = [];
      this.fixedDuplicatedAgents = [];
      this.needDiscoPolicyPerMid = {};
      this.sortAgentsData();
      this.prepareAgentsData();
  },

  getAgentsPerMid: function(){
      return this.agentsPerMid;
  },

  getDiscoPolicyPerMid: function(){
      return this.needDiscoPolicyPerMid;
  },

  getDuplicateAgents: function(){
      return this.duplicatedAgents;
  },

  getFixedDuplicateAgents: function(){
      return this.fixedDuplicatedAgents;
  },

  /**
  * Prepare/sort the agent data structure per agent name in order to decide aboud duplicates and ci discoveries
  * - Mark all agents except first for each "agent_ids" as duplicate
  * - If "is_duplicate" flag is true and only one id in the "agent_ids" unmark this agent since it's fixed
  * - Run discovery only for first id in the "agent_ids" if "need_to_discover" is true
  * [
  *  {
  *     "agent_name1": {
  *       "agent_values": [
  *         {
  *            "agent_id": "id1",
  *            "sys_created_on": "numeric value",
  *            "is_duplicate": false
  *            "need_to_discover": false,
  *            "mid_id": "22222222222222222",
  *         },
  *         {
  *            "agent_id": "id2",
  *            "sys_created_on": "Date",
  *            "is_duplicate": true
  *            "need_to_discover": true,
  *            "mid_id": "22222222222222222",
  *         },
  *       ]
  *     }
  *   },
  *  {
  *     "agent_name2": {
  *       "agent_values": [
  *         {
  *            "agent_id": "id3",
  *            "sys_created_on": "numeric value",
  *            "is_duplicate": false
  *            "need_to_discover": true,
  *            "mid_id": "22222222222222222",
  *         }
  *       ]
  *     }
  *   }
  * ]
  */

  sortAgentsData: function(){
      //var sortAgentsDataTime = (new Date()).getTime();

      var grAgent = new GlideRecord("sn_agent_ci_extended_info");
      grAgent.addQuery("status","IN", "0,1"); // 0=Up, 1=Warning
      grAgent.query();
      while (grAgent.next()) {
          var useCloudServices = global.JSUtil.getBooleanValue(grAgent, "use_cloud_services");
          // When an agent is running using Cloud Services architecture, run using special "sys_id"
          var midId = grAgent.getValue("mid");
          var agentName = grAgent.getValue("name");
          var agentId = grAgent.getValue("agent_id");
          var cmdbCI = grAgent.getValue("cmdb_ci");
          var hostData = grAgent.getValue("host_data");
          var needToDiscover = false;
          var isDuplicate = grAgent.getValue("is_duplicate");
          var status = grAgent.getValue("status");
          var sysCreatedOn = new GlideDateTime(grAgent.getValue("sys_created_on"));

          var agent_values = [];
          var midName = "";
          // There is no CI and also host data is NOT being collected (hostData=1) nor host data collection had failed (hostData=2)
          if (!cmdbCI && hostData != '1' && hostData != '2' && hostData != '3') {
              needToDiscover = true;
              midName = useCloudServices ? ACCConstants.CLOUD_SERVICES_POD_PREFIX + midId : grAgent.mid.name;
          }
          if (this.agentsInfo[agentName]){
             agent_values = this.agentsInfo[agentName].agent_values;
          }

          agent_values.push({
              "agent_id": agentId,
              "sys_created_on": sysCreatedOn.getNumericValue(),
              "is_duplicate": isDuplicate,
              "need_to_discover": needToDiscover,
              "mid_id": midId,
              "mid_name": midName,
          });

          this.agentsInfo[agentName] = {
              "agent_values": agent_values
          };

          var agents = this.agentsPerMid[midId];
          if(!agents) {
              agents = {};
              this.agentsPerMid[midId] = agents;
          }
          agents[agentId] = false;
      }
      //gs.debug("!!PERF!! Agent sortAgentsData" + ((new Date()).getTime()-sortAgentsDataTime));
  },


  prepareAgentsData: function(){
      for (var indexAgent in this.agentsInfo) {
          var agent = this.agentsInfo[indexAgent];
          /*
          var isDuplicate = agent.agent_values.length > 1;
          Removing the below duplication logic as the current logic is based on Agent_name.
          This will be addressed in future Story implementation
          if (isDuplicate)
              agent.agent_values.sort(function (agentA, agentB) {
                  // If every metric is equal return based on the date
                  if (agentA.is_duplicate == agentB.is_duplicate && agentA.status == agentB.status && agentA.host_data == agentB.host_data && agentA.cmdb_ci == agentB.cmdb_ci)
                      return agentA.sys_created_on - agentB.sys_created_on;
                  
                  // Make agentA higher in priority (B is larger than A)
                  if (agentA.is_duplicate == '0' &&
                      ((agentA.status == '0' && agentB.status != '0') ||
                      (agentA.host_data == '0' && agentB.host_data != '0') ||
                      (agentA.cmdb_ci && !agentB.cmdb_ci)))
                      return -1;
                  
                  // Make agentB higher in priority (A is larger than B)
                  if (agentB.is_duplicate == '0' && 
                      ((agentB.status == '0' && agentA.status != '0') ||
                      (agentB.host_data == '0' && agentA.host_data != '0') ||
                      (agentB.cmdb_ci && !agentA.cmdb_ci)))
                      return 1;
                  
                  // Default - based on created date
                  return agentA.sys_created_on - agentB.sys_created_on;
              });
          */

          if (agent.agent_values[0].need_to_discover == true) {
              var midName = agent.agent_values[0].mid_name;
              var agentId = agent.agent_values[0].agent_id;
              var needDiscoPolicy = this.needDiscoPolicyPerMid[midName];
              if (!needDiscoPolicy) {
                  needDiscoPolicy = {
                      clients_cis: {}
                  };
                  this.needDiscoPolicyPerMid[midName] = needDiscoPolicy;
              }
              needDiscoPolicy["clients_cis"][agentId] = {};
          }
          
          /*
          Removing the below duplication logic as the current logic is based on Agent_name.
          This will be addressed in future Story implementation

          if( agent.agent_values[0].is_duplicate == true && agent.agent_values.length >= 1) {
              this.fixedDuplicatedAgents.push(agent.agent_values[0].agent_id);
          }

          if (isDuplicate)
              for (var i=1; i < agent.agent_values.length; i++) {
                  var duplicateAgentID = agent.agent_values[i].agent_id;
                  this.duplicatedAgents.push(duplicateAgentID);
                  gs.error("Found duplicated agent name: " + indexAgent + ", agent_id: " + duplicateAgentID);
              }
          */
      }
  },


  type: 'MonitoringAgentsInfo'
};

Sys ID

f4a2a96193acf300692e35bb357ffb9d

Offical Documentation

Official Docs: