Name
sn_cmdb_int_util.ADMHelper
Description
No description available
Script
var ADMHelper = Class.create();
ADMHelper.prototype = {
initialize: function (tcp_connections, runningProcess, ciSysId, props) {
this.running_processes = runningProcess;
this.connections = tcp_connections;
this.ciSysId = ciSysId;
var vergs = props.dummyAppVersion;
if (gs.nil(vergs)) {
if (vergs == null) {
vergs = "unknown";
} else {
vergs = "";
}
}
this.dummyVersion = vergs;
this.includeVersion = (props.includeDummyAppCiVersion == "true") ? true : false;
},
addConnectionsAndRelationshipsToCMDB: function (postionInCIJson, hostname) {
// massage raw connections data a bit...
this.connections = new global.CookConnections('' + this.ciSysId).process(this.connections);
// enrich the connections and running processes...
var epac = new global.EnrichProcessesAndConnections(this);
epac.process();
var relatedData = new AdmData(this.running_processes, this.connections, this.ciSysId);
var adm = new global.ApplicationDependencyMapping(relatedData);
running_processes = adm.matched_processes;
this.runningProcessReconcile(epac, this.ciSysId, this.running_processes);
var relatedListdataObj = {};
relatedListdataObj['cmdb_tcp'] = {
data: this.connections,
refName: 'computer',
keyName: 'type,ip,port,pid',
deleteGr: false
};
var cmdbGr = new GlideRecord("cmdb_ci");
cmdbGr.get("sys_id", this.ciSysId);
new global.DiscoveryReconciler(cmdbGr, relatedListdataObj).process();
return this.createApplicationCIs(adm.matched_processes, postionInCIJson, hostname);
},
runningProcessReconcile: function (epac, ciSysId, running_process) {
// rebuild the process tree roots
epac.rps = running_process;
epac.indexPIDs();
epac.buildProcessTree();
// get rid of any nonsense connections...
epac.noNonsense();
// flip any "to" connections to "on" connections that need to be flipped
epac._flipAllConnections();
// Remove connections if process or any parent process has the same listening port as the process
// The order is important that this follows _flipAllConnections
epac._removeConnections();
// fix up the values in the sensor...
epac.sensor.running_processes = epac.rps;
epac.sensor.connections = epac.conns;
epac.fixProcessPorts();
// reconcile the running processes...
var rpr = new global.RunningProcessReconciler(ciSysId, epac.rps, epac.processTreeRoots, epac.byPID);
rpr.reconcile();
// now that we've got process sys_ids, fix up our connection references...
epac.fixConnectionField('sys_id', 'process');
// deduplicate connections before reconciling...
epac.dedupeConnections();
},
/**
* Inputs:
* - ADM matched processes
* - CI reference in IRE payload
* - hostname
* Loops through all the ADM matches process and creates Application CI with relationship records
**/
createApplicationCIs: function (matched_processes, currentPositioninCIJson, hostname) {
var pidToPorts = this.getProcessWithAssociatedPorts(this.connections);
var applicationCIPosition = currentPositioninCIJson;
var items = [];
var relations = [];
var references = [];
for (var process = 0; process < matched_processes.length; process++) {
var matchedProcess = matched_processes[process];
if (!matchedProcess.computer) {
gs.debug('No computer field in on the matchedProcess. application CI will not be created');
continue;
}
var applicationCI = {};
applicationCI['className'] = matchedProcess.classifier.table;
var applicationCIValues = {};
applicationCI['values'] = applicationCIValues;
var tcpPort = pidToPorts[matchedProcess.pid];
if (typeof tcpPort != 'undefined') {
if (tcpPort.substring(1).indexOf(':') == tcpPort.length - 2) {
tcpPort = tcpPort.substring(1, tcpPort.length - 1);
}
applicationCIValues['tcp_port'] = tcpPort;
}
applicationCIValues['running_process_command'] = matchedProcess['command'];
applicationCIValues['running_process_key_parameters'] = matchedProcess['key_parameters'];
applicationCIValues['config_file'] = matchedProcess['file'];
applicationCIValues['version'] = matchedProcess['version'];
var applicationCIGr = new GlideRecord(matchedProcess.classifier.table);
var appName = applicationCIGr.getClassDisplayValue();
appName = appName + '@' + hostname;
applicationCIValues['name'] = appName;
if (this.includeVersion) {
applicationCIValues['version'] = this.dummyVersion;
}
// Find the running process sys_id
var processGr = new GlideRecord('cmdb_running_process');
processGr.addQuery('computer', matchedProcess.computer);
processGr.addQuery('pid', matchedProcess.pid);
processGr.query();
if (processGr.next())
applicationCIValues['running_process'] = processGr.getUniqueValue();
else
gs.debug('process ' + matchedProcess.pid + ' not found on computer ' + matchedProcess.computer);
items.push(applicationCI);
relations.push(this.addRelationForPositionsAndType("Runs on::Runs", applicationCIPosition, 0));
applicationCIPosition++;
}
return [items, relations];
},
/**
* helper to add relationship
**/
addRelationForPositionsAndType: function (type, parent, child) {
var relationJsonObj = {};
relationJsonObj.type = type;
relationJsonObj.parent = parent;
relationJsonObj.child = child;
return relationJsonObj;
},
/**
* Construct PID to Ports mapping
**/
getProcessWithAssociatedPorts: function (connections) {
var pidToPorts = {};
if (connections != null && typeof connections != 'undefined' && connections.length > 0) {
var called = 0;
for (var idx = 0; idx < connections.length; idx++) {
var tcp = connections[idx];
this.addPortToPID(pidToPorts, tcp.pid, tcp.port);
called = called + 1;
}
}
return pidToPorts;
},
/**
* Format ports with ":" saperator.
* handle multiple ports found for PID
**/
addPortToPID: function (pidToPorts, pid, port) {
if ( !gs.nil(port)) {
var ports = pidToPorts[pid];
if (typeof ports == 'undefined') {
pidToPorts[pid] = ':' + port + ':';
} else {
if (!ports.includes(':' + port + ':')) {
pidToPorts[pid] = ports + port + ':';
}
}
}
},
type: 'ADMHelper'
};
var AdmData = Class.create();
AdmData.prototype = {
initialize: function (r_p, conn, ci) {
this.running_processes = r_p;
this.connections = conn;
this.ci = ci;
},
getCmdbCi: function () {
return this.ci;
}
};
Sys ID
6157145a77502110258d234468106139