Name

sn_agent.AgentKeepAlive

Description

No description available

Script

var AgentKeepAlive = Class.create();

AgentKeepAlive.AGENT_UP = '0';
AgentKeepAlive.AGENT_WARNING = '1';
AgentKeepAlive.AGENT_DOWN = '2';
AgentKeepAlive.AGENT_RESTARTING = '3';
AgentKeepAlive.AGENT_UPGRADING = '4';
AgentKeepAlive.AGENT_DISCONNECTED = '5';

AgentKeepAlive.prototype = {
  initialize: function() {},

  type: 'AgentKeepAlive'
};

AgentKeepAlive.getAgentsWithNoUpdateByStatus = function(timeAgo, status) {
  var gr = new GlideRecord('sn_agent_ci_extended_info');
  gr.addQuery('status', status);
  // Null query is needed for DEF0220355
  gr.addNullQuery('last_refreshed').addOrCondition('last_refreshed', '<', timeAgo);
  gr.orderBy('mid'); // This will group agents by MID which can help for later processing (like triggering checks) to be more efficient
  gr.setWorkflow(false);

  return gr;
};

AgentKeepAlive.keepAlive = function() {
  var timeAgo = new GlideDateTime();
  var duration = gs.getProperty('sn_agent.keep_alive.alive_duration', 120);
  timeAgo.addSeconds(0 - duration);

  //first switch to Warning and only then to Down, otherwise we will skip the initial phase.
  // Get GR with Warning or Restart status - to create events
  var arrStatus = [AgentKeepAlive.AGENT_WARNING, AgentKeepAlive.AGENT_RESTARTING];
  var gr = AgentKeepAlive.getAgentsWithNoUpdateByStatus(timeAgo, arrStatus);
  gr.query();
  if (gr.hasNext())
      AgentKeepAlive.createEvents(gr, 'down');

  // Get GR with Warning or Restart status - to update values (we cannot use GR method to set location as this is a Scoped GR)
  // First update status=down to all agents with host data collection = collecting...
  gr = AgentKeepAlive.getAgentsWithNoUpdateByStatus(timeAgo, arrStatus);
  gr.addQuery('host_data', '1'); // Agent host data collection is still in collecting phase
  gr.setValue('status', AgentKeepAlive.AGENT_DOWN);
  gr.setValue('host_data', '2'); // Set host data collection to failed
  gr.updateMultiple();
  // Now update all the rest of the agents that are still with Warning or Restart status to status=down
  gr = AgentKeepAlive.getAgentsWithNoUpdateByStatus(timeAgo, arrStatus);
  gr.setValue('status', AgentKeepAlive.AGENT_DOWN);
  gr.updateMultiple();

  // Get GR with Disconnected status and MID is back up - change to Down
  gr = AgentKeepAlive.getAgentsWithNoUpdateByStatus(timeAgo, AgentKeepAlive.AGENT_DISCONNECTED);
  gr.addQuery('mid.status', 'Up');
  gr.setValue('status', AgentKeepAlive.AGENT_DOWN);
  gr.updateMultiple();

  // Get GR with Up status and MID is down - change to Disconnected
  gr = AgentKeepAlive.getAgentsWithNoUpdateByStatus(timeAgo, AgentKeepAlive.AGENT_UP);
  gr.addQuery('mid.status', 'Down');
  gr.setValue('status', AgentKeepAlive.AGENT_DISCONNECTED);
  gr.updateMultiple();

  // Get rest of GR with Up status - change to Warning
  gr = AgentKeepAlive.getAgentsWithNoUpdateByStatus(timeAgo, AgentKeepAlive.AGENT_UP);
  gr.setValue('status', AgentKeepAlive.AGENT_WARNING);
  gr.updateMultiple();

  var upgradeTimeStr = gs.getProperty('sn_agent.agent_upgrade_wait_time', 30); // Default we give the upgrade up to 30 minutes to finish
  var upgradeTime = parseInt(upgradeTimeStr);

  // Update all Upgrading Agents status to Down if they did not get back to Up state
  if (!upgradeTime)
      upgradeTime = 30; // Default we give the upgrade up to 30 minutes to finish
  var upgradeTimeAgo = new GlideDateTime();
  upgradeTimeAgo.addSeconds(-1 * upgradeTime * 60);
  // Update upgrade history from InProcess to Failed
  gr = AgentKeepAlive.getAgentsWithNoUpdateByStatus(upgradeTimeAgo, AgentKeepAlive.AGENT_UPGRADING);
  var acc = new AgentUpgradeUtil();
  acc.markFailedIfTimeout(gr);
  // Get GR with Upgrading status - to Change to Down
  gr = AgentKeepAlive.getAgentsWithNoUpdateByStatus(upgradeTimeAgo, AgentKeepAlive.AGENT_UPGRADING);
  gr.setValue('status', AgentKeepAlive.AGENT_WARNING);
  gr.updateMultiple();

  // Up events are handled in the method classifyClientsAndUpdate under MonitoringConfig
};

AgentKeepAlive.createEvents = function(gr, state) {
  var arr = [];

  while (gr.next()) {
      arr.push(gr.getValue('agent_id'));
  }

  AgentKeepAlive.chunkAndSendEvents(arr, state);
};

AgentKeepAlive.chunkAndSendEvents = function(arr, state) {
  var MAX_CHARS = 4000; // max per Parm1
  var agentIdsLenght = 16;
  var maxArrItems = MAX_CHARS / (agentIdsLenght + 1);
  var i = 0;

  var chunks = MonitoringUtils.chunkArray(arr, maxArrItems);
  for (i = 0; i < chunks.length; i++) {
      gs.eventQueue('sn_agent.' + state, undefined, chunks[i].join(','));
  }
};

Sys ID

0a90eeeec314130039a3553a81d3aeba

Offical Documentation

Official Docs: