Name

sn_agent.GetAllIpAddressesForMidServer

Description

A Script that can create an ECC queue per MID to run command to get all IPs. Also have a parsing method to parse the data from the IP command and populate ecc_agent_ip_address

Script

var GetAllIpAddressesForMidServer = Class.create();
GetAllIpAddressesForMidServer.prototype = {
  initialize: function() {
  },
  
  runGetIpAddressesOnMid: function(midIp, midName, midOs){
  	if (!midIp || !midName)
  		return;
  	
  	/**
  		IMPORTANT:
  		If you change the getIpCommand value - you MUST also change the "Process IP Addresses per MID" Business Rule condition to match
  		AND
  		In the Script Action "Update IP Addresses When Mid Is Up"
  	**/
  	var getIpCommand = "powershell -ExecutionPolicy bypass .\\scripts\\powershell\\GetAllIpsForWindows.ps1"; // Run Windows Script file
  	
  	if (midOs && midOs.toLowerCase() != "windows")
  		getIpCommand = "bash ./scripts/SSH/GetAllIpsForLinux.sh"; // Run Linux script file
  	
  	var eccGr = new GlideRecord("ecc_queue");
  	eccGr.initialize();
  	eccGr.setValue("agent", "mid.server." + midName);
  	eccGr.setValue("name", getIpCommand);
  	eccGr.setValue("priority", "1");
  	eccGr.setValue("queue", "output");
  	eccGr.setValue("source", midIp);
  	eccGr.setValue("state", "ready");
  	eccGr.setValue("topic", "Command");
  	
  	eccGr.insert();
  },
  
  parseIpAddressesAndPopulateTable: function(ecc){
  	var payload = '' + ecc.payload;
  	var stdout = "";
  	var stderr = "";
  	var arrIps = [];
  	
  	var streamsRegex = /<stdout>((?:.|\s)*?)<\/stdout>(?:<stderr>(.*?)<\/stderr>)?/g;
  	var streamsMatch = streamsRegex.exec(payload);
  	
  	if (!streamsMatch) {
  		gs.error("GetAllIpAddressesForMidServer: Could not get IP addresses command response for MID=" + midName);
  		return;
  	}
  	
  	if (streamsMatch[1])
  		stdout = streamsMatch[1].trim();
  	if (streamsMatch[2])
  		stderr = streamsMatch[2].trim();
  	
  	if (!stdout && !stderr) {
  		gs.error("GetAllIpAddressesForMidServer: Could not get IP addresses command output or error for MID=" + midName);
  		return;
  	}
  	
  	if (!stdout && stderr) {
  		gs.error("GetAllIpAddressesForMidServer: Something went wrong when trying to fetch IP address from mid=" + midName + ". Error=" + stderr );
  		return;
  	}
  	
  	if (stdout && stderr) {
  		gs.warn("GetAllIpAddressesForMidServer: We got stdout=" + stdout + ", and stderr=" + stderr);
  	} else {
  		gs.info("GetAllIpAddressesForMidServer: We got stdout=" + stdout);
  	}
  	
  	arrIps = stdout.split(/\r?\n/);
  	
  	this.populateTableWithIpsWithEcc(arrIps, ecc);
  },
  
  isValidIpAddress: function(ip){
  	if (!ip || ip == "127.0.0.1" || ip.startsWith("169.254.")) // not empty, not default loopback and not Windows default IP address range
  		return false;
  	
  	return true;
  },
  
  populateTableWithIpsWithEcc: function(arrIps, ecc) {
  	var midIp = ecc.source;
  	var midName = ecc.agent;
  	
  	if (!arrIps)
  		return;
  	
  	var midGr = new GlideRecord("ecc_agent");
  	midGr.addQuery("ip_address", midIp);
  	midGr.addQuery("name", midName.substring("mid.server.".length));
  	midGr.query();
  	
  	if (!midGr.next() || midGr.getRowCount() > 1){
  		gs.error("GetAllIpAddressesForMidServer: Something went wrong: Got no MID results or more than 1 MID servers for midname=" + midName + " and mid IP =" + midIp );
  		return;
  	}

  	var midSysId = midGr.getUniqueValue();
  	var midDomain = midGr.getValue("sys_domain");
  	
  	this.populateTableWithIpsAndDeleteNonDiscoverIps(arrIps, midSysId, midDomain);
  },
  
  populateTableWithIpsAndDeleteNonDiscoverIps: function(arrIps, midSysId, midDomain) {
  	var midIpGr = new GlideRecord("ecc_agent_ip_address");
  	var arrSysIdsToKeep = [];
  	
  	for (var i=0; i<arrIps.length; i++){
  		if (!this.isValidIpAddress(arrIps[i]))
  			continue;
  			
  		var sysId = this.getSysIdOfRecord(midSysId, arrIps[i], midDomain);
  		if (sysId) {
  			arrSysIdsToKeep.push(sysId);
  			continue;
  		}
  		
  		midIpGr.initialize();
  		midIpGr.setValue("mid_server", midSysId);
  		midIpGr.setValue("ip_address", arrIps[i]);
  		
  		if (midDomain)
  			midIpGr.setValue("sys_domain", midDomain);
  	
  		arrSysIdsToKeep.push(midIpGr.insert());
  	}
  	
  	// There must be at least 1 IP.
  	if (arrSysIdsToKeep.length == 0){
  		gs.error("GetAllIpAddressesForMidServer: we got 0 IPs for the mid when trying to insert/update - this is an error as at least 1 IP should exist");
  		return;
  	}
  	
  	// Delete the old IP addresses as they are not discoverable now
  	midIpGr = new GlideRecord("ecc_agent_ip_address");
  	midIpGr.addQuery('mid_server', midSysId);
  	midIpGr.addQuery('sys_id', 'NOT IN', arrSysIdsToKeep);
  	midIpGr.query();
  	
  	if (midIpGr.getRowCount() > 0 ){
  		midIpGr.deleteMultiple();
  	}
  } ,
  
  getSysIdOfRecord: function(midSysId, ip, domain) {
  	var midIpGrForRead = new GlideRecord("ecc_agent_ip_address");
  	midIpGrForRead.addQuery("mid_server", midSysId);
  	midIpGrForRead.addQuery("ip_address", ip);
  	if (domain)
  		midIpGrForRead.addQuery("sys_domain", domain);
  	midIpGrForRead.query();
  	
  	if(midIpGrForRead.next())
  		return midIpGrForRead.getUniqueValue();
  	
  	return null;
  },
  
  type: 'GetAllIpAddressesForMidServer'
};

Sys ID

1aede6aa535110102e07ddeeff7b12fc

Offical Documentation

Official Docs: